diff --git a/service/src/FlaskWebsite/website/models.py b/service/src/FlaskWebsite/website/models.py index 32172ed..028f5d8 100644 --- a/service/src/FlaskWebsite/website/models.py +++ b/service/src/FlaskWebsite/website/models.py @@ -7,7 +7,8 @@ db.Column('name', db.String(150)), db.Column('NoteId', db.Integer, db.ForeignKey('Note.id')), db.Column('UserId', db.Integer, db.ForeignKey('User.id')), - db.Column('endDate', db.Date)) + db.Column('endDate', db.Date), + db.Column('group_key', db.String(255))) class Note(db.Model): __tablename__ = 'Note' @@ -31,6 +32,5 @@ class User(db.Model, UserMixin): notes = db.relationship('Note', secondary=NoteGroup, backref='User') - diff --git a/service/src/FlaskWebsite/website/templates/base.html b/service/src/FlaskWebsite/website/templates/base.html index fbbd385..b71a5a2 100644 --- a/service/src/FlaskWebsite/website/templates/base.html +++ b/service/src/FlaskWebsite/website/templates/base.html @@ -32,6 +32,7 @@ {% if user.is_authenticated %} Home Logout + Groups {% else %} Login Sign Up diff --git a/service/src/FlaskWebsite/website/templates/groups.html b/service/src/FlaskWebsite/website/templates/groups.html index 8a27f95..6588121 100644 --- a/service/src/FlaskWebsite/website/templates/groups.html +++ b/service/src/FlaskWebsite/website/templates/groups.html @@ -1,18 +1,25 @@ {% extends "base.html" %} {% block title %}Group Channels{% endblock %} {% block content %} -

Groups

-
- -
+ + +
+ +
- +
- {% endblock %} \ No newline at end of file +{% endblock %} + + + + \ No newline at end of file diff --git a/service/src/FlaskWebsite/website/templates/groups_copy.html b/service/src/FlaskWebsite/website/templates/groups_copy.html new file mode 100644 index 0000000..ae554eb --- /dev/null +++ b/service/src/FlaskWebsite/website/templates/groups_copy.html @@ -0,0 +1,24 @@ +{% extends "base.html" %} {% block title %}Group Channels{% endblock %} {% block content + %} +

Groups

+ +
+ + +
+ +
+
+ +
+
+ {% endblock %} \ No newline at end of file diff --git a/service/src/FlaskWebsite/website/views.py b/service/src/FlaskWebsite/website/views.py index 651b714..0ade0df 100644 --- a/service/src/FlaskWebsite/website/views.py +++ b/service/src/FlaskWebsite/website/views.py @@ -28,25 +28,70 @@ def home(): @views.route('/creategroup', methods=['GET', 'POST']) @login_required -def creategroup(): +def group_headfunction(): + if request.method == 'POST': + print(request.form) + if 'join_group' in request.form: + group_id = request.form.get('join_group') + key = request.form.get('group_key_join_' + str(group_id)) + join_group(group_id, key) + elif 'add_group' in request.form: + group_name = request.form.get('group_name') + group_key = request.form.get('group_key') + creategroup(group_name, group_key) + + try: + # Retrieve all rows from the NoteGroup table + note_groups = db.session.query(NoteGroup).all() + except: + flash('No groups found.', category='error') + return render_template("groups.html", user=current_user, groups=[]) + # Prepare a list of dictionaries where each dictionary represents a row with column names as keys and values as values + groups = [{column.name: getattr(note_group, column.name) for column in NoteGroup.columns} for note_group in note_groups] + return render_template("groups.html", user=current_user, groups=groups) + +def creategroup(group_name, group_key): if request.method == 'POST': group_name = request.form.get('group_name') if len(group_name) < 1: flash('Group Name is too short!', category='error') else: # Create a new NoteGroup instance and add it to the session - new_group = NoteGroup.insert().values(name=group_name, UserId=current_user.id) + new_group = NoteGroup.insert().values(name=group_name, group_key=group_key, UserId=current_user.id) db.session.execute(new_group) db.session.commit() flash('Group added!', category='success') - - print("here") # Retrieve all rows from the NoteGroup table note_groups = db.session.query(NoteGroup).all() # Prepare a list of dictionaries where each dictionary represents a row with column names as keys and values as values groups = [{column.name: getattr(note_group, column.name) for column in NoteGroup.columns} for note_group in note_groups] return render_template("groups.html", user=current_user, groups=groups) +def join_group(group_id, key): + print("Joining group") + group = db.session.query(NoteGroup).filter_by(id=group_id).all() + if group: + if key == group[0].group_key: + #NoteGroup.insert().values(id=group[0].id, name=group[0]['name'], NoteId = group[0].NoteId,UserId=current_user.id, endDate=group[0].endDate, group_key=group[0]['group_key']) + id = group[0][0] + name = group[0][1] + NoteId = group[0][2] + UserId = current_user.id + endDate = group[0][4] + group_key = group[0][5] + print(id, name, NoteId, UserId, endDate, group_key) + + # Add the current user to the group + join = NoteGroup.insert().values(name=name, group_key=group_key, UserId=current_user.id) + db.session.execute(join) + db.session.commit() + flash('You have joined the group!', category='success') + else: + flash('Incorrect key. Please try again.', category='error') + else: + flash('Group not found.', category='error') + return redirect(url_for('views.home')) + @views.route('/creategroup/', methods=['GET', 'POST']) @login_required def group_page(group_id): @@ -54,14 +99,33 @@ def group_page(group_id): if group_allusers: if any(one_user.UserId == current_user.id for one_user in group_allusers): # Retrieve all notes associated with the group - notes = db.session.query(Note).filter_by(owner_id=current_user.id).all() - return render_template("group_page.html", user=current_user, group=group_allusers, notes=notes) + #notes = db.session.query(Note).filter_by(NoteGroup.id = Note).all() + return render_template("group_page.html", user=current_user, group=group_allusers) else: flash('You are not authorized to access this group.', category='error') else: flash('Group not found.', category='error') return redirect(url_for('views.home')) +# @views.route('/creategroup', methods=['POST']) +# @login_required +# def join_group(group_id): +# key = request.form.get('key') +# group = NoteGroup.query.get(group_id) +# if group: +# if key == group.group_key: +# # Add the current user to the group +# group.users.append(current_user) +# db.session.commit() +# flash('You have joined the group!', category='success') +# else: +# flash('Incorrect key. Please try again.', category='error') +# else: +# flash('Group not found.', category='error') +# return redirect(url_for('views.home')) + +#@views.route('/creategroup//addnote', methods=['POST']) + @views.route('/delete-note', methods=['POST']) def delete_note(): note = json.loads(request.data) # this function expects a JSON from the INDEX.js file