Workout With Friends
Stay fit with a little motivation
 All Classes Namespaces Files Functions Variables Properties
pivot_tables.py
Go to the documentation of this file.
1 from sqlalchemy.orm import relationship
2 from sqlalchemy.schema import Column, ForeignKey
3 from sqlalchemy.types import Boolean, Integer
4 from wowf.models.meta import Base
5 
6 
7 ##
8 #
9 # Pivot table between users and the users they follow.
10 #
11 class Buddy(Base):
12 
13  __tablename__ = 'buddies'
14  user_id = Column(
15  Integer(unsigned=True), ForeignKey('users.id', ondelete='cascade'),
16  primary_key=True, autoincrement=False)
17  buddy_id = Column(
18  Integer(unsigned=True), ForeignKey('users.id', ondelete='cascade'),
19  primary_key=True, autoincrement=False)
20 
21 
22 ##
23 #
24 # Pivot table between users and the challenges they are in.
25 #
27 
28  __tablename__ = 'users_challenges'
29  user_id = Column(
30  Integer(unsigned=True), ForeignKey('users.id', ondelete='cascade'),
31  primary_key=True, autoincrement=False)
32  challenge_id = Column(
33  Integer(unsigned=True), ForeignKey('challenges.id', ondelete='cascade'),
34  primary_key=True, autoincrement=False)
35  is_accepted = Column(Boolean, default=None)
36 
37  user = relationship('User', lazy='joined')
38  challenge = relationship('Challenge', lazy='joined')
39 
40  def __init__(self, user, challenge):
41  self.user = user
42  self.challenge = challenge
43 
44  @classmethod
45  def create(cls, user, challenge):
46  return super(UserChallenge, cls).create(user=user, challenge=challenge)
47 
48  def accept(self):
49  self.is_accepted = True
50 
51  def deny(self):
52  self.is_accepted = False
53 
54 
55 ##
56 #
57 # Pivot table between users and the notifications they should receive.
58 #
60 
61  __tablename__ = 'users_notifications'
62  notification_id = Column(
63  Integer(unsigned=True),
64  ForeignKey('notifications.id', ondelete='cascade'),
65  primary_key=True, autoincrement=False)
66  user_id = Column(
67  Integer(unsigned=True), ForeignKey('users.id', ondelete='cascade'),
68  primary_key=True, autoincrement=False)
69  is_confirmed = Column(Boolean, nullable=False, default=False)
70 
71  def confirm(self):
72  self.is_confirmed = True
73 
74 
75 ##
76 #
77 # Pivot table between users and the groups they belong to.
78 #
79 class UserGroup(Base):
80 
81  __tablename__ = 'users_groups'
82  user_id = Column(
83  Integer(unsigned=True), ForeignKey('users.id', ondelete='cascade'),
84  primary_key=True, autoincrement=False)
85  group_id = Column(
86  Integer(unsigned=True), ForeignKey('groups.id', ondelete='cascade'),
87  primary_key=True, autoincrement=False)
88