added functional groups api + started pictures
This commit is contained in:
262
src/db/groups.py
Normal file
262
src/db/groups.py
Normal file
@ -0,0 +1,262 @@
|
||||
import psycopg2.extras
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
# group create and delete
|
||||
|
||||
def create_group(
|
||||
conn: connection,
|
||||
groupname: str,
|
||||
invite_code: str,
|
||||
author: str,
|
||||
allow_skips: bool = True,
|
||||
feed_interval_minutes: int = 1440,
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
insert into picrinth.groups
|
||||
(groupname, invite_code, author, allow_skips,
|
||||
feed_interval_minutes, created_at)
|
||||
values (%s, %s, %s, %s, %s, now())
|
||||
""",
|
||||
(groupname, invite_code, author, allow_skips, feed_interval_minutes),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
|
||||
def delete_group(
|
||||
conn: connection,
|
||||
groupname: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
delete from picrinth.groups
|
||||
where groupname = %s
|
||||
""",
|
||||
(groupname,),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
|
||||
# group checks
|
||||
|
||||
def check_group_existence(
|
||||
conn: connection,
|
||||
groupname: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select exists(
|
||||
select 1
|
||||
from picrinth.groups
|
||||
where groupname = %s
|
||||
);
|
||||
""",
|
||||
(groupname,),
|
||||
)
|
||||
return cur.fetchone()[0] # type: ignore
|
||||
|
||||
|
||||
def check_invite_code(
|
||||
conn: connection,
|
||||
invite_code: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select exists(
|
||||
select 1
|
||||
from picrinth.groups
|
||||
where invite_code = %s
|
||||
);
|
||||
""",
|
||||
(invite_code,),
|
||||
)
|
||||
return cur.fetchone()[0] # type: ignore
|
||||
|
||||
|
||||
# group updates
|
||||
|
||||
def update_group_groupname(
|
||||
conn: connection,
|
||||
groupname: str,
|
||||
new_groupname: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
update picrinth.groups
|
||||
set groupname = %s
|
||||
where groupname = %s
|
||||
""",
|
||||
(new_groupname, groupname),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
def update_group_author(
|
||||
conn: connection,
|
||||
groupname: str,
|
||||
author: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
update picrinth.groups
|
||||
set author = %s
|
||||
where groupname = %s
|
||||
""",
|
||||
(author, groupname),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
def update_group_invite_code(
|
||||
conn: connection,
|
||||
groupname: str,
|
||||
invite_code: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
update picrinth.groups
|
||||
set invite_code = %s
|
||||
where groupname = %s
|
||||
""",
|
||||
(invite_code, groupname),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
|
||||
def update_group_allow_skips(
|
||||
conn: connection,
|
||||
groupname: str,
|
||||
allow_skips: bool
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
update picrinth.groups
|
||||
set allow_skips = %s
|
||||
where groupname = %s
|
||||
""",
|
||||
(allow_skips, groupname),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
def update_group_feed_interval(
|
||||
conn: connection,
|
||||
groupname: str,
|
||||
feed_interval: int
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
update picrinth.groups
|
||||
set feed_interval_minutes = %s
|
||||
where groupname = %s
|
||||
""",
|
||||
(feed_interval, groupname),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
def update_group_last_feed(
|
||||
conn: connection,
|
||||
groupname: str,
|
||||
last_feed_id: int
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
update picrinth.groups
|
||||
set last_feed_id = %s
|
||||
where groupname = %s
|
||||
""",
|
||||
(last_feed_id, groupname),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
|
||||
# group receiving
|
||||
|
||||
def get_group(
|
||||
conn: connection,
|
||||
groupname: str
|
||||
):
|
||||
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select groupname, author,
|
||||
invite_code, allow_skips,
|
||||
feed_interval_minutes,
|
||||
last_feed_id, created_at
|
||||
from picrinth.groups
|
||||
where groupname = %s
|
||||
""",
|
||||
(groupname,),
|
||||
)
|
||||
return cur.fetchone()
|
||||
|
||||
|
||||
def get_group_by_invite_code(
|
||||
conn: connection,
|
||||
invite_code: str
|
||||
):
|
||||
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select groupname, author,
|
||||
invite_code, allow_skips,
|
||||
feed_interval_minutes,
|
||||
last_feed_id, created_at
|
||||
from picrinth.groups
|
||||
where invite_code = %s
|
||||
""",
|
||||
(invite_code,),
|
||||
)
|
||||
return cur.fetchone()
|
||||
|
||||
|
||||
def get_groupname_by_invite_code(
|
||||
conn: connection,
|
||||
invite_code: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select groupname
|
||||
from picrinth.groups
|
||||
where invite_code = %s
|
||||
""",
|
||||
(invite_code,),
|
||||
)
|
||||
result = cur.fetchone()
|
||||
if result is None:
|
||||
return None
|
||||
return result[0] # type: ignore
|
||||
|
||||
def get_group_invite_code(
|
||||
conn: connection,
|
||||
groupname: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select invite_code
|
||||
from picrinth.groups
|
||||
where groupname = %s
|
||||
""",
|
||||
(groupname,),
|
||||
)
|
||||
result = cur.fetchone()
|
||||
if result is None:
|
||||
return None
|
||||
return result[0] # type: ignore
|
||||
93
src/db/memberships.py
Normal file
93
src/db/memberships.py
Normal file
@ -0,0 +1,93 @@
|
||||
import psycopg2.extras
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
# membership create and delete
|
||||
|
||||
def create_membership(
|
||||
conn: connection,
|
||||
username: str,
|
||||
groupname: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
insert into picrinth.memberships
|
||||
(username, groupname, joined_at)
|
||||
values (%s, %s, now())
|
||||
""",
|
||||
(username, groupname),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
|
||||
def delete_membership(
|
||||
conn: connection,
|
||||
username: str,
|
||||
groupname: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
delete from picrinth.memberships
|
||||
where username = %s and groupname = %s
|
||||
""",
|
||||
(username, groupname),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
|
||||
# membership checks
|
||||
|
||||
def check_membership_exists(
|
||||
conn: connection,
|
||||
username: str,
|
||||
groupname: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select exists(
|
||||
select 1
|
||||
from picrinth.memberships
|
||||
where username = %s and groupname = %s
|
||||
);
|
||||
""",
|
||||
(username, groupname),
|
||||
)
|
||||
return cur.fetchone()[0] # type: ignore
|
||||
|
||||
|
||||
# membership receiving
|
||||
|
||||
def get_memberships_by_username(
|
||||
conn: connection,
|
||||
username: str
|
||||
):
|
||||
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select *
|
||||
from picrinth.memberships
|
||||
where username = %s
|
||||
""",
|
||||
(username,),
|
||||
)
|
||||
return cur.fetchall()
|
||||
|
||||
|
||||
def get_memberships_by_groupname(
|
||||
conn: connection,
|
||||
groupname: str
|
||||
):
|
||||
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select *
|
||||
from picrinth.memberships
|
||||
where groupname = %s
|
||||
""",
|
||||
(groupname,),
|
||||
)
|
||||
return cur.fetchall()
|
||||
97
src/db/pictures.py
Normal file
97
src/db/pictures.py
Normal file
@ -0,0 +1,97 @@
|
||||
import json
|
||||
|
||||
import psycopg2.extras
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
# picture create and delete
|
||||
|
||||
def create_picture(
|
||||
conn: connection,
|
||||
source: str,
|
||||
external_id: str,
|
||||
url: str,
|
||||
metadata: dict
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
insert into picrinth.pictures
|
||||
(source, external_id, url, metadata, created_at)
|
||||
values (%s, %s, %s, %s, now())
|
||||
returning id
|
||||
""",
|
||||
(source, external_id, url, json.dumps(metadata)),
|
||||
)
|
||||
result = cur.fetchone()
|
||||
conn.commit()
|
||||
if result is None:
|
||||
return None
|
||||
return result[0]
|
||||
|
||||
|
||||
def delete_picture_by_url(
|
||||
conn: connection,
|
||||
url: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
delete from picrinth.pictures
|
||||
where url = %s
|
||||
""",
|
||||
(url,),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
def delete_picture_by_id(
|
||||
conn: connection,
|
||||
id: int
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
delete from picrinth.pictures
|
||||
where id = %s
|
||||
""",
|
||||
(id,),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
|
||||
# picture receiving
|
||||
|
||||
def get_picture_by_url(
|
||||
conn: connection,
|
||||
url: str
|
||||
):
|
||||
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select id, source,
|
||||
external_id, url,
|
||||
metadata, created_at
|
||||
from picrinth.pictures
|
||||
where url = %s
|
||||
""",
|
||||
(url,),
|
||||
)
|
||||
return cur.fetchone()
|
||||
|
||||
def get_picture_by_id(
|
||||
conn: connection,
|
||||
id: str
|
||||
):
|
||||
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select id, source,
|
||||
external_id, url,
|
||||
metadata, created_at
|
||||
from picrinth.pictures
|
||||
where id = %s
|
||||
""",
|
||||
(id,),
|
||||
)
|
||||
return cur.fetchone()
|
||||
@ -55,7 +55,7 @@ def check_user_existence(
|
||||
""",
|
||||
(username,),
|
||||
)
|
||||
return cur.fetchone()
|
||||
return cur.fetchone()[0] # type: ignore
|
||||
|
||||
def check_user_disabled(
|
||||
conn: connection,
|
||||
@ -70,7 +70,10 @@ def check_user_disabled(
|
||||
""",
|
||||
(username,),
|
||||
)
|
||||
return cur.fetchone()
|
||||
result = cur.fetchone()
|
||||
if result is None:
|
||||
return None
|
||||
return result[0] # type: ignore
|
||||
|
||||
|
||||
# user updates
|
||||
@ -91,6 +94,7 @@ def update_user_disabled(
|
||||
(disabled, username),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
def update_user_role(
|
||||
conn: connection,
|
||||
@ -107,6 +111,7 @@ def update_user_role(
|
||||
(role, username),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
|
||||
def update_user_username(
|
||||
@ -124,6 +129,7 @@ def update_user_username(
|
||||
(newUsername, username),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
def update_user_password(
|
||||
conn: connection,
|
||||
@ -140,6 +146,7 @@ def update_user_password(
|
||||
(password, username),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
|
||||
def update_user_last_seen(
|
||||
@ -156,6 +163,7 @@ def update_user_last_seen(
|
||||
(username,),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
|
||||
# user receiving
|
||||
@ -190,4 +198,7 @@ def get_user_password(
|
||||
""",
|
||||
(username,),
|
||||
)
|
||||
return cur.fetchone()[0] # type: ignore
|
||||
result = cur.fetchone()
|
||||
if result is None:
|
||||
return None
|
||||
return result[0] # type: ignore
|
||||
|
||||
Reference in New Issue
Block a user