Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rounds information #5

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions goteoapi/models/reward.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -109,13 +109,17 @@ def __repr__(self):
return '<Reward(%d) %s[%s]: %s>' % (
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):
Expand Down
48 changes: 48 additions & 0 deletions goteoapi/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,6 +54,28 @@ class ProjectCategory(db.Model):
def __repr__(self):
return '<ProjectCategory %s-%s>' % (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 '<ProjectConf %s-%s>' % (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'

Expand Down Expand Up @@ -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(
Expand All @@ -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"""
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions goteoapi/projects/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down