mattermore/app/models.py

66 lines
2.1 KiB
Python
Raw Normal View History

2018-09-24 20:57:36 +02:00
from .app import db
2018-10-03 01:18:00 +02:00
from datetime import datetime
import re
2018-09-24 20:57:36 +02:00
2019-03-27 23:40:21 +01:00
MONTHS = ["januari", "februari", "maart", "april", "mei", "juni", "juli", "augustus", "september",
"oktober", "november", "december"]
2018-09-24 20:57:36 +02:00
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
2018-09-24 21:26:12 +02:00
username = db.Column(db.String(255), unique=True, nullable=False)
2018-09-24 20:57:36 +02:00
authorized = db.Column(db.Boolean, default=True)
admin = db.Column(db.Boolean, default=False)
def __repr__(self):
return '<User %r>' % self.username
def __init__(self, username, admin=False):
super()
self.username = username
self.admin = admin
2018-10-03 01:18:00 +02:00
2019-02-14 20:41:07 +01:00
2018-10-03 01:18:00 +02:00
class Quote(db.Model):
id = db.Column(db.Integer, primary_key=True)
quoter = db.Column(db.String(255), unique=False, nullable=False)
quotee = db.Column(db.String(255), unique=False, nullable=True)
channel = db.Column(db.String(255), unique=False, nullable=False)
2019-02-14 20:41:07 +01:00
quote = db.Column(db.String(16383), unique=False, nullable=False)
2018-10-03 01:18:00 +02:00
created_at = db.Column(
2019-02-14 20:41:07 +01:00
db.DateTime, nullable=False,
default=datetime.utcnow
)
2018-10-03 01:18:00 +02:00
QUOTEE_REGEX = re.compile('\W*([a-zA-Z\-_0-9]+).*')
2018-10-03 01:18:00 +02:00
def __repr__(self):
2019-02-13 22:38:21 +01:00
return "<Quote {} \"{}\">".format(self.quoter, self.quote)
2018-10-03 01:18:00 +02:00
2019-02-14 00:50:38 +01:00
def __init__(self, quoter, quote, channel, created_at=None):
2018-10-03 01:18:00 +02:00
super()
self.quoter = quoter
self.quote = quote
self.channel = channel
2019-02-14 00:50:38 +01:00
if created_at is None:
self.created_at = datetime.utcnow()
else:
self.created_at = created_at
2018-10-03 01:18:00 +02:00
# Experimentally try to find quoted user
quotee_match = Quote.QUOTEE_REGEX.search(quote)
self.quotee = quotee_match.group(1) if quotee_match is not None else None
2019-03-27 23:40:21 +01:00
def slur(self):
return self.created_at.strftime("%Y-%m-%d_%H:%M:%S")
def created_at_machine(self):
2019-03-28 01:41:08 +01:00
return self.created_at.strftime("%Y-%m-%dT%H:%M:%S%z")
2019-03-27 23:40:21 +01:00
def created_at_human(self):
c = self.created_at
return "{} {} {:04}, {}:{:02}".format(
c.day, MONTHS[c.month - 1], c.year,
c.hour, c.minute
)