1 from __future__
import unicode_literals
2 from sqlalchemy.ext.declarative
import declared_attr
3 from sqlalchemy.orm
import backref, relationship
4 from sqlalchemy.schema
import Column, ForeignKey
5 from sqlalchemy.types
import DateTime, Integer, Numeric, SmallInteger
18 __tablename__ =
'challenges'
19 __mapper_args__ = {
'polymorphic_on':
'challenge_type_id',
'with_polymorphic':
'*'}
20 id = Column(Integer(unsigned=
True), primary_key=
True)
21 user_id = Column(Integer(unsigned=
True), ForeignKey(
'users.id'), nullable=
False)
22 challenge_type_id = Column(
23 Integer(unsigned=
True), ForeignKey(
'challenge_types.id', ondelete=
'cascade'),
25 created_at = Column(DateTime, nullable=
False, default=current_timestamp)
27 _creator = relationship(
'User', lazy=
'joined')
28 competitors = relationship(
29 'User', backref=backref(
'challenges', lazy=
'dynamic'),
30 secondary=
'users_challenges', lazy=
'dynamic')
41 pivot = UserChallenge.create(creator, self)
44 creator = property(_get_creator, _set_creator)
52 self.competitors.append(user)
60 self.competitors.remove(user)
67 return isinstance(self, DeviceChallenge)
74 return isinstance(self, WeightChallenge)
81 return isinstance(self, SpeedChallenge)
88 return isinstance(self, EnduranceChallenge)
95 return isinstance(self, BenchPressChallenge)
102 return isinstance(self, SquatChallenge)
109 return self.workouts.count() == 2
117 winning_workout = self.workouts.order_by(Workout.points.desc()).first()
118 return user
is winning_workout.user
129 Integer(unsigned=
True), ForeignKey(
'challenges.id', ondelete=
'cascade'),
145 class WeightChallenge(_Challenge):
147 percentage = Column(Numeric(3, 2), nullable=
False, doc=
'percentage of body weight')
151 return super(WeightChallenge, cls).
create(creator=creator, percentage=percentage)
158 return round(self.
percentage * user.weight, 2)
163 __tablename__ =
'speed_challenges'
164 __mapper_args__ = {
'polymorphic_identity': ChallengeType.lookup_data.index(
'speed') + 1}
165 distance = Column(SmallInteger(unsigned=
True), nullable=
False, doc=
'distance in meters')
169 return 'Sprint %d meters as fast as you can. The person with the fastest time wins.' % (
173 return '%d meter speed challenge' % self.
distance
177 return super(SpeedChallenge, cls).
create(creator=creator, distance=distance)
182 __tablename__ =
'endurance_challenges'
183 __mapper_args__ = {
'polymorphic_identity': ChallengeType.lookup_data.index(
'endurance') + 1}
184 duration = Column(SmallInteger(unsigned=
True), nullable=
False, doc=
'duration in minutes')
188 return 'Run as hard as you can for %d minutes. The person with the highest vitals wins.' % (
192 return '%d minute endurance challenge' % self.
duration
196 return super(EnduranceChallenge, cls).
create(creator=creator, duration=duration)
201 __tablename__ =
'bench_press_challenges'
202 __mapper_args__ = {
'polymorphic_identity': ChallengeType.lookup_data.index(
'bench_press') + 1}
206 return 'Bench press %d%% of your body weight for as many repetitions as you can. The person with the most repetitions wins.' % (
210 return '%d%% bench press challenge' % (self.
percentage * 100)
215 __tablename__ =
'squat_challenges'
216 __mapper_args__ = {
'polymorphic_identity': ChallengeType.lookup_data.index(
'squat') + 1}
220 return 'Squat %d%% of your body weight for as many repetitions as you can. The person with the most repetitions wins.' % (
224 return '%d%% squat challenge' % (self.
percentage * 100)