diff --git a/goteoapi/models/reward.py b/goteoapi/models/reward.py index cc20d8e..774768a 100644 --- a/goteoapi/models/reward.py +++ b/goteoapi/models/reward.py @@ -27,7 +27,7 @@ def __repr__(self): @hybrid_method @cacher - def reward_available_units(self, reward_id): + def reward_purchased_units(self, reward_id): from goteoapi.invests.models import Invest """Number of available units of a reward""" @@ -109,13 +109,17 @@ def __repr__(self): return '' % ( self.id, self.project_id, self.type, self.name) + @hybrid_property + def purchased_units(self): + return RewardInvest.reward_purchased_units(self.id) + @hybrid_property def available_units(self): if self.units is None: return 0 if self.units == 0: return 0 - return self.units - RewardInvest.reward_available_units(self.id) + return self.units - RewardInvest.reward_purchased_units(self.id) @hybrid_property def icon_url(self): diff --git a/goteoapi/projects/models.py b/goteoapi/projects/models.py index 7e04369..878fbe7 100644 --- a/goteoapi/projects/models.py +++ b/goteoapi/projects/models.py @@ -5,6 +5,7 @@ from sqlalchemy.ext.hybrid import hybrid_property, hybrid_method from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound from sqlalchemy.orm import aliased, relationship +from datetime import date from ..helpers import image_url, project_url, project_widget_url,as_list from ..helpers import utc_from_local, get_lang @@ -53,6 +54,28 @@ class ProjectCategory(db.Model): def __repr__(self): return '' % (self.project_id, self.category_id) +class ProjectConf(db.Model): + __tablename__ = 'project_conf' + + project_id = db.Column('project', String(50), + db.ForeignKey('project.id'), primary_key=True) + days_round1 = db.Column('days_round1', Integer) + days_round2 = db.Column('days_round2', Integer) + one_round = db.Column('one_round', Integer) + + def __repr__(self): + return '' % (self.project_id, self.days_round1, self.days_round2, self.one_round) + + @hybrid_method + @cacher + def get(self, project_id): + """Get a valid project form id""" + try: + return self.query \ + .filter(self.project_id == project_id).one() + except NoResultFound: + return None + class ProjectImage(db.Model): __tablename__ = 'project_image' @@ -178,6 +201,7 @@ class Project(db.Model): social_commitment_description = db.Column('social_commitment_description', Text) social_commitment_id = db.Column('social_commitment', Integer, db.ForeignKey('social_commitment.id')) SocialCommitment = relationship("SocialCommitment", lazy='joined') # Eager loading for catching + ProjectConf = relationship("ProjectConf", lazy='joined') # Eager loading for catching # Translations = relationship( @@ -193,6 +217,10 @@ def social_commitment(self): from ..social_commitments.models import SocialCommitment return SocialCommitment.get(self.social_commitment_id, lang=self.lang) + @hybrid_property + def project_conf(self): + return ProjectConf.get(self.id) + @hybrid_method def status_number(self, status): """Returns apropiate numbers string statuses""" @@ -214,6 +242,26 @@ def node(self): def owner(self): return self.user_id + @hybrid_property + def rounds(self): + conf = self.project_conf + one_round = conf.one_round if conf else 0 + days_round1 = conf.days_round1 if conf else 40 + days_round2 = conf.days_round2 if conf else 40 + days_passed = (date.today() - self.published).days + remaining = remaining_round1 = days_round1 - days_passed + remaining_round2 = 0 + if not one_round: + remaining += days_round2 + remaining_round2 = days_round2 if remaining_round1 > 0 else remaining + return { + "round1": days_round1, + "round1-remaining": remaining_round1 if remaining_round1 > 0 else 0, + "round2": 0 if one_round else days_round2, + "round2-remaining": remaining_round2 if remaining_round2 > 0 else 0, + "days-remaining": remaining if remaining > 0 else 0 + } + @hybrid_property def owner_name(self): # Manually get the User object if not exists diff --git a/goteoapi/projects/resources.py b/goteoapi/projects/resources.py index ab6787f..7c6b32d 100644 --- a/goteoapi/projects/resources.py +++ b/goteoapi/projects/resources.py @@ -56,6 +56,7 @@ "amount": fields.Integer, "units": fields.Integer, "available_units": fields.Integer, + "purchased_units": fields.Integer, "icon_url": fields.String, "license": fields.String, "license_description": fields.String, @@ -102,6 +103,13 @@ "name": fields.String, "description": fields.String } +rounds_resource_fields = { + "round1": fields.Integer, + "round2": fields.Integer, + "days-remaining": fields.Integer, + "round1-remaining": fields.Integer, + "round2-remaining": fields.Integer +} categories_translate_resource_fields = { "name": fields.String, "description": fields.String @@ -134,6 +142,7 @@ project_full_resource_fields["date_succeeded"] = DateTime project_full_resource_fields["date_closed"] = DateTime project_full_resource_fields["date_passed"] = DateTime +project_full_resource_fields["rounds"] = fields.Nested(rounds_resource_fields) project_full_resource_fields["location"] = fields.List( fields.Nested(location_resource_fields)) project_full_resource_fields["user"] = fields.Nested(user_resource_fields)