Flesh out database schema
This commit is contained in:
parent
1059a4d62d
commit
5ed02a5fbf
|
@ -1,22 +1,29 @@
|
||||||
from sqlalchemy import Column, Integer, String, Unicode, Boolean, DateTime
|
from sqlalchemy import Column, Integer, String, Unicode, Boolean, DateTime
|
||||||
from sqlalchemy import ForeignKey, Table, UnicodeText, Text, text
|
from sqlalchemy import ForeignKey, Table, UnicodeText, Text, text
|
||||||
from sqlalchemy.orm import relationship, backref
|
from sqlalchemy.orm import relationship, backref
|
||||||
|
from sqlalchemy_utils import ChoiceType
|
||||||
|
|
||||||
from .database import Base
|
from .database import Base
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from enum import Enum
|
||||||
import bcrypt
|
import bcrypt
|
||||||
import os
|
import os
|
||||||
import hashlib
|
import hashlib
|
||||||
|
|
||||||
|
class DonationType(Enum):
|
||||||
|
one_time = "one_time"
|
||||||
|
monthly = "monthly"
|
||||||
|
|
||||||
class User(Base):
|
class User(Base):
|
||||||
__tablename__ = 'user'
|
__tablename__ = 'users'
|
||||||
id = Column(Integer, primary_key = True)
|
id = Column(Integer, primary_key=True)
|
||||||
email = Column(String(256), nullable = False, index = True)
|
email = Column(String(256), nullable=False, index=True)
|
||||||
admin = Column(Boolean())
|
admin = Column(Boolean())
|
||||||
password = Column(String)
|
password = Column(String)
|
||||||
created = Column(DateTime)
|
created = Column(DateTime)
|
||||||
passwordReset = Column(String(128))
|
password_reset = Column(String(128))
|
||||||
passwordResetExpiry = Column(DateTime)
|
password_reset_expires = Column(DateTime)
|
||||||
|
|
||||||
def set_password(self, password):
|
def set_password(self, password):
|
||||||
self.password = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
|
self.password = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
|
||||||
|
@ -28,7 +35,7 @@ class User(Base):
|
||||||
self.set_password(password)
|
self.set_password(password)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<User %r>' % self.username
|
return "<User {}>".format(self.username)
|
||||||
|
|
||||||
# Flask.Login stuff
|
# Flask.Login stuff
|
||||||
# We don't use most of these features
|
# We don't use most of these features
|
||||||
|
@ -40,3 +47,41 @@ class User(Base):
|
||||||
return False
|
return False
|
||||||
def get_id(self):
|
def get_id(self):
|
||||||
return self.email
|
return self.email
|
||||||
|
|
||||||
|
class Donation(Base):
|
||||||
|
__tablename__ = 'donations'
|
||||||
|
id = Column(Integer, primary_key=True)
|
||||||
|
user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
||||||
|
user = relationship("User", backref=backref("donations"))
|
||||||
|
project_id = Column(Integer, ForeignKey("projects.id"))
|
||||||
|
project = relationship("Project", backref=backref("donations"))
|
||||||
|
type = Column(ChoiceType(DonationType, impl=String()))
|
||||||
|
amount = Column(Integer, nullable=False)
|
||||||
|
created = Column(DateTime, nullable=False)
|
||||||
|
|
||||||
|
def __init__(self, user, type, amount):
|
||||||
|
self.user = user
|
||||||
|
self.type = type
|
||||||
|
self.amount = amount
|
||||||
|
self.created = datetime.now()
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "<Donation {} from {}: ${} ({})>".format(
|
||||||
|
self.id,
|
||||||
|
self.user.email,
|
||||||
|
self.amount,
|
||||||
|
self.type
|
||||||
|
)
|
||||||
|
|
||||||
|
class Project(Base):
|
||||||
|
__tablename__ = 'projects'
|
||||||
|
id = Column(Integer, primary_key=True)
|
||||||
|
name = Column(String, nullable=False)
|
||||||
|
created = Column(DateTime, nullable=False)
|
||||||
|
|
||||||
|
def __init__(self, name):
|
||||||
|
self.name = name
|
||||||
|
self.created = datetime.now()
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "<Project {} {}>".format(self.id, self.name)
|
||||||
|
|
|
@ -3,3 +3,4 @@ Flask
|
||||||
Jinja2
|
Jinja2
|
||||||
Flask-Misaka
|
Flask-Misaka
|
||||||
gunicorn
|
gunicorn
|
||||||
|
sqlalchemy-utils
|
||||||
|
|
Loading…
Reference in New Issue