You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
suchwow/suchwow/_models.py

219 lines
5.9 KiB

from random import choice
from os import path
from datetime import datetime
from peewee import *
from PIL import Image
from suchwow import wownero
from suchwow import config
db = SqliteDatabase(f"{config.DATA_FOLDER}/suchwow_db.sqlite")
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 User(Model):
id = AutoField()
username = CharField()
address = CharField(null=True)
moderator = BooleanField(default=False)
banned = BooleanField(default=False)
ban_reason = TextField(null=True)
ban_timestamp = DateField(null=True)
login_timestamp = DateTimeField(null=True)
def get_wow_received(self):
tips = TipReceived.select().join(Post).where(Post.user == self)
return sum(tip.amount for tip in tips)
def get_wow_sent(self):
tips = TipSent.select().where(TipSent.from_user == self)
return sum(tip.amount for tip in tips) + sum(tip.fee for tip in tips)
def get_post_count(self):
posts = Post.select().where(Post.user == self)
return posts.count()
class Meta:
database = db
class Post(Model):
id = AutoField()
title = CharField()
text = CharField(null=True)
user = ForeignKeyField(User)
image_name = CharField()
account_index = IntegerField()
address_index = IntegerField(unique=True)
address = CharField(unique=True)
timestamp = DateTimeField(default=datetime.utcnow)
approved = BooleanField(default=False)
approved_by = ForeignKeyField(User, null=True)
def get_random(self):
all_posts = Post.select().where(Post.approved == True)
if all_posts:
return choice([i.id for i in all_posts])
else:
return None
def get_previous(self):
prev = Post.select().where(Post.id == self.id - 1).first()
if prev and prev.approved:
return prev.id
else:
return None
def get_next(self):
next = Post.select().where(Post.id == self.id + 1).first()
if next and next.approved:
return next.id
else:
return None
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.thumbnail)
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 strip_exif(self):
try:
image = Image.open(self.get_image_path())
data = image.getdata()
image_without_exif = Image.new(image.mode, image.size)
image_without_exif.putdata(data)
image_without_exif.save(self.get_image_path())
image_without_exif.close()
image.close()
except:
return False
def resize_image(self):
try:
with Image.open(self.get_image_path()) as img:
img.thumbnail((1800,1800))
img.save(self.get_image_path())
except:
return False
@property
def resized(self):
s = path.splitext(self.image_name)
return s[0] + '.resized' + s[1]
@property
def thumbnail(self):
s = path.splitext(self.image_name)
return s[0] + '.thumbnail' + s[1]
def get_wow_received(self):
tips = TipReceived.select().where(TipReceived.post == self)
return sum(tip.amount for tip in tips)
def hours_elapsed(self):
now = datetime.utcnow()
diff = now - self.timestamp
return diff.total_seconds() / 60 / 60
def show(self):
return {
'id': self.id,
'title': self.title,
'text': self.text,
'user': self.user.username,
'image_name': self.image_name,
'image_path': self.get_image_path(),
'thumbnail_name': self.thumbnail,
'thumbnail_path': self.get_image_path(True),
'account_index': self.account_index,
'address_index': self.address_index,
'address': self.address,
'timestamp': self.timestamp,
'approved': self.approved,
'approved_by': self.approved_by,
'received_wow': self.get_wow_received(),
'hours_elapsed': self.hours_elapsed(),
'tips_received': [tip for tip in TipReceived.select().where(TipReceived.post == self)]
}
class Meta:
database = db
class SocialPost(Model):
id = AutoField()
post = ForeignKeyField(Post)
service = CharField()
class Meta:
database = db
class Vote(Model):
id = AutoField()
post = ForeignKeyField(Post)
upvote = BooleanField()
timestamp = DateTimeField(default=datetime.now)
class Meta:
database = db
class AuditEvent(Model):
id = AutoField()
user = ForeignKeyField(User)
timestamp = DateTimeField(default=datetime.now)
action = CharField()
class Meta:
database = db
class TipReceived(Model):
id = AutoField()
post = ForeignKeyField(Post)
txid = CharField()
timestamp = DateTimeField()
amount = IntegerField()
fee = IntegerField()
class Meta:
database = db
class TipSent(Model):
id = AutoField()
from_user = ForeignKeyField(User)
to_user = ForeignKeyField(User)
txid = CharField()
timestamp = DateTimeField()
amount = IntegerField()
fee = IntegerField()
class Meta:
database = db