def rate_service(service_name, publisher_id, stars, comment): if not (isinstance(stars, int) and 1 <= stars <= 5): raise ValueError("Stars field should be int: from 1 to 5!") user_id = username_to_user_id(service_name) with db_request("Ratings") as Ratings: row = Ratings.select().where((Ratings.food_service_id == user_id) & ( Ratings.client_id == publisher_id)).first() if row is not None: raise ValueError("You have already published your comment!") with db_request("Ratings") as Ratings: Ratings.insert(food_service_id=user_id, client_id=publisher_id, stars_amount=stars, comment_content=comment).execute()
def add_service_servers_location(user_id, private_info): if not (0 < len(private_info) < 256): raise ValueError("Bad private info") with db_request("User") as User: User.update(user_meta=private_info)\ .where(User.id == user_id)\ .execute()
def activate_invite(user_id, group, invite_code): with db_request("Group") as Group: row = Group.select().where(Group.group_name == group).first() if row is None: raise ValueError("You don't own this group/this group doesn't exists") invite_codes_list = loads(row.group_invites_list) if invite_code in invite_codes_list: invite_codes_list.remove(invite_code) add_user_to_group(user_id, group) with db_request("Group") as Group: Group.update(group_invites_list=dumps(invite_codes_list)).where( Group.group_name == group).execute() return "Sucessfully added to {}".format(group) raise ValueError("Incorrect user group or invite code!")
def add_user_to_group(user_id, group_name): with db_request("User") as User: row = User.select().where(User.id == user_id).first() user_groups_set = set(loads(row.user_groups)) user_groups_set.add(group_name) User.update( user_groups=dumps(list(user_groups_set))).where(User.id == user_id)\ .execute()
def get_services_list(offset, amount): with db_request("User") as User: rows = User.select()\ .order_by(User.id.desc())\ .where(User.is_food_service)\ .offset(offset)\ .limit(amount) return [row.username for row in rows]
def create_ticket(provider_name, code, content, target_group): with db_request("TicketStorage") as TicketStorage: TicketStorage.insert( ticket_provider=provider_name, ticket_code=code, ticket_content=content, ticket_target_group=target_group ).execute()
def get_all_food_services_tickets(): with db_request("TicketStorage") as TicketStorage: rows = TicketStorage.select().execute() return [{ "provider": row.ticket_provider, "code": row.ticket_code, "content": row.ticket_content } for row in rows]
def create_user_group(creator_id, group_name): with db_request("Group") as Group: try: Group.insert( group_name=group_name, group_creator_id=creator_id, group_invites_list="[]" ).execute() except IntegrityError: raise ValueError("This group already exists!")
def check_user_password(username, password): username_regexp = re.compile(r'[A-Za-z0-9_-]{3,32}') if not re.fullmatch(username_regexp, username): raise ValueError("Bad Symbols!") with db_request("User") as User: row = User.select().where( (fn.lower(User.username) == username.lower()) & (User.password_hash == salted_sha256_string(password))).first() if row is None: raise ValueError("Bad user credentials") return row.id, row.username, row.is_food_service
def generate_invite_code(user_id, group, amount): invite_codes_list = get_invites_list(group, user_id) invites = [generate_random_hash()[:32] for _ in range(amount)] invite_codes_list += invites with db_request("Group") as Group: Group.update(group_invites_list=dumps(invite_codes_list)).where( (Group.group_name == group) & (Group.group_creator_id == user_id)).execute() return invites
def get_invites_list(group, user_id): with db_request("Group") as Group: row = Group.select().where( (Group.group_name == group) & (Group.group_creator_id == user_id) ).first() if row is None: raise ValueError("You don't own this group/this group doesn't exists") return loads(row.group_invites_list)
def get_tickets_by_user_id(user_id): user_groups = get_user_groups_list_by_user_id(user_id) with db_request("TicketStorage") as TicketStorage: rows = TicketStorage.select().where( TicketStorage.ticket_target_group << user_groups ).execute() return [{ "provider": row.ticket_provider, "code": row.ticket_code, "content": row.ticket_content } for row in rows]
def get_ratings(service_id, stars, offset, amount): if not all([isinstance(element, int) for element in stars]): raise ValueError("Stars list should contain only ints") with db_request("Ratings") as Ratings: rows = Ratings.select().where( (Ratings.food_service_id == service_id) & Ratings.stars_amount << stars)\ .offset(offset)\ .limit(amount) return [{ "stars": row.stars_amount, "comment": row.comment_content } for row in rows]
def register_user(username, password, is_food_provider=False): with db_request("User") as User: try: row = User.select()\ .where(fn.lower(User.username) == username.lower()).first() if row is not None: raise IntegrityError() User.insert(username=username, password_hash=salted_sha256_string(password), registration_date=datetime.now(), is_food_service=is_food_provider).execute() except IntegrityError: raise ValueError( "User \"{}\" already registered!".format(username))
def username_to_user_id(username): with db_request("User") as User: row = User.select().where(User.username == username).first() if row is None: raise ValueError("No such user in db!") return row.id
def get_user_groups_list_by_user_id(user_id): with db_request("User") as User: user_row = User.select().where(User.id == user_id).first() return loads(user_row.user_groups)
def get_service_servers_location(user_id): with db_request("User") as User: row = User.select().where(User.id == user_id).first() return row.user_meta