From 1c88ea885113cb5ea82fdd691bfbe2014f213918 Mon Sep 17 00:00:00 2001 From: Aevann Date: Tue, 13 Feb 2024 15:00:58 +0200 Subject: [PATCH] add ping group description --- files/classes/group.py | 1 + files/routes/groups.py | 27 +++++++++++++++++++ files/templates/group_memberships.html | 25 +++++++++++++++++ files/templates/groups.html | 6 +++++ .../20240213-add-ping-group-description.sql | 1 + 5 files changed, 60 insertions(+) create mode 100644 migrations/20240213-add-ping-group-description.sql diff --git a/files/classes/group.py b/files/classes/group.py index a02a7eef9..4e34a1521 100644 --- a/files/classes/group.py +++ b/files/classes/group.py @@ -15,6 +15,7 @@ class Group(Base): name = Column(String, primary_key=True) created_utc = Column(Integer) owner_id = Column(Integer, ForeignKey("users.id")) + description = Column(String) memberships = relationship("GroupMembership", primaryjoin="GroupMembership.group_name==Group.name", order_by="GroupMembership.approved_utc") owner = relationship("User", primaryjoin="Group.owner_id==User.id") diff --git a/files/routes/groups.py b/files/routes/groups.py index 324b4ab3c..5c2183c9a 100644 --- a/files/routes/groups.py +++ b/files/routes/groups.py @@ -333,3 +333,30 @@ def group_usurp(v, group_name): g.db.add(group) return {"message": f'You have usurped control of !{group.name} successfully!'} + +@app.post("/!/description") +@limiter.limit('1/second', scope=rpath) +@limiter.limit('1/second', scope=rpath, key_func=get_ID) +@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400) +@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID) +@auth_required +def group_change_description(v, group_name): + group_name = group_name.strip().lower() + + group = g.db.get(Group, group_name) + if not group: abort(404) + + if v.id != group.owner_id: + abort(403, f"Only the group owner (@{group.owner.username}) can change the description!") + + description = request.values.get('description') + + if description: + description = description.strip() + else: + description = None + + group.description = description + g.db.add(group) + + return {"message": 'Description changed successfully!'} diff --git a/files/templates/group_memberships.html b/files/templates/group_memberships.html index cc7005dd6..bcb9543dc 100644 --- a/files/templates/group_memberships.html +++ b/files/templates/group_memberships.html @@ -13,6 +13,31 @@ {% endif %} + {% if v.id == group.owner_id %} +
!{{group}} Description
+
+
+
+
+
+ + + Limit of 100 characters + +
+
+
+
+
+ {% elif group.description %} +
!{{group}} Description
+

{{group.description}}

+ {% endif %} +
{% macro process_memberships(memberships, name) %}
!{{group}} {{name}}
diff --git a/files/templates/groups.html b/files/templates/groups.html index e9f62e1b8..1cccac400 100644 --- a/files/templates/groups.html +++ b/files/templates/groups.html @@ -33,6 +33,7 @@ # Name Members + Description Created on @@ -56,6 +57,11 @@ {% endif %} + + {% if group.description %} + {{group.description}} + {% endif %} + {% endfor %} diff --git a/migrations/20240213-add-ping-group-description.sql b/migrations/20240213-add-ping-group-description.sql new file mode 100644 index 000000000..1b571536c --- /dev/null +++ b/migrations/20240213-add-ping-group-description.sql @@ -0,0 +1 @@ +alter table groups add column description varchar(100) default '' not null;