Skip to content
This repository has been archived by the owner on Mar 14, 2019. It is now read-only.

Commit

Permalink
API Update for revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothée Poisot committed Sep 19, 2014
1 parent 3925600 commit 82a6ec8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 54 deletions.
1 change: 0 additions & 1 deletion admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from models import *

admin.site.register(Taxa)
admin.site.register(Population)
admin.site.register(Item)
admin.site.register(Trait)
admin.site.register(Environment)
Expand Down
19 changes: 2 additions & 17 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,18 @@ def __unicode__(self):
na += " ("+self.vernacular+")"
return na

# population
class Population(models.Model):
taxa = models.ForeignKey(Taxa)
name = models.CharField(max_length=M_NAME, help_text = "A name allowing to identify the population")
owner = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_owner", help_text = "The identifier of the uploader")
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, of taxa %s' % (self.name, self.taxa)

# item
class Item(models.Model):
owner = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_owner", help_text = "The identifier of the uploader")
population = models.ForeignKey(Population)
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.population)
return u'%s (%s) - belongs to %s' % (self.name, self.level, self.taxa)

# reference
class Ref(models.Model):
Expand Down Expand Up @@ -140,8 +131,6 @@ class Interaction(models.Model):
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')
pop_from = models.ForeignKey(Population, related_name='pop_from', blank=True, null=True)
pop_to = models.ForeignKey(Population, related_name='pop_to', blank=True, null=True)
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")
Expand All @@ -160,10 +149,6 @@ class Interaction(models.Model):
def __unicode__(self):
From = self.taxa_from
To = self.taxa_to
if self.pop_from :
From = self.pop_from
if self.pop_to:
To = self.pop_to
if self.item_from :
From = self.item_from
if self.item_to:
Expand Down
46 changes: 11 additions & 35 deletions resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,36 +189,12 @@ class Meta:
}
allowed_methods = ['get','post','patch']


class PopulationResource(ModelResource):
owner = fields.ForeignKey(UserResource, 'owner', full=True)
taxa = fields.ForeignKey(TaxaResource, 'taxa', full=True, help_text = "The identifier (or URI) of the taxa object to which the population belongs.")
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'])
return bundle
class Meta:
authentication = MultiAuthentication(ApiKeyAuthentication(), BasicAuthentication(), Authentication())
authorization = MangalAuthorization()
always_return_data = True
queryset = Population.objects.all()
filtering = {
'taxa': ALL_WITH_RELATIONS,
'name': ALL,
'description': ALL,
'owner': ALL_WITH_RELATIONS,
}
resource_name = 'population'
allowed_methods = ['get','post','patch']


class ItemResource(ModelResource):
population = fields.ForeignKey(PopulationResource, 'population', full=True, help_text = "The identifier (or URI) of the population 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.")
def dehydrate(self, bundle):
bundle.data['population'] = str(bundle.obj.population_id)
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']]
Expand All @@ -237,7 +213,7 @@ class Meta:
always_return_data = True
queryset = Item.objects.all()
filtering = {
'population': ALL_WITH_RELATIONS,
'taxa': ALL_WITH_RELATIONS,
'description': ALL,
'owner': ALL_WITH_RELATIONS,
'stage': ALL,
Expand All @@ -252,10 +228,10 @@ class InteractionResource(ModelResource):
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.")
pop_from = fields.ForeignKey(PopulationResource, 'pop_from', full=True, null = True, help_text = "Identifier (or URI) of the pop. establishing the interaction.")
pop_to = fields.ForeignKey(PopulationResource, 'pop_to', full=True, null = True, help_text = "Identifier (or URI) of the pop. 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:
Expand All @@ -265,15 +241,13 @@ def build_schema(self):
})
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']]
bundle.data['id'] = str(bundle.data['id'])
bundle.data['owner'] = str(bundle.data['owner'].data['username'])
bundle.data['taxa_from'] = str(bundle.data['taxa_from'].obj.id)
bundle.data['environment'] = [str(env.data['id']) for env in bundle.data['environment']]
bundle.data['taxa_to'] = str(bundle.data['taxa_to'].obj.id)
if bundle.data['pop_from']:
bundle.data['pop_from'] = str(bundle.data['pop_from'].obj.id)
if bundle.data['pop_to']:
bundle.data['pop_to'] = str(bundle.data['pop_to'].obj.id)
if bundle.data['item_from']:
bundle.data['item_from'] = str(bundle.data['item_from'].obj.id)
if bundle.data['item_to']:
Expand All @@ -288,8 +262,6 @@ class Meta:
'owner': ALL_WITH_RELATIONS,
'taxa_from': ALL_WITH_RELATIONS,
'taxa_to': ALL_WITH_RELATIONS,
'pop_to': ALL_WITH_RELATIONS,
'pop_from': ALL_WITH_RELATIONS,
'item_to': ALL_WITH_RELATIONS,
'item_from': ALL_WITH_RELATIONS,
'link_type': ALL_WITH_RELATIONS,
Expand All @@ -304,12 +276,16 @@ class Meta:
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.")
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']]
bundle.data['environment'] = [str(env.data['id']) for env in bundle.data['environment']]
bundle.data['owner'] = str(bundle.data['owner'].data['username'])
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()
Expand Down
1 change: 0 additions & 1 deletion urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
v1_api = Api(api_name='v1')
v1_api.register(UserResource())
v1_api.register(TaxaResource())
v1_api.register(PopulationResource())
v1_api.register(InteractionResource())
v1_api.register(NetworkResource())
v1_api.register(DatasetResource())
Expand Down

0 comments on commit 82a6ec8

Please sign in to comment.