Workout With Friends Stay fit with a little motivation

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Properties
lookup_tables.py
Go to the documentation of this file.
1 from __future__ import unicode_literals
2 import transaction
3 from sqlalchemy.orm import backref, relationship
4 from sqlalchemy.orm.exc import NoResultFound
5 from sqlalchemy.schema import Column
6 from sqlalchemy.types import Integer, Unicode
7 from wowf.lib.utils import get_subclasses
8 from wowf.models.meta import Base
9
10
11 ##
12 #
13 # Populate all lookup tables defined in this module.
14 #
15 def populate_lookups():
16 from wowf.models import lookup_tables
17 transaction.begin()
18 for cls in get_subclasses(lookup_tables, lookup_tables.Lookup):
19 cls.populate()
20 transaction.commit()
21
22
23 class Lookup(object):
24
25 lookup_data = ()
26 id = Column(Integer(unsigned=True), primary_key=True)
27 name = Column(Unicode(40), nullable=False, index=True)
28
29 def __init__(self, name):
30 self.name = name
31
32 def __unicode__(self):
33 return self.name
34
35 @classmethod
36 def create(cls, name):
37 return super(Lookup, cls).create(name=name)
38
39 @classmethod
40 def populate(cls):
41 for name in cls.lookup_data:
42 try:
43 cls.get_by_name(name)
44 except NoResultFound:
45 cls.create(name)
46
47 @classmethod
48 def get_by_name(cls, name):
49 return cls.query.filter(cls.name==name).one()
50
51
52 class NotificationType(Lookup, Base):
53
54 __tablename__ = 'notification_types'
55 lookup_data = (
56 'requested_challenge', 'accepted_challenge', 'denied_challenge',
57 'new_buddy', 'uploaded_workout')
58
59
60 class ChallengeType(Lookup, Base):
61
62 __tablename__ = 'challenge_types'
63 lookup_data = (
64 'speed', 'endurance', 'bench_press', 'squat')
65
66
67 class Group(Lookup, Base):
68
69 __tablename__ = 'groups'
70 lookup_data = ('login', 'admin')
71
72 users = relationship(
73 'User', backref=backref('groups', lazy='dynamic'),
74 secondary='users_groups', lazy='dynamic')
75