Skip to content

Commit

Permalink
Introducing Groups (creating and joining) joining has to be fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
SirGankalot committed May 3, 2024
1 parent 2eaab16 commit 01dab21
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 20 deletions.
4 changes: 2 additions & 2 deletions service/src/FlaskWebsite/website/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -31,6 +32,5 @@ class User(db.Model, UserMixin):
notes = db.relationship('Note', secondary=NoteGroup, backref='User')





1 change: 1 addition & 0 deletions service/src/FlaskWebsite/website/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
{% if user.is_authenticated %}
<a class="nav-item nav-link" id="home" href="/">Home</a>
<a class="nav-item nav-link" id="logout" href="/logout">Logout</a>
<a class="nav-item nav-link" id="groups" href="/creategroup">Groups</a>
{% else %}
<a class="nav-item nav-link" id="login" href="/login">Login</a>
<a class="nav-item nav-link" id="signUp" href="/sign-up">Sign Up</a>
Expand Down
31 changes: 19 additions & 12 deletions service/src/FlaskWebsite/website/templates/groups.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
{% extends "base.html" %} {% block title %}Group Channels{% endblock %} {% block content
%}
<h1 align="center">Groups</h1>
<ul class="list-group list-group-flush" id="groups">
{% for group in groups %}
<li class="list-group-item">
<a href="{{ url_for('views.group_page', group_id=group.id) }}">{{ group.name }}</a>
</li>
{% endfor %}
</ul>
<form method="POST">
<textarea name="group_name" id="group_name" class="form-control"></textarea>
<br />
<ul class="list-group list-group-flush" id="groups">
{% for group in groups %}
<li class="list-group-item">
<a href="{{ url_for('views.group_page', group_id=group.id) }}">{{ group.name }}</a>
<input type="text" name="group_key_join_{{ group.id }}" class="form-control" placeholder="Enter group key">
<button type="submit" class="btn btn-primary" name="join_group" value="{{ group.id }}">Join Group</button>
</li>
{% endfor %}
</ul>
<input type="text" name="group_name" class="form-control" placeholder="Enter group name">
<br>
<input type="text" name="group_key" class="form-control" placeholder="Enter group key">
<br>
<div align="center">
<button type="submit" class="btn btn-primary">Add Group</button>
<button type="submit" class="btn btn-primary" name="add_group">Add Group</button>
</div>
</form>
{% endblock %}
{% endblock %}




24 changes: 24 additions & 0 deletions service/src/FlaskWebsite/website/templates/groups_copy.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends "base.html" %} {% block title %}Group Channels{% endblock %} {% block content
%}
<h1 align="center">Groups</h1>

<form method="POST">
<ul class="list-group list-group-flush" id="groups">
{% for group in groups %}
<li class="list-group-item">
<a href="{{ url_for('views.group_page', group_id=group.id) }}">{{ group.name }}</a>
<input type="text" id="group_key" class="form-control" placeholder="Enter group key">
<button class="btn btn-primary" onclick="join_group('{{ group.id }}', document.getElementById('group_key').value)">Join Group</button>
<!-- <button class="btn btn-secondary" disabled>Join Group</button> -->
</li>
{% endfor %}
</ul>
<input type="text" name="group_name" id="group_name" class="form-control" placeholder="Enter group name">
<br />
<input type="text" name="group_key" id="group_key" class="form-control" placeholder="Enter group key">
<br />
<div align="center">
<button type="submit" class="btn btn-primary">Add Group</button>
</div>
</form>
{% endblock %}
76 changes: 70 additions & 6 deletions service/src/FlaskWebsite/website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,104 @@ 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/<int:group_id>', methods=['GET', 'POST'])
@login_required
def group_page(group_id):
group_allusers = db.session.query(NoteGroup).filter_by(id=group_id).all()
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/<int:group_id>/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
Expand Down

0 comments on commit 01dab21

Please sign in to comment.