You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
suchwow/suchwow/models.py

157 lines
4.2 KiB
Python

from random import choice
from os import path
from datetime import datetime
2 years ago
from peewee import *
from PIL import Image
2 years ago
from suchwow import wownero
from suchwow import config
db = SqliteDatabase(f"{config.DATA_FOLDER}/sqlite.db")
ban_reasons = [
'you smell bad',
'didnt pass the vibe check, homie',
'your memes are bad and you should feel bad',
'i just dont like you'
]
def get_ban_reason():
return choice(ban_reasons)
class Post(Model):
id = AutoField()
title = CharField()
text = CharField()
submitter = CharField()
image_name = CharField()
readonly = BooleanField(default=False)
hidden = BooleanField(default=False)
account_index = IntegerField()
address_index = IntegerField()
timestamp = DateTimeField(default=datetime.now)
reddit_url = CharField(null=True)
to_reddit = BooleanField(default=False)
to_discord = BooleanField(default=False)
approved = BooleanField(default=False)
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):
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:
return 0
except:
return '?'
def hours_elapsed(self):
now = datetime.utcnow()
diff = now - self.timestamp
return diff.total_seconds() / 60 / 60
4 years ago
def show(self):
return {
'id': self.id,
'title': self.title,
'text': self.text,
'submitter': self.submitter,
'image_name': self.image_name,
'image_path': self.get_image_path(),
'thumbnail_name': self.get_thumbnail_name(),
'thumbnail_path': self.get_image_path(True),
4 years ago
'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,
'received_wow': self.get_received_wow(),
'hours_elapsed': self.hours_elapsed()
4 years ago
}
class Meta:
database = db
class Moderator(Model):
id = AutoField()
username = CharField(unique=True)
class Meta:
database = db
class Profile(Model):
id = AutoField()
username = CharField()
address = CharField()
4 years ago
notifications = IntegerField(default=0)
class Meta:
database = db
class Comment(Model):
id = AutoField()
comment = TextField()
4 years ago
commenter = ForeignKeyField(Profile)
post = ForeignKeyField(Post)
timestamp = DateTimeField(default=datetime.now)
class Meta:
database = db
class Notification(Model):
type = CharField()
message = TextField()
4 years ago
username = ForeignKeyField(Profile)
timestamp = DateTimeField(default=datetime.now)
class Meta:
database = db
class Ban(Model):
id = AutoField()
user = ForeignKeyField(Profile)
reason = TextField()
timestamp = DateTimeField(default=datetime.now)
class Meta:
database = db
2 years ago
class AuditEvent(Model):
id = AutoField()
user = ForeignKeyField(Profile)
timestamp = DateTimeField(default=datetime.now)
action = CharField()
class Meta:
database = db