Skip to content

Instantly share code, notes, and snippets.

@itsfarseen
Created October 11, 2018 17:32
Show Gist options
  • Save itsfarseen/d0cd2e029da5f597b91290c4d4e185bd to your computer and use it in GitHub Desktop.
Save itsfarseen/d0cd2e029da5f597b91290c4d4e185bd to your computer and use it in GitHub Desktop.
from peewee import MySQLDatabase, fn
from typing import (Optional, List)
import models
import bcrypt
from models import (OfflineRegAdmin, User, Hospitality, OfflineReg)
database = MySQLDatabase('nitcfest_test',
**{'charset': 'utf8', 'use_unicode': True,
'host': 'tathva.org',
'port': 3306,
'user': 'nitcfest_dd',
'password': 'Dharanee.86'})
class Logic:
def __init__(self) -> None:
models.database_proxy.initialize(database)
def login_admin(self, user: str, passwd: str) -> Optional[OfflineRegAdmin]:
admin_record = OfflineRegAdmin\
.get_or_none(OfflineRegAdmin.username == user
and OfflineRegAdmin.password == passwd)
return admin_record
class LoggedInLogic:
def __init__(self, admin: OfflineRegAdmin) -> None:
self.admin = admin
def get_user_details(self, tathva_id: int) -> Optional[User]:
return User.get_or_none(id == tathva_id)
def who_completed_user_offline_reg(self, tathva_id: int) -> Optional[OfflineRegAdmin]:
offline_reg = OfflineReg.get_or_none(OfflineReg.user == tathva_id)
if offline_reg is None:
return None
return OfflineRegAdmin.get_or_none(OfflineRegAdmin.id == offline_reg.admin)
def who_completed_user_hospitality(self, tathva_id: int) -> Optional[OfflineRegAdmin]:
hospitality = Hospitality.get_or_none(Hospitality.user == tathva_id)
if hospitality is None:
return None
return OfflineRegAdmin.get_or_none(OfflineRegAdmin.id == hospitality.admin)
def get_admin_hos_count(self) -> int:
return Hospitality.select(fn.Count(Hospitality.user)).where(Hospitality.admin == self.admin.id).scalar()
def do_hospitality(self, tathva_id: int) -> None:
Hospitality.create(
user=tathva_id, admin=self.admin.id, hostel="", room="")
def update_hospitality(self, hospitality: Hospitality):
hospitality.save()
def get_hospitality(self, tathva_id: int) -> Optional[Hospitality]:
return Hospitality.get_or_none(Hospitality.user == tathva_id)
def filter_users(self, filter: str) -> List[User]:
if filter.startswith(":") and len(filter) > 1:
try:
users = User.select().where(
User.id == int(filter[1:])).objects()
return users
except ValueError:
pass
if filter.upper().startswith("T18"):
id_filter = fn.LPAD(User.id.cast("CHAR(5)"), 5, '0') **\
"{}%".format(filter[3:])
else:
id_filter = fn.LPAD(User.id.cast("CHAR(5)"), 5, '0') **\
"%{}%".format(filter)
filter_like = "%{}%".format(filter)
users = User.select().where(
id_filter |
User.name ** filter_like |
User.college ** filter_like).order_by(User.id.desc()).limit(50).objects()
return users
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment