suchwow/suchwow/models.py

126 lines
3.6 KiB
Python
Raw Normal View History

2020-07-15 09:18:13 +01:00
from peewee import *
2021-01-29 06:01:42 +00:00
from os import path
2020-07-15 09:18:13 +01:00
from datetime import datetime
2021-01-29 06:01:42 +00:00
from PIL import Image
from suchwow import wownero
from suchwow import config
2020-07-15 09:18:13 +01:00
2021-09-14 08:59:06 +01:00
db = SqliteDatabase(f"{config.DATA_FOLDER}/sqlite.db")
2020-07-15 09:18:13 +01:00
2020-07-28 21:31:07 +01:00
class Post(Model):
2020-07-15 09:18:13 +01:00
id = AutoField()
title = CharField()
text = CharField()
2020-07-15 09:18:13 +01:00
submitter = CharField()
image_name = CharField()
2020-07-28 21:31:07 +01:00
readonly = BooleanField(default=False)
hidden = BooleanField(default=False)
account_index = IntegerField()
address_index = IntegerField()
timestamp = DateTimeField(default=datetime.now)
reddit_url = CharField(null=True)
2020-10-18 05:39:19 +01:00
to_reddit = BooleanField(default=False)
2020-10-29 08:13:02 +00:00
to_discord = BooleanField(default=False)
approved = BooleanField(default=False)
2021-01-29 06:01:42 +00:00
def get_image_path(self, thumbnail=False):
save_path_base = path.join(config.DATA_FOLDER, "uploads")
if thumbnail:
save_path = path.join(save_path_base, self.get_thumbnail_name())
else:
save_path = path.join(save_path_base, self.image_name)
return save_path
def save_thumbnail(self):
try:
image = Image.open(self.get_image_path())
image.thumbnail((200,200), Image.ANTIALIAS)
image.save(self.get_image_path(True), format=image.format, quality=90)
image.close()
return True
except:
return False
def get_thumbnail_name(self):
s = path.splitext(self.image_name)
return s[0] + '.thumbnail' + s[1]
def get_received_wow(self):
2021-04-30 03:27:21 +01:00
try:
w = wownero.Wallet()
it = w.incoming_transfers(self.account_index)
if 'transfers' in it:
amounts = [amt['amount'] for amt in it['transfers'] if 'transfers' in it]
return wownero.as_wownero(wownero.from_atomic(sum(amounts)))
else:
2021-05-05 09:50:54 +01:00
return 0
2021-04-30 03:27:21 +01:00
except:
return '?'
2021-05-05 09:50:54 +01:00
def hours_elapsed(self):
now = datetime.utcnow()
diff = now - self.timestamp
return diff.total_seconds() / 60 / 60
2020-12-29 20:41:10 +00:00
def show(self):
return {
'id': self.id,
'title': self.title,
'text': self.text,
'submitter': self.submitter,
'image_name': self.image_name,
2021-01-29 06:01:42 +00:00
'image_path': self.get_image_path(),
'thumbnail_name': self.get_thumbnail_name(),
'thumbnail_path': self.get_image_path(True),
2020-12-29 20:41:10 +00:00
'readonly': self.readonly,
'hidden': self.hidden,
'account_index': self.account_index,
'address_index': self.address_index,
'timestamp': self.timestamp,
'reddit_url': self.reddit_url,
'to_reddit': self.to_reddit,
'to_discord': self.to_discord,
'approved': self.approved,
2021-05-05 09:50:54 +01:00
'received_wow': self.get_received_wow(),
'hours_elapsed': self.hours_elapsed()
2020-12-29 20:41:10 +00:00
}
class Meta:
database = db
class Moderator(Model):
id = AutoField()
username = CharField(unique=True)
2020-07-28 21:31:07 +01:00
class Meta:
database = db
class Profile(Model):
id = AutoField()
username = CharField()
address = CharField()
2020-08-10 07:59:45 +01:00
notifications = IntegerField(default=0)
2020-07-28 21:31:07 +01:00
class Meta:
database = db
class Comment(Model):
id = AutoField()
comment = TextField()
2020-08-10 07:59:45 +01:00
commenter = ForeignKeyField(Profile)
post = ForeignKeyField(Post)
2020-07-28 21:31:07 +01:00
timestamp = DateTimeField(default=datetime.now)
class Meta:
database = db
class Notification(Model):
type = CharField()
message = TextField()
2020-08-10 07:59:45 +01:00
username = ForeignKeyField(Profile)
2020-07-15 09:18:13 +01:00
timestamp = DateTimeField(default=datetime.now)
class Meta:
database = db