suchwow/suchwow/models.py

126 lines
3.6 KiB
Python
Raw Normal View History

2020-07-15 08:18:13 +00:00
from peewee import *
2021-01-29 06:01:42 +00:00
from os import path
2020-07-15 08:18:13 +00: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 08:18:13 +00:00
2021-09-14 07:59:06 +00:00
db = SqliteDatabase(f"{config.DATA_FOLDER}/sqlite.db")
2020-07-15 08:18:13 +00:00
2020-07-28 20:31:07 +00:00
class Post(Model):
2020-07-15 08:18:13 +00:00
id = AutoField()
title = CharField()
text = CharField()
2020-07-15 08:18:13 +00:00
submitter = CharField()
image_name = CharField()
2020-07-28 20:31:07 +00: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 04:39:19 +00: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 02:27:21 +00: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 08:50:54 +00:00
return 0
2021-04-30 02:27:21 +00:00
except:
return '?'
2021-05-05 08:50:54 +00: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 08:50:54 +00: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 20:31:07 +00:00
class Meta:
database = db
class Profile(Model):
id = AutoField()
username = CharField()
address = CharField()
2020-08-10 06:59:45 +00:00
notifications = IntegerField(default=0)
2020-07-28 20:31:07 +00:00
class Meta:
database = db
class Comment(Model):
id = AutoField()
comment = TextField()
2020-08-10 06:59:45 +00:00
commenter = ForeignKeyField(Profile)
post = ForeignKeyField(Post)
2020-07-28 20:31:07 +00:00
timestamp = DateTimeField(default=datetime.now)
class Meta:
database = db
class Notification(Model):
type = CharField()
message = TextField()
2020-08-10 06:59:45 +00:00
username = ForeignKeyField(Profile)
2020-07-15 08:18:13 +00:00
timestamp = DateTimeField(default=datetime.now)
class Meta:
database = db