diff --git a/models.py b/models.py index 874cb7e..4c99722 100644 --- a/models.py +++ b/models.py @@ -13,174 +13,261 @@ M_NAME = 400 D_NAME = 1000 -# trait -class Trait(models.Model): - name = models.CharField(max_length=M_NAME, help_text = "The name of the measured trait") - owner = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_owner", help_text = "The identifier of the uploader") - value = models.CharField(help_text = "The value of the trait", max_length=50) - units = models.CharField(max_length=50, help_text = "Units in which the trait was measured", blank=True, null=True) - description = models.CharField(max_length=D_NAME,blank=True,null=True, help_text = "A longer description of the trait") - def __unicode__(self): - return u'%s (%.4f %s)' % (self.name, self.value, self.units) +# reference + + +class Ref(models.Model): + doi = models.CharField(max_length=200, blank=True, null=True, + help_text="DOI of the reference object") + owner = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_owner") + jstor = models.CharField(max_length=200, blank=True, null=True, + help_text="JSTOR identifier of the reference object") + pmid = models.CharField(max_length=200, blank=True, null=True, + help_text="PubMed ID of the reference object") + url = models.CharField(max_length=200, blank=True, null=True, + help_text="URL of the reference object") + fulltext = models.CharField(max_length=900, blank=True, null=True, + help_text="Full text of the reference") # environment + + class Environment(models.Model): - name = models.CharField(max_length=M_NAME, help_text = "The environmental property being measured") - owner = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_owner") - value = models.CharField(help_text = "The value of the environmental property", max_length = 50) - units = models.CharField(max_length=50, help_text = "The units in which the environmental property is measured", blank=True, null=True) - description = models.CharField(max_length=D_NAME,blank=True,null=True, help_text = "A description of the environmental property") - def __unicode__(self): - return u'%s (%.4f %s)' % (self.name, self.value, self.units) + name = models.CharField( + max_length=M_NAME, help_text="The environmental property being measured") + owner = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_owner") + value = models.CharField(help_text="The value of the environmental property", max_length=50) + units = models.CharField( + max_length=50, help_text="The units in which the environmental property is measured", blank=True, null=True) + description = models.CharField(max_length=D_NAME, blank=True, null=True, + help_text="A description of the environmental property") + + def __unicode__(self): + return u'%s (%.4f %s)' % (self.name, self.value, self.units) + +# trait + + +class Trait(models.Model): + name = models.CharField(max_length=M_NAME, help_text="The name of the measured trait") + owner = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_owner", + help_text="The identifier of the uploader") + value = models.CharField(help_text="The value of the trait", max_length=50) + units = models.CharField( + max_length=50, help_text="Units in which the trait was measured", blank=True, null=True) + description = models.CharField(max_length=D_NAME, blank=True, + null=True, help_text="A longer description of the trait") + latitude = models.CharField(max_length=20, blank=True, null=True, help_text="Latitude") + longitude = models.CharField(max_length=20, blank=True, null=True, help_text="Longitude") + environment = models.ManyToManyField( + Environment, blank=True, null=True, help_text="Local environment") + papers = models.ManyToManyField(Ref, related_name='trait_papers', blank=True, null=True) + data = models.ManyToManyField(Ref, related_name='trait_data', blank=True, null=True) + date = models.DateField(blank=True, null=True, + help_text="The time at which the traits were measured") + + def __unicode__(self): + return u'%s (%.4f %s)' % (self.name, self.value, self.units) # taxa + + class Taxa(models.Model): - TAXO_CHOICE = ( - ('confirmed', 'Confirmed'), - ('trophic species', 'Trophic species'), - ('morphospecies', 'Morphospecies'), - ('nomen dubium', 'nomen dubium'), - ('nomen oblitum', 'nomen oblitum'), - ('nomen nudum', 'nomen nudum'), - ('nomen novum', 'nomen novum'), - ('nomen conservandum', 'nomen conservandum'), - ('species inquirenda', 'species inquirenda'), - ) - name = models.CharField(max_length=M_NAME, unique=True, help_text = "The scientific name of the taxa") - owner = models.ForeignKey(User, related_name ="%(app_label)s_%(class)s_owner") - vernacular = models.CharField(max_length=M_NAME, help_text = "The vernacular name of the taxa, in English", blank=True, null=True) - description = models.CharField(max_length=D_NAME,blank=True,null=True, help_text = "A short description of the taxa") - traits = models.ManyToManyField(Trait,blank=True,null=True) - ncbi = models.IntegerField(blank=True,null=True, unique=True, help_text = "The NCBI Taxonomy identifier of the taxa") - gbif = models.IntegerField(blank=True,null=True, unique=True, help_text = "The GBIF identifier of the taxa") - bold = models.IntegerField(blank=True,null=True, unique=True, help_text = "The BOLD identifier of the taxa") - itis = models.IntegerField(blank=True,null=True, unique=True, help_text = "The ITIS identifier of the taxa") - eol = models.IntegerField(blank=True,null=True, unique=True, help_text = "The EOL identifier of the taxa") - status = models.CharField(max_length=50, choices = TAXO_CHOICE, default = 'confirmed', help_text = "The taxonomic status of the taxa.") - def __unicode__(self): - na = self.name - if self.vernacular > 0: - na += " ("+self.vernacular+")" - return na + TAXO_CHOICE = ( + ('confirmed', 'Confirmed'), + ('trophic species', 'Trophic species'), + ('morphospecies', 'Morphospecies'), + ('nomen dubium', 'nomen dubium'), + ('nomen oblitum', 'nomen oblitum'), + ('nomen nudum', 'nomen nudum'), + ('nomen novum', 'nomen novum'), + ('nomen conservandum', 'nomen conservandum'), + ('species inquirenda', 'species inquirenda'), + ) + name = models.CharField(max_length=M_NAME, unique=True, + help_text="The scientific name of the taxa") + owner = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_owner") + vernacular = models.CharField( + max_length=M_NAME, help_text="The vernacular name of the taxa, in English", blank=True, null=True) + description = models.CharField(max_length=D_NAME, blank=True, + null=True, help_text="A short description of the taxa") + traits = models.ManyToManyField(Trait, blank=True, null=True) + ncbi = models.IntegerField(blank=True, null=True, unique=True, + help_text="The NCBI Taxonomy identifier of the taxa") + tsn = models.IntegerField(blank=True, null=True, unique=True, help_text="The TSN identifier") + gbif = models.IntegerField(blank=True, null=True, unique=True, + help_text="The GBIF identifier of the taxa") + bold = models.IntegerField(blank=True, null=True, unique=True, + help_text="The BOLD identifier of the taxa") + eol = models.IntegerField(blank=True, null=True, unique=True, + help_text="The EOL identifier of the taxa") + status = models.CharField(max_length=50, choices=TAXO_CHOICE, + default='confirmed', help_text="The taxonomic status of the taxa.") + + def __unicode__(self): + na = self.name + if self.vernacular > 0: + na += " (" + self.vernacular + ")" + return na # item + + class Item(models.Model): - owner = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_owner", help_text = "The identifier of the uploader") - taxa = models.ForeignKey(Taxa) - level = models.CharField(max_length=50, choices = (('individual', 'Individual'), ('population', 'Population'),), help_text = "Whether the item is a single individual, or a population") - name = models.CharField(max_length=M_NAME, help_text = "A name for the item, useful to identify it later") - traits = models.ManyToManyField(Trait,blank=True,null=True) - size = models.FloatField(blank=True,null=True, help_text = "If the item is a population, the number of individuals or biomass") - units = models.CharField(max_length=50,blank=True,null=True, help_text = "Units in which the population size is measured") - description = models.CharField(max_length=D_NAME,blank=True,null=True, help_text = "A short description of the population") - def __unicode__(self): - return u'%s (%s) - belongs to %s' % (self.name, self.level, self.taxa) + owner = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_owner", + help_text="The identifier of the uploader") + taxa = models.ForeignKey(Taxa) + level = models.CharField(max_length=50, choices=(('individual', 'Individual'), ( + 'population', 'Population'),), help_text="Whether the item is a single individual, or a population") + name = models.CharField( + max_length=M_NAME, help_text="A name for the item, useful to identify it later") + traits = models.ManyToManyField(Trait, blank=True, null=True) + size = models.FloatField( + blank=True, null=True, help_text="If the item is a population, the number of individuals or biomass") + units = models.CharField(max_length=50, blank=True, null=True, + help_text="Units in which the population size is measured") + description = models.CharField(max_length=D_NAME, blank=True, + null=True, help_text="A short description of the population") -# reference -class Ref(models.Model): - doi = models.CharField(max_length=200, blank=True, null=True, help_text = "DOI of the reference object") - owner = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_owner") - jstor = models.CharField(max_length=200, blank=True, null=True, help_text = "JSTOR identifier of the reference object") - pmid = models.CharField(max_length=200, blank=True, null=True, help_text = "PubMed ID of the reference object") - url = models.CharField(max_length=200, blank=True, null=True, help_text = "URL of the reference object") + def __unicode__(self): + return u'%s (%s) - belongs to %s' % (self.name, self.level, self.taxa) # interaction + + class Interaction(models.Model): - STAGE_CHOICES = ( - ('seed', 'Seed'), - ('juvenile', 'Juvenile'), - ('adult', 'Adult'), - ('dead', 'Dead'), - ('larva', 'Larva'), - ('all', 'All'), - ) - TYPE_CHOICES = ( - ('predation', 'Predation'), - ('herbivory', 'Herbivory'), - ('ectoparasitism', 'Ectoparasitism'), - ('endoparasitism', 'Endoparasitism'), - ('intra-cellular parasitism', 'Intra-Cellular parasitism'), - ('parasitoidism', 'Parasitoidism'), - ('mycoheterotrophy', 'Mycoheterotrophy'), - ('antixenosis', 'Anitxenosis'), - ('teletoxy', 'Teletoxy'), - ('amensalism', 'Amensalism'), - ('antibiosis', 'Antibiosis'), - ('allelopathy', 'Allelopathy'), - ('competition', 'Competition'), - ('facilitation', 'Facilitation'), - ('refuge creation', 'Refuge creation'), - ('inquilinism', 'Inquilinism'), - ('phoresis', 'Phoresis'), - ('epibiosis', 'Epibiosis'), - ('pollination', 'Pollination'), - ('mutualistic symbiosis', 'Mutualistic symbiosis'), - ('zoochory', 'Zoochory'), - ('mutual protection', 'Mutual protection'), - ) - OBSERVATION_CHOICES = ( - ('unspecified', 'Unspecified'), - ('observation', 'Observation'), - ('litterature', 'Litterature'), - ('absence', 'Confirmed absence'), - ('inferred', 'Inferred'), - ) - public = models.BooleanField(default=True, blank=True, help_text="Whether the interaction can be viewed by all users") - link_type = models.CharField(max_length=50, choices = TYPE_CHOICES, help_text ="The type of interaction") - obs_type = models.CharField(max_length=50, choices = OBSERVATION_CHOICES, help_text ="How the interaction was observed") - owner = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_owner") - taxa_from = models.ForeignKey(Taxa, related_name='taxa_from') - taxa_to = models.ForeignKey(Taxa, related_name='taxa_to') - item_from = models.ForeignKey(Item, related_name='item_from', blank=True, null=True) - item_to = models.ForeignKey(Item, related_name='item_to', blank=True, null=True) - stage_from = models.CharField(max_length=50, choices = STAGE_CHOICES, default = 'all', help_text = "The stage of the establishing, to be selected in the list of allowed values") - stage_to = models.CharField(max_length=50, choices = STAGE_CHOICES, default = 'all', help_text = "The stage of the receiving entity, to be selected in the list of allowed values") - strength_f = models.FloatField(blank=True,null=True, help_text = "The strength of the interaction for the item ESTABLISHING the interaction") - strength_t = models.FloatField(blank=True,null=True, help_text = "The strength of the interaction for the item RECEVING the interaction") - units_f = models.CharField(max_length=50,blank=True,null=True, help_text = "Units in which the interaction strength is measured") - units_t = models.CharField(max_length=50,blank=True,null=True, help_text = "Units in which the interaction strength is measured") - description = models.CharField(max_length=D_NAME,blank=True,null=True, help_text = "A description of the interaction") - latitude = models.CharField(max_length=20,blank=True,null=True, help_text = "Latitude") - longitude = models.CharField(max_length=20,blank=True,null=True, help_text = "Longitude") - environment = models.ManyToManyField(Environment,blank=True,null=True, help_text = "Local environment") - papers = models.ManyToManyField(Ref, related_name='int_papers',blank=True,null=True) - data = models.ManyToManyField(Ref, related_name='int_data',blank=True,null=True) - date = models.DateField(blank=True, null=True, help_text="The time at which the interaction was sampled") - def __unicode__(self): - From = self.taxa_from - To = self.taxa_to - if self.item_from : - From = self.item_from - if self.item_to: - To = self.item_to - return u'%s (%s) of %s by %s'% (self.link_type, self.obs_type, To, From) + STAGE_CHOICES = ( + ('seed', 'Seed'), + ('juvenile', 'Juvenile'), + ('adult', 'Adult'), + ('dead', 'Dead'), + ('larva', 'Larva'), + ('all', 'All'), + ) + TYPE_CHOICES = ( + ('predation', 'Predation'), + ('herbivory', 'Herbivory'), + ('ectoparasitism', 'Ectoparasitism'), + ('endoparasitism', 'Endoparasitism'), + ('intra-cellular parasitism', 'Intra-Cellular parasitism'), + ('parasitoidism', 'Parasitoidism'), + ('idiobiontism', 'Idiobiontism'), + ('koinobiontism', 'Koinobiontism'), + ('ectoparasitoidism', 'Ectoparasitoidism'), + ('endoparasitoidism', 'Endoparasitoidism'), + ('mycoheterotrophy', 'Mycoheterotrophy'), + ('antixenosis', 'Anitxenosis'), + ('teletoxy', 'Teletoxy'), + ('amensalism', 'Amensalism'), + ('antibiosis', 'Antibiosis'), + ('allelopathy', 'Allelopathy'), + ('competition', 'Competition'), + ('facilitation', 'Facilitation'), + ('refuge creation', 'Refuge creation'), + ('inquilinism', 'Inquilinism'), + ('phoresis', 'Phoresis'), + ('epibiosis', 'Epibiosis'), + ('pollination', 'Pollination'), + ('mutualistic symbiosis', 'Mutualistic symbiosis'), + ('zoochory', 'Zoochory'), + ('mutual protection', 'Mutual protection'), + ) + SEX_CHOICES = ( + ('all', 'All'), + ('male', 'Male'), + ('female', 'Female'), + ('unknown', 'Unknown'), + ) + OBSERVATION_CHOICES = ( + ('unspecified', 'Unspecified'), + ('observation', 'Observation'), + ('litterature', 'Litterature'), + ('absence', 'Confirmed absence'), + ('inferred', 'Inferred'), + ) + public = models.BooleanField(default=True, blank=True, + help_text="Whether the interaction can be viewed by all users") + link_type = models.CharField(max_length=50, choices=TYPE_CHOICES, + help_text="The type of interaction") + obs_type = models.CharField(max_length=50, choices=OBSERVATION_CHOICES, + help_text="How the interaction was observed") + owner = models.ForeignKey( + User, related_name="%(app_label)s_%(class)s_owner", help_text="ID of the user") + taxa_from = models.ForeignKey(Taxa, related_name='taxa_from') + taxa_to = models.ForeignKey(Taxa, related_name='taxa_to') + item_from = models.ForeignKey(Item, related_name='item_from', blank=True, null=True) + item_to = models.ForeignKey(Item, related_name='item_to', blank=True, null=True) + stage_from = models.CharField(max_length=50, choices=STAGE_CHOICES, default='all', + help_text="The stage of the establishing entity, to be selected in the list of allowed values") + stage_to = models.CharField(max_length=50, choices=STAGE_CHOICES, default='all', + help_text="The stage of the receiving entity, to be selected in the list of allowed values") + sex_from = models.CharField(max_length=50, choices=SEX_CHOICES, default='all', + help_text="The sex of the establishing, to be selected in the list of allowed values") + sex_to = models.CharField(max_length=50, choices=SEX_CHOICES, default='all', + help_text="The sex of the receiving entity, to be selected in the list of allowed values") + strength_f = models.FloatField( + blank=True, null=True, help_text="The strength of the interaction for the item ESTABLISHING the interaction") + strength_t = models.FloatField( + blank=True, null=True, help_text="The strength of the interaction for the item RECEVING the interaction") + units_f = models.CharField(max_length=50, blank=True, null=True, + help_text="Units in which the interaction strength is measured") + units_t = models.CharField(max_length=50, blank=True, null=True, + help_text="Units in which the interaction strength is measured") + description = models.CharField(max_length=D_NAME, blank=True, + null=True, help_text="A description of the interaction") + latitude = models.CharField(max_length=20, blank=True, null=True, help_text="Latitude") + longitude = models.CharField(max_length=20, blank=True, null=True, help_text="Longitude") + environment = models.ManyToManyField( + Environment, blank=True, null=True, help_text="Local environment") + papers = models.ManyToManyField(Ref, related_name='int_papers', blank=True, null=True) + data = models.ManyToManyField(Ref, related_name='int_data', blank=True, null=True) + date = models.DateField(blank=True, null=True, + help_text="The time at which the interaction was sampled") + + def __unicode__(self): + From = self.taxa_from + To = self.taxa_to + if self.item_from: + From = self.item_from + if self.item_to: + To = self.item_to + return u'%s (%s) of %s by %s' % (self.link_type, self.obs_type, To, From) # network class Network(models.Model): - public = models.BooleanField(default=True, blank=True, help_text="Whether the network can be viewed by all users") - owner = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_owner") - name = models.CharField(max_length=M_NAME, help_text = "The name of the network") - description = models.CharField(max_length=D_NAME,blank=True,null=True, help_text = "A short description of the network") - interactions = models.ManyToManyField(Interaction) - latitude = models.CharField(max_length=20,blank=True,null=True, help_text = "Latitude") - longitude = models.CharField(max_length=20,blank=True,null=True, help_text = "Longitude") - environment = models.ManyToManyField(Environment,blank=True,null=True) - papers = models.ManyToManyField(Ref, related_name='net_papers',blank=True,null=True) - data = models.ManyToManyField(Ref, related_name='net_data',blank=True,null=True) - date = models.DateField(blank=True, null=True, help_text="The time at which the network was sampled") - def __unicode__(self): - return self.name + public = models.BooleanField(default=True, blank=True, + help_text="Whether the network can be viewed by all users") + owner = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_owner") + name = models.CharField(max_length=M_NAME, help_text="The name of the network") + description = models.CharField(max_length=D_NAME, blank=True, + null=True, help_text="A short description of the network") + interactions = models.ManyToManyField(Interaction) + latitude = models.CharField(max_length=20, blank=True, null=True, help_text="Latitude") + longitude = models.CharField(max_length=20, blank=True, null=True, help_text="Longitude") + environment = models.ManyToManyField(Environment, blank=True, null=True) + papers = models.ManyToManyField(Ref, related_name='net_papers', blank=True, null=True) + data = models.ManyToManyField(Ref, related_name='net_data', blank=True, null=True) + date = models.DateField(blank=True, null=True, + help_text="The time at which the network was sampled") + + def __unicode__(self): + return self.name # dataset + + class Dataset(models.Model): - public = models.BooleanField(default=True, blank=True, help_text="Whether the dataset can be viewed by all users") - owner = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_owner") - name = models.CharField(max_length=M_NAME, help_text = "The name of the dataset") - description = models.CharField(max_length=D_NAME,blank=True,null=True, help_text = "A short description of the dataset") - papers = models.ManyToManyField(Ref, related_name='papers',blank=True,null=True) - data = models.ManyToManyField(Ref, related_name='data',blank=True,null=True) - networks = models.ManyToManyField(Network,blank=True,null=True) - environment = models.ManyToManyField(Environment,blank=True,null=True) - def __unicode__(self): - return self.name + public = models.BooleanField(default=True, blank=True, + help_text="Whether the dataset can be viewed by all users") + owner = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_owner") + name = models.CharField(max_length=M_NAME, help_text="The name of the dataset") + description = models.CharField(max_length=D_NAME, blank=True, + null=True, help_text="A short description of the dataset") + papers = models.ManyToManyField(Ref, related_name='papers', blank=True, null=True) + data = models.ManyToManyField(Ref, related_name='data', blank=True, null=True) + networks = models.ManyToManyField(Network, blank=True, null=True, related_name='network') + environment = models.ManyToManyField(Environment, blank=True, null=True) + + def __unicode__(self): + return self.name diff --git a/resources.py b/resources.py index 494e2f0..aba9101 100644 --- a/resources.py +++ b/resources.py @@ -8,6 +8,7 @@ from tastypie.exceptions import BadRequest, Unauthorized from django.db.models import Q + class MangalAuthorization(Authorization): def is_object_readable(self, ob, bundle): @@ -20,19 +21,19 @@ def is_object_readable(self, ob, bundle): (ii) the owner (iv) the public if public is True """ - try : + try: ob.public ob.owner - except AttributeError : + except AttributeError: return True - except : + except: # I don't know why it is needed, but the schemas are NOT # returned without that... return True - else : + else: if bundle.request.user.is_staff or ob.owner == bundle.request.user or ob.public: return True - else : + else: return False def read_list(self, object_list, bundle): @@ -58,10 +59,11 @@ def update_detail(self, object_list, bundle): return self.is_object_readable(bundle.obj, bundle) def delete_list(self, object_list, bundle): - raise Unauthorized("Deleting objects is not permitted") + return [ob for ob in object_list if self.is_object_readable(ob, bundle)] def delete_detail(self, object_list, bundle): - raise Unauthorized("Deleting objects is not permitted") + return True + class UserAuthorization(DjangoAuthorization): @@ -89,7 +91,9 @@ def delete_list(self, object_list, bundle): def delete_detail(self, object_list, bundle): raise Unauthorized("Deleting users is not permitted") + class UserResource(ModelResource): + class Meta: object_class = User #authentication = MultiAuthentication(ApiKeyAuthentication(), BasicAuthentication(), Authentication()) @@ -97,12 +101,14 @@ class Meta: queryset = User.objects.all() always_return_data = True resource_name = 'user' - excludes = ['date_joined', 'is_active', 'is_staff', 'is_superuser', 'last_login', 'password'] + excludes = ['date_joined', 'is_active', 'is_staff', + 'is_superuser', 'last_login', 'password'] filtering = {'username': ALL, 'email': ALL, 'last_name': ALL, 'first_name': ALL, } allowed_methods = ['get'] + def obj_create(self, bundle, request=None, **kwargs): username, password = bundle.data['username'], bundle.data['password'] - try : + try: bundle.obj = User.objects.create_user(username, '', password) except IntegrityError: raise BadRequest('That username already exists') @@ -110,136 +116,169 @@ def obj_create(self, bundle, request=None, **kwargs): class RefResource(ModelResource): - owner = fields.ForeignKey(UserResource, 'owner', full=True, help_text = "URI of the profile of the owner. When objects are uploaded from the R package, this field is set automatically.") + owner = fields.ForeignKey(UserResource, 'owner', full=True, + help_text="URI of the profile of the owner. When objects are uploaded from the R package, this field is set automatically.") + def dehydrate(self, bundle): bundle.data['id'] = str(bundle.data['id']) bundle.data['owner'] = str(bundle.data['owner'].data['username']) return bundle + class Meta: queryset = Ref.objects.all() - authentication = MultiAuthentication(ApiKeyAuthentication(), BasicAuthentication(), Authentication()) + authentication = MultiAuthentication( + ApiKeyAuthentication(), BasicAuthentication(), Authentication()) authorization = MangalAuthorization() always_return_data = True resource_name = 'reference' - allowed_methods = ['get','post','patch'] + allowed_methods = ['get', 'post', 'patch', 'delete'] class TraitResource(ModelResource): owner = fields.ForeignKey(UserResource, 'owner', full=True) + def dehydrate(self, bundle): bundle.data['id'] = str(bundle.data['id']) bundle.data['owner'] = str(bundle.data['owner'].data['username']) return bundle + class Meta: queryset = Trait.objects.all() - authentication = MultiAuthentication(ApiKeyAuthentication(), BasicAuthentication(), Authentication()) + authentication = MultiAuthentication( + ApiKeyAuthentication(), BasicAuthentication(), Authentication()) authorization = MangalAuthorization() always_return_data = True resource_name = 'trait' - allowed_methods = ['get','post','patch'] + allowed_methods = ['get', 'post', 'patch', 'delete'] class EnvironmentResource(ModelResource): owner = fields.ForeignKey(UserResource, 'owner', full=True) + def dehydrate(self, bundle): bundle.data['id'] = str(bundle.data['id']) bundle.data['owner'] = str(bundle.data['owner'].data['username']) return bundle + class Meta: queryset = Environment.objects.all() - authentication = MultiAuthentication(ApiKeyAuthentication(), BasicAuthentication(), Authentication()) + authentication = MultiAuthentication( + ApiKeyAuthentication(), BasicAuthentication(), Authentication()) authorization = MangalAuthorization() always_return_data = True resource_name = 'environment' - allowed_methods = ['get','post','patch'] + allowed_methods = ['get', 'post', 'patch', 'delete'] class TaxaResource(ModelResource): - owner = fields.ForeignKey(UserResource, 'owner', full=True, help_text = "URI of the taxa owner. When submitting from the R package, this field is populated automatically.") - traits = fields.ManyToManyField(TraitResource, 'traits', full=True, blank = True, null = True, help_text = "A list of traits values indentifiers (or URIs) that were measured on this taxa. Good for taxa-level traits.") + owner = fields.ForeignKey(UserResource, 'owner', full=True, + help_text="URI of the taxa owner. When submitting from the R package, this field is populated automatically.") + traits = fields.ManyToManyField(TraitResource, 'traits', full=True, blank=True, null=True, + help_text="A list of traits values indentifiers (or URIs) that were measured on this taxa. Good for taxa-level traits.") + def dehydrate(self, bundle): bundle.data['id'] = str(bundle.data['id']) bundle.data['owner'] = str(bundle.data['owner'].data['username']) bundle.data['traits'] = [str(tr.data['id']) for tr in bundle.data['traits']] return bundle + def build_schema(self): base_schema = super(TaxaResource, self).build_schema() for f in self._meta.object_class._meta.fields: if f.name in base_schema['fields'] and f.choices: base_schema['fields'][f.name].update({ 'choices': [cho[0] for cho in f.choices], - }) + }) return base_schema + class Meta: queryset = Taxa.objects.all() - authentication = MultiAuthentication(ApiKeyAuthentication(), BasicAuthentication(), Authentication()) + authentication = MultiAuthentication( + ApiKeyAuthentication(), BasicAuthentication(), Authentication()) authorization = MangalAuthorization() always_return_data = True resource_name = 'taxa' filtering = { - 'name': ALL, - 'vernacular': ALL, - 'gbif': ALL, - 'eol': ALL, - 'itis': ALL, - 'ncbi': ALL, - 'bold': ALL, - 'owner': ALL, - 'filter': ALL, - } - allowed_methods = ['get','post','patch'] + 'name': ALL, + 'vernacular': ALL, + 'gbif': ALL, + 'eol': ALL, + 'itis': ALL, + 'ncbi': ALL, + 'bold': ALL, + 'owner': ALL, + 'filter': ALL, + } + allowed_methods = ['get', 'post', 'patch', 'delete'] + class ItemResource(ModelResource): - taxa = fields.ForeignKey(TaxaResource, 'taxa', full=True, help_text = "The identifier (or URI) of the taxa object to which the item belongs.") + taxa = fields.ForeignKey(TaxaResource, 'taxa', full=True, + help_text="The identifier (or URI) of the taxa object to which the item belongs.") owner = fields.ForeignKey(UserResource, 'owner', full=True) - traits = fields.ManyToManyField(TraitResource, 'traits', full=True, blank = True, null = True, help_text = "A list of traits values indentifiers (or URIs) that were measured on this item.") + traits = fields.ManyToManyField(TraitResource, 'traits', full=True, blank=True, null=True, + help_text="A list of traits values indentifiers (or URIs) that were measured on this item.") + def dehydrate(self, bundle): bundle.data['taxa'] = str(bundle.obj.taxa_id) bundle.data['id'] = str(bundle.data['id']) bundle.data['owner'] = str(bundle.data['owner'].data['username']) bundle.data['traits'] = [str(tr.data['id']) for tr in bundle.data['traits']] return bundle + def build_schema(self): base_schema = super(ItemResource, self).build_schema() for f in self._meta.object_class._meta.fields: if f.name in base_schema['fields'] and f.choices: base_schema['fields'][f.name].update({ 'choices': [cho[0] for cho in f.choices], - }) + }) return base_schema + class Meta: - authentication = MultiAuthentication(ApiKeyAuthentication(), BasicAuthentication(), Authentication()) + authentication = MultiAuthentication( + ApiKeyAuthentication(), BasicAuthentication(), Authentication()) authorization = MangalAuthorization() always_return_data = True queryset = Item.objects.all() filtering = { - 'taxa': ALL_WITH_RELATIONS, - 'description': ALL, - 'owner': ALL_WITH_RELATIONS, - 'stage': ALL, - 'level': ALL, - } + 'taxa': ALL_WITH_RELATIONS, + 'description': ALL, + 'owner': ALL_WITH_RELATIONS, + 'stage': ALL, + 'level': ALL, + } resource_name = 'item' - allowed_methods = ['get','post','patch'] + allowed_methods = ['get', 'post', 'patch', 'delete'] class InteractionResource(ModelResource): - owner = fields.ForeignKey(UserResource, 'owner', full=True, help_text = "Who uploaded the data. URI of the data owner.") - environment = fields.ManyToManyField(EnvironmentResource, 'environment', full=True, help_text = "List of identifiers (or URIs) of the environments associated to the interaction.", null=True, blank=True) - taxa_from = fields.ForeignKey(TaxaResource, 'taxa_from', full=True, help_text = "Identifier (or URI) of the taxa establishing the interaction.") - taxa_to = fields.ForeignKey(TaxaResource, 'taxa_to', full=True, help_text = "Identifier (or URI) of the taxa receiving the interaction.") - item_from = fields.ForeignKey(ItemResource, 'item_from', full=True, null = True, blank = True, help_text = "Identifier (or URI) of the item establishing the interaction.") - item_to = fields.ForeignKey(ItemResource, 'item_to', full=True, null = True, blank = True, help_text = "Identifier (or URI) of the item receiving the interaction.") - data = fields.ManyToManyField(RefResource, 'data', full=True, null=True, blank=True, help_text = "List of identifiers (or URIs) of the references describing the data.") - papers = fields.ManyToManyField(RefResource, 'papers', full=True, null=True, blank=True, help_text = "List of identifiers (or URIs) of the references to the papers associated with the dataset.") + owner = fields.ForeignKey(UserResource, 'owner', full=True, + help_text="Who uploaded the data. URI of the data owner.", null=True, blank=True) + environment = fields.ManyToManyField(EnvironmentResource, 'environment', full=True, + help_text="List of identifiers (or URIs) of the environments associated to the interaction.", null=True, blank=True) + taxa_from = fields.ForeignKey(TaxaResource, 'taxa_from', full=True, + help_text="Identifier (or URI) of the taxa establishing the interaction.") + taxa_to = fields.ForeignKey(TaxaResource, 'taxa_to', full=True, + help_text="Identifier (or URI) of the taxa receiving the interaction.") + item_from = fields.ForeignKey(ItemResource, 'item_from', full=True, null=True, blank=True, + help_text="Identifier (or URI) of the item establishing the interaction.") + item_to = fields.ForeignKey(ItemResource, 'item_to', full=True, null=True, blank=True, + help_text="Identifier (or URI) of the item receiving the interaction.") + data = fields.ManyToManyField(RefResource, 'data', full=True, null=True, blank=True, + help_text="List of identifiers (or URIs) of the references describing the data.") + papers = fields.ManyToManyField(RefResource, 'papers', full=True, null=True, blank=True, + help_text="List of identifiers (or URIs) of the references to the papers associated with the dataset.") + def build_schema(self): base_schema = super(InteractionResource, self).build_schema() for f in self._meta.object_class._meta.fields: if f.name in base_schema['fields'] and f.choices: base_schema['fields'][f.name].update({ 'choices': [cho[0] for cho in f.choices], - }) + }) return base_schema + def dehydrate(self, bundle): bundle.data['data'] = [str(ref.data['id']) for ref in bundle.data['data']] bundle.data['papers'] = [str(ref.data['id']) for ref in bundle.data['papers']] @@ -253,32 +292,39 @@ def dehydrate(self, bundle): if bundle.data['item_to']: bundle.data['item_to'] = str(bundle.data['item_to'].obj.id) return bundle + class Meta: queryset = Interaction.objects.all() - authentication = MultiAuthentication(ApiKeyAuthentication(), BasicAuthentication(), Authentication()) + authentication = MultiAuthentication( + ApiKeyAuthentication(), BasicAuthentication(), Authentication()) authorization = MangalAuthorization() always_return_data = True filtering = { - 'owner': ALL_WITH_RELATIONS, - 'taxa_from': ALL_WITH_RELATIONS, - 'taxa_to': ALL_WITH_RELATIONS, - 'item_to': ALL_WITH_RELATIONS, - 'item_from': ALL_WITH_RELATIONS, - 'link_type': ALL_WITH_RELATIONS, - 'obs_type': ALL_WITH_RELATIONS, - 'nature': ALL_WITH_RELATIONS, - 'description': ALL, - } + 'owner': ALL_WITH_RELATIONS, + 'taxa_from': ALL_WITH_RELATIONS, + 'taxa_to': ALL_WITH_RELATIONS, + 'item_to': ALL_WITH_RELATIONS, + 'item_from': ALL_WITH_RELATIONS, + 'link_type': ALL_WITH_RELATIONS, + 'obs_type': ALL_WITH_RELATIONS, + 'nature': ALL_WITH_RELATIONS, + 'description': ALL, + } resource_name = 'interaction' - allowed_methods = ['get','post','patch'] + allowed_methods = ['get', 'post', 'patch', 'delete'] class NetworkResource(ModelResource): - interactions = fields.ManyToManyField(InteractionResource, 'interactions', full=True, help_text = "List of identifiers (or URIs) of the interactions in the network.") - environment = fields.ManyToManyField(EnvironmentResource, 'environment', full=True, null=True, blank=True, help_text = "List of identifiers (or URIs) of environmental measurements associated to the network.") - data = fields.ManyToManyField(RefResource, 'data', full=True, null=True, blank=True, help_text = "List of identifiers (or URIs) of the references describing the data.") - papers = fields.ManyToManyField(RefResource, 'papers', full=True, null=True, blank=True, help_text = "List of identifiers (or URIs) of the references to the papers associated with the dataset.") + interactions = fields.ManyToManyField(InteractionResource, 'interactions', full=True, + help_text="List of identifiers (or URIs) of the interactions in the network.") + environment = fields.ManyToManyField(EnvironmentResource, 'environment', full=True, null=True, blank=True, + help_text="List of identifiers (or URIs) of environmental measurements associated to the network.") + data = fields.ManyToManyField(RefResource, 'data', full=True, null=True, blank=True, + help_text="List of identifiers (or URIs) of the references describing the data.") + papers = fields.ManyToManyField(RefResource, 'papers', full=True, null=True, blank=True, + help_text="List of identifiers (or URIs) of the references to the papers associated with the dataset.") owner = fields.ForeignKey(UserResource, 'owner', full=True) + def dehydrate(self, bundle): bundle.data['id'] = str(bundle.data['id']) bundle.data['interactions'] = [str(inte.data['id']) for inte in bundle.data['interactions']] @@ -287,30 +333,37 @@ def dehydrate(self, bundle): bundle.data['data'] = [str(ref.data['id']) for ref in bundle.data['data']] bundle.data['papers'] = [str(ref.data['id']) for ref in bundle.data['papers']] return bundle + class Meta: queryset = Network.objects.all() - authentication = MultiAuthentication(ApiKeyAuthentication(), BasicAuthentication(), Authentication()) + authentication = MultiAuthentication( + ApiKeyAuthentication(), BasicAuthentication(), Authentication()) authorization = MangalAuthorization() always_return_data = True resource_name = 'network' filtering = { - 'owner': ALL_WITH_RELATIONS, - 'interactions': ALL_WITH_RELATIONS, - 'metaweb': ALL_WITH_RELATIONS, - 'description': ALL, - 'name': ALL, - 'latitude': ALL_WITH_RELATIONS, - 'longitude': ALL_WITH_RELATIONS, - } - allowed_methods = ['get','post','patch'] + 'owner': ALL_WITH_RELATIONS, + 'interactions': ALL_WITH_RELATIONS, + 'metaweb': ALL_WITH_RELATIONS, + 'description': ALL, + 'name': ALL, + 'latitude': ALL_WITH_RELATIONS, + 'longitude': ALL_WITH_RELATIONS, + } + allowed_methods = ['get', 'post', 'patch', 'delete'] class DatasetResource(ModelResource): - networks = fields.ManyToManyField(NetworkResource, 'networks', full=True, help_text = "List of identifiers (or URIs) of the networks in the dataset.") - environment = fields.ManyToManyField(EnvironmentResource, 'environment', full=True, help_text = "List of identifiers (or URIs) of the environments associated to the dataset.", null=True, blank=True) - data = fields.ManyToManyField(RefResource, 'data', full=True, null=True, blank=True, help_text = "List of identifiers (or URIs) of the references describing the data.") - papers = fields.ManyToManyField(RefResource, 'papers', full=True, null=True, blank=True, help_text = "List of identifiers (or URIs) of the references to the papers associated with the dataset.") + networks = fields.ManyToManyField(NetworkResource, 'networks', full=True, + help_text="List of identifiers (or URIs) of the networks in the dataset.") + environment = fields.ManyToManyField(EnvironmentResource, 'environment', full=True, + help_text="List of identifiers (or URIs) of the environments associated to the dataset.", null=True, blank=True) + data = fields.ManyToManyField(RefResource, 'data', full=True, null=True, blank=True, + help_text="List of identifiers (or URIs) of the references describing the data.") + papers = fields.ManyToManyField(RefResource, 'papers', full=True, null=True, blank=True, + help_text="List of identifiers (or URIs) of the references to the papers associated with the dataset.") owner = fields.ForeignKey(UserResource, 'owner', full=True) + def dehydrate(self, bundle): bundle.data['id'] = str(bundle.data['id']) bundle.data['networks'] = [str(net.data['id']) for net in bundle.data['networks']] @@ -319,17 +372,18 @@ def dehydrate(self, bundle): bundle.data['data'] = [str(ref.data['id']) for ref in bundle.data['data']] bundle.data['owner'] = str(bundle.data['owner'].data['username']) return bundle + class Meta: queryset = Dataset.objects.all() - authentication = MultiAuthentication(ApiKeyAuthentication(), BasicAuthentication(), Authentication()) + authentication = MultiAuthentication( + ApiKeyAuthentication(), BasicAuthentication(), Authentication()) authorization = MangalAuthorization() always_return_data = True resource_name = 'dataset' filtering = { - 'owner': ALL_WITH_RELATIONS, - 'networks': ALL_WITH_RELATIONS, - 'description': ALL, - 'name': ALL, - } - allowed_methods = ['get','post','patch'] - + 'owner': ALL_WITH_RELATIONS, + 'networks': ALL_WITH_RELATIONS, + 'description': ALL, + 'name': ALL, + } + allowed_methods = ['get', 'post', 'patch', 'delete']