Commit 9815050d authored by Kostas Papadimitriou's avatar Kostas Papadimitriou Committed by Giorgos Korfiatis
Browse files

Remove resource/service model dependencies

- Remove foreign key dependency between Service/Resource model. Replace
  it with a service CharField to keep track of the service identifier
  that the resource was registered from.
- Remove all references to resource service name in resource lookups
parent 11814b66
......@@ -40,8 +40,7 @@ from smtplib import SMTPException
from astakos.im.models import (
AstakosUser,
Resource, Service, RESOURCE_SEPARATOR,
Project, ProjectApplication, ProjectMembership)
Resource, Service, Project, ProjectApplication, ProjectMembership)
from astakos.im.api.backends.base import (
BaseBackend, SuccessResult, FailureResult)
from astakos.im.api.backends.errors import (
......@@ -247,9 +246,7 @@ class DjangoBackend(BaseBackend):
t = (i if i else 0 for i in t)
(entity, name, quantity, capacity, importLimit, exportLimit,
imported, exported, returned, released, flags) = t
service, sep, resource = name.partition(RESOURCE_SEPARATOR)
resource = Resource.objects.select_related().get(
service__name=service, name=resource)
resource = Resource.objects.select_related().get(name=name)
currValue=quantity + imported - released - exported + returned
d = dict(name=name,
description=resource.desc,
......
......@@ -57,7 +57,7 @@ from django.core.exceptions import PermissionDenied
from astakos.im.models import (
AstakosUser, EmailChange, Invitation,
Resource, PendingThirdPartyUser, get_latest_terms, RESOURCE_SEPARATOR,
Resource, PendingThirdPartyUser, get_latest_terms,
ProjectApplication, Project)
from astakos.im.settings import (
INVITATIONS_PER_LEVEL, BASEURL, SITENAME, RECAPTCHA_PRIVATE_KEY,
......@@ -865,8 +865,7 @@ class ProjectApplicationForm(forms.ModelForm):
if name.endswith('_uplimit'):
subs = name.split('_uplimit')
prefix, suffix = subs
s, sep, r = prefix.partition(RESOURCE_SEPARATOR)
resource = Resource.objects.get(service__name=s, name=r)
resource = Resource.objects.get(name=prefix)
# keep only resource limits for selected resource groups
if self.data.get(
......@@ -874,9 +873,9 @@ class ProjectApplicationForm(forms.ModelForm):
) == "1":
d = model_to_dict(resource)
if uplimit:
d.update(dict(service=s, resource=r, uplimit=uplimit))
d.update(dict(resource=prefix, uplimit=uplimit))
else:
d.update(dict(service=s, resource=r, uplimit=None))
d.update(dict(resource=prefix, uplimit=None))
append(d)
ordered_keys = RESOURCES_PRESENTATION_DATA['resources_order']
......
......@@ -154,10 +154,9 @@ for a single user from the command line
if capacity == 'default':
try:
service, sep, name = resource.partition('.')
q = AstakosUserQuota.objects.get(
user=user,
resource__service__name=service,
resource__name=resource,
resource__name=name)
q.delete()
except Exception as e:
......
......@@ -98,8 +98,6 @@ def get_content_type():
_content_type = content_type
return content_type
RESOURCE_SEPARATOR = '.'
inf = float('inf')
class Service(models.Model):
......@@ -132,15 +130,6 @@ class Service(models.Model):
def __str__(self):
return self.name
@property
def resources(self):
return self.resource_set.all()
@resources.setter
def resources(self, resources):
for s in resources:
self.resource_set.create(**s)
_presentation_data = {}
def get_presentation(resource):
......@@ -154,10 +143,10 @@ def get_presentation(resource):
class Resource(models.Model):
name = models.CharField(_('Name'), max_length=255, unique=True)
service = models.ForeignKey(Service)
desc = models.TextField(_('Description'), null=True)
unit = models.CharField(_('Name'), null=True, max_length=255)
group = models.CharField(_('Group'), null=True, max_length=255)
service = models.CharField(_('Service identifier'), max_length=255,
null=True)
unit = models.CharField(_('Unit'), null=True, max_length=255)
uplimit = intDecimalField(default=0)
objects = ForUpdateManager()
......@@ -174,13 +163,20 @@ class Resource(models.Model):
'unit': self.unit,
}
@property
def group(self):
default = self.name
return get_presentation(str(self)).get('group', default)
@property
def help_text(self):
return get_presentation(str(self)).get('help_text', '')
default = "%s resource" % self.name
return get_presentation(str(self)).get('help_text', default)
@property
def help_text_input_each(self):
return get_presentation(str(self)).get('help_text_input_each', '')
default = "%s resource" % self.name
return get_presentation(str(self)).get('help_text_input_each', default)
@property
def is_abbreviation(self):
......@@ -188,15 +184,16 @@ class Resource(models.Model):
@property
def report_desc(self):
return get_presentation(str(self)).get('report_desc', '')
default = "%s resource" % self.name
return get_presentation(str(self)).get('report_desc', default)
@property
def placeholder(self):
return get_presentation(str(self)).get('placeholder', '')
return get_presentation(str(self)).get('placeholder', self.unit)
@property
def verbose_name(self):
return get_presentation(str(self)).get('verbose_name', '')
return get_presentation(str(self)).get('verbose_name', self.name)
@property
def display_name(self):
......@@ -408,8 +405,7 @@ class AstakosUser(User):
self, resource, capacity,
update=True):
"""Raises ObjectDoesNotExist, IntegrityError"""
s, sep, r = resource.partition(RESOURCE_SEPARATOR)
resource = Resource.objects.get(service__name=s, name=r)
resource = Resource.objects.get(name=resource)
if update:
AstakosUserQuota.objects.update_or_create(
user=self, resource=resource, defaults={
......@@ -422,8 +418,7 @@ class AstakosUser(User):
)
def get_resource_policy(self, resource):
s, sep, r = resource.partition(RESOURCE_SEPARATOR)
resource = Resource.objects.get(service__name=s, name=r)
resource = Resource.objects.get(name=resource)
default_capacity = resource.uplimit
try:
policy = AstakosUserQuota.objects.get(user=self, resource=resource)
......@@ -433,7 +428,7 @@ class AstakosUser(User):
def remove_resource_policy(self, service, resource):
"""Raises ObjectDoesNotExist, IntegrityError"""
resource = Resource.objects.get(service__name=service, name=resource)
resource = Resource.objects.get(name=resource)
q = self.policies.get(resource=resource).delete()
def update_uuid(self):
......@@ -1445,7 +1440,7 @@ class ProjectApplication(models.Model):
def add_resource_policy(self, service, resource, uplimit):
"""Raises ObjectDoesNotExist, IntegrityError"""
q = self.projectresourcegrant_set
resource = Resource.objects.get(service__name=service, name=resource)
resource = Resource.objects.get(name=resource)
q.create(resource=resource, member_capacity=uplimit)
def members_count(self):
......@@ -1453,8 +1448,8 @@ class ProjectApplication(models.Model):
@property
def grants(self):
return self.projectresourcegrant_set.values(
'member_capacity', 'resource__name', 'resource__service__name')
return self.projectresourcegrant_set.values('member_capacity',
'resource__name')
@property
def resource_policies(self):
......
......@@ -179,12 +179,11 @@ class MessagesNode(template.Node):
@register.simple_tag
def get_grant_value(rname, form):
grants = form.instance.grants
service_name, resource_name = rname.split('.',1)
try:
return form.instance.projectresourcegrant_set.get(resource__name=resource_name,
resource__service__name=service_name).member_capacity
except:
return ''
r = form.instance.projectresourcegrant_set.get(resource__name=rname).member_capacity
except Exception, e:
r = ''
return r
@register.tag(name="provider_login_url")
@basictag(takes_context=True)
......
......@@ -42,9 +42,8 @@ from django.core.paginator import Paginator, EmptyPage
from django.db.models.query import QuerySet
from astakos.im.settings import PAGINATE_BY
from astakos.im.presentation import RESOURCES_PRESENTATION_DATA
from astakos.im.models import RESOURCE_SEPARATOR, ProjectResourceGrant
from astakos.im import settings
from astakos.im.models import ProjectResourceGrant
register = template.Library()
......@@ -203,9 +202,8 @@ def resource_grants(project_definition):
grants = project_definition.projectresourcegrant_set
grants = grants.values_list(
'resource__name',
'resource__service__name',
'member_capacity'
)
return dict((RESOURCE_SEPARATOR.join([e[1], e[0]]), e[2]) for e in grants)
return dict((e[0], e[1]) for e in grants)
except:
return {}
......@@ -78,8 +78,7 @@ from astakos.im.activation_backends import get_backend, SimpleBackend
from astakos.im import tables
from astakos.im.models import (
AstakosUser, ApprovalTerms,
EmailChange, RESOURCE_SEPARATOR,
AstakosUserAuthProvider, PendingThirdPartyUser,
EmailChange, AstakosUserAuthProvider, PendingThirdPartyUser,
ProjectApplication, ProjectMembership, Project)
from astakos.im.util import (
get_context, prepare_response, get_query, restrict_next)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment