-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
122 lines (95 loc) · 3.83 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from flask import Flask
from flask_migrate import Migrate, MigrateCommand
from flask_sqlalchemy import SQLAlchemy
from flask_script import Manager
from config import Config
from database import Base
from flask_security import UserMixin, RoleMixin
from sqlalchemy.orm import relationship, backref
from sqlalchemy import (
Boolean, DateTime, Column, Integer,
String, ForeignKey, LargeBinary
)
import datetime
app = Flask(__name__)
# set up the configurations
app.config.from_object(Config)
# set up sql alchemy
db = SQLAlchemy(app)
# impoet from database base
migrate = Migrate(app, Base)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
class RolesUsers(Base):
__tablename__ = 'roles_users'
id = Column(Integer(), primary_key=True)
user_id = Column('user_id', Integer(), ForeignKey('user.id'))
role_id = Column('role_id', Integer(), ForeignKey('role.id'))
class Role(Base, RoleMixin):
__tablename__ = 'role'
id = Column(Integer(), primary_key=True)
name = Column(String(80), unique=True)
description = Column(String(255))
class User(Base, UserMixin):
__tablename__ = "user"
id = Column(Integer, primary_key=True)
username = Column(String(255), unique=True)
password = Column(String(255))
first_name = Column(String(64), index=True)
second_name = Column(String(64), index=True)
email = Column(String(255), unique=True, nullable=False)
phone_number = Column(String(20), nullable=True)
active = Column(Boolean, default=True)
photographer = relationship("Photographer", uselist=False, back_populates="user")
current_photo = relationship("CurrentPhoto", uselist=False, back_populates="user")
date_created = Column(DateTime, default=datetime.datetime.utcnow)
current_login_at = Column(DateTime())
last_login_ip = Column(String(100))
current_login_ip = Column(String(100))
login_count = Column(Integer)
confirmed_at = Column(DateTime())
roles = relationship('Role', secondary='roles_users',
backref=backref('users', lazy='dynamic'))
def __repr__(self):
return '<User {}>'.format(self.username)
# def is_photographer(self):
# return True if self.photographer else False
class Photographer(Base):
__tablename__ = "photographer"
id = Column(Integer, primary_key=True)
user_id = Column('user_id', Integer(), ForeignKey('user.id'), unique=True)
photos = relationship("Photo", backref='photographer', lazy=True)
user = relationship("User", uselist=False, back_populates="photographer")
def __repr__(self):
return '<Photographer {}>'.format(self.id)
class PhotoMixin:
file = Column(LargeBinary)
file_type = Column(String(100))
# dimension of the image x ad y
x = Column(Integer)
y = Column(Integer)
def set_size(self, x: int, y: int):
# set the size of the photo
self.x = x
self.y = y
def get_size(self):
return self.x, self.y
def add_image_data(self, dimension1, dimension2, file_type):
self.set_size(dimension1, dimension2)
self.file_type = file_type
class CurrentPhoto(Base, PhotoMixin):
__tablename__ = "current_photo"
id = Column(Integer, primary_key=True)
user_id = Column('user_id', Integer(), ForeignKey('user.id'), unique=True)
user = relationship("User", uselist=False, back_populates="current_photo")
date_added = Column(DateTime, default=datetime.datetime.utcnow)
def __repr__(self):
return '<Current Photo {}>'.format(self.id)
class Photo(Base, PhotoMixin):
__tablename__ = "photo"
id = Column(Integer, primary_key=True)
name = Column(String(255), index=True, unique=True)
photographer_id = Column(Integer, ForeignKey("photographer.id"), index=True)
date_added = Column(DateTime, default=datetime.datetime.utcnow)
if __name__ == '__main__':
manager.run()