diff --git a/aula/Group/routes.py b/aula/Group/routes.py index 8914330..78acfad 100644 --- a/aula/Group/routes.py +++ b/aula/Group/routes.py @@ -5,8 +5,9 @@ import sys, datetime Group = Blueprint('Group', __name__) -@Group.route("/groups/index", methods=['GET']) +@Group.route("/groups", methods=['GET']) def index(): + current_user.join_group(1000) groups = current_user.get_groups() return render_template('groups.html', groups=groups) diff --git a/aula/models.py b/aula/models.py index 1493ede..98ee88a 100644 --- a/aula/models.py +++ b/aula/models.py @@ -66,10 +66,10 @@ class User(tuple, UserMixin): def get_groups(self): cur = conn.cursor() - sql_call = f""" - SELECT groups.* FROM users_groups JOIN groups ON users_groups.group_id = groups.group_id WHERE users_groups.user_id = {self.user_id} + sql_call = """ + SELECT groups.* FROM users_groups JOIN groups ON users_groups.group_id = groups.group_id WHERE users_groups.user_id = %s """ - cur.execute(sql_call) + cur.execute(sql_call, (self.user_id,)) groups = cur.fetchall() result = [] for group_data in groups: @@ -77,6 +77,25 @@ class User(tuple, UserMixin): cur.close() return result + def leave_group(self, group_id): + # TODO: Tjek om brugeren må forlade gruppen + cur = conn.cursor() + sql_call = """ + DELETE FROM users_groups WHERE user_id = %s AND group_id = %s + """ + cur.execute(sql_call, (self.user_id, group_id)) + conn.commit() + cur.close() + + def join_group(self, group_id): + cur = conn.cursor() + sql_call = """ + INSERT INTO users_groups VALUES (%s, %s) + """ + cur.execute(sql_call, (self.user_id, group_id)) + conn.commit() + cur.close() + def insert_users(user_id, first_name, last_name, password, email, adresse, role): cur = conn.cursor()