2022-03-12 12:46:31 +00:00
|
|
|
import os, re, random
|
|
|
|
from typing import Optional, List
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from peewee import SqliteDatabase, SQL, ForeignKeyField
|
|
|
|
import peewee as pw
|
|
|
|
|
|
|
|
import settings
|
|
|
|
|
|
|
|
db = SqliteDatabase(settings.DB_PATH)
|
|
|
|
|
|
|
|
|
|
|
|
class User(pw.Model):
|
|
|
|
id = pw.UUIDField(primary_key=True)
|
2022-03-12 20:06:22 +00:00
|
|
|
created: datetime = pw.DateTimeField(default=datetime.now)
|
2022-03-12 12:46:31 +00:00
|
|
|
username = pw.CharField(unique=True, null=False)
|
|
|
|
address = pw.CharField(null=True)
|
|
|
|
|
2022-03-12 20:06:22 +00:00
|
|
|
@property
|
|
|
|
def created_dt(self):
|
|
|
|
return self.created.strftime('%Y-%m-%d')
|
|
|
|
|
2022-03-12 12:46:31 +00:00
|
|
|
@staticmethod
|
|
|
|
async def search(needle) -> List['User']:
|
|
|
|
needle = needle.replace("*", "")
|
2022-03-12 20:06:22 +00:00
|
|
|
if len(needle) <= 1:
|
2022-03-12 12:46:31 +00:00
|
|
|
raise Exception("need longer search term")
|
2022-03-12 20:06:22 +00:00
|
|
|
|
|
|
|
return User.select().where(
|
|
|
|
User.address.is_null(False),
|
|
|
|
User.username % f"*{needle}*"
|
|
|
|
)
|
2022-03-12 12:46:31 +00:00
|
|
|
|
|
|
|
def to_json(self, ignore_key=None):
|
|
|
|
data = {
|
|
|
|
"id": self.id,
|
|
|
|
"username": self.username,
|
|
|
|
"address": self.address
|
|
|
|
}
|
|
|
|
if isinstance(ignore_key, str):
|
|
|
|
data.pop(ignore_key)
|
|
|
|
return data
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
database = db
|