Skip to content

Commit

Permalink
section, group, channel initial setup
Browse files Browse the repository at this point in the history
  • Loading branch information
MaryamAbdul-Aziz committed Dec 11, 2024
1 parent ccc26e3 commit dc1c7e2
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
53 changes: 51 additions & 2 deletions api/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from flask import Blueprint, request, jsonify, current_app, Response, g
from flask_restful import Api, Resource # used for REST API building
from datetime import datetime
from __init__ import app
from sqlite3 import IntegrityError
from __init__ import app, db
from api.jwt_authorize import token_required
from model.channel import Channel
from model.group import Group
Expand Down Expand Up @@ -220,4 +221,52 @@ def post(self):
api.add_resource(_CRUD, '/channel')
api.add_resource(_BULK_CRUD, '/channels')
api.add_resource(_BULK_FILTER, '/channels/filter')
api.add_resource(_FILTER, '/channel/filter')
api.add_resource(_FILTER, '/channel/filter')

def initChannels():
with app.app_context():
db.create_all()
classics = Group.query.filter_by(_name='Classics').first()
fantasy = Group.query.filter_by(_name='Fantasy').first()
nonfiction = Group.query.filter_by(_name='Nonfiction').first()
histfic = Group.query.filter_by(_name='Historical Fiction').first()
suspense = Group.query.filter_by(_name='Suspense/Thriller').first()
romance = Group.query.filter_by(_name='Romance').first()
dystopian = Group.query.filter_by(_name='Dystopian').first()
mystery = Group.query.filter_by(_name='Mystery').first()

bookworm_channels = [
Channel(name='Book Recommender', group_id=classics.id),
Channel(name='Chatroom', group_id=classics.id),

Channel(name='Book Recommender', group_id=fantasy.id),
Channel(name='Chatroom', group_id=fantasy.id),

Channel(name='Book Recommender', group_id=nonfiction.id),
Channel(name='Chatroom', group_id=nonfiction.id),

Channel(name='Book Recommender', group_id=histfic.id),
Channel(name='Chatroom', group_id=histfic.id),

Channel(name='Book Recommender', group_id=suspense.id),
Channel(name='Chatroom', group_id=suspense.id),

Channel(name='Book Recommender', group_id=romance.id),
Channel(name='Chatroom', group_id=romance.id),

Channel(name='Book Recommender', group_id=dystopian.id),
Channel(name='Chatroom', group_id=dystopian.id),

Channel(name='Book Recommender', group_id=mystery.id),
Channel(name='Chatroom', group_id=mystery.id),
]

channels = bookworm_channels
for channel in channels:
try:
db.session.add(channel)
db.session.commit()
print(f"Record created: {repr(channel)}")
except IntegrityError:
db.session.rollback()
print(f"Records exist, duplicate email, or error: {channel.name}")
19 changes: 17 additions & 2 deletions api/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask import Blueprint, request, jsonify, current_app, Response, g
from flask_restful import Api, Resource # used for REST API building
from datetime import datetime
from __init__ import app
from __init__ import app, db
from api.jwt_authorize import token_required
from model.group import Group
from model.user import User
Expand Down Expand Up @@ -251,4 +251,19 @@ def post(self):
api.add_resource(_CRUD, '/group')
api.add_resource(_BULK_CRUD, '/groups')
api.add_resource(_BULK_FILTER, '/groups/filter')
api.add_resource(_FILTER, '/group/filter')
api.add_resource(_FILTER, '/group/filter')

def initGroups():
with app.app_context():
db.create_all()
bookworm_section = Section.query.filter_by(_name='Bookworms').first()
group = [
Group(name='Classics', section_id=bookworm_section.id, moderators=[User.query.get(1)]),
Group(name='Fantasy', section_id=bookworm_section.id, moderators=[User.query.get(1)]),
Group(name='Nonfiction', section_id=bookworm_section.id, moderators=[User.query.get(1)]),
Group(name='Historical Fiction', section_id=bookworm_section.id, moderators=[User.query.get(1)]),
Group(name='Suspense/Thriller', section_id=bookworm_section.id, moderators=[User.query.get(1)]),
Group(name='Romance', section_id=bookworm_section.id, moderators=[User.query.get(1)]),
Group(name='Dystopian', section_id=bookworm_section.id, moderators=[User.query.get(1)]),
Group(name='Mystery', section_id=bookworm_section.id, moderators=[User.query.get(1)]),
]
20 changes: 18 additions & 2 deletions api/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
from flask import Blueprint, request, jsonify, current_app, Response, g
from flask_restful import Api, Resource # used for REST API building
from datetime import datetime
from __init__ import app
from __init__ import app, db
from api.jwt_authorize import token_required
from model.section import Section
from sqlite3 import IntegrityError


"""
This Blueprint object is used to define APIs for the Section model.
Expand Down Expand Up @@ -155,4 +157,18 @@ def get(self):
- The _BULK_CRUD class defines the bulk operations for the API.
"""
api.add_resource(_CRUD, '/section')
api.add_resource(_BULK_CRUD, '/sections')
api.add_resource(_BULK_CRUD, '/sections')

def initSections():
with app.app_context():
db.create_all()
s1 = Section(name='Bookworms')
sections = [s1]

for section in sections:
try:
section.create()
except IntegrityError:
'''fails with bad or duplicate data'''
db.session.remove()
print(f"Records exist, duplicate email, or error: {section._name}")

0 comments on commit dc1c7e2

Please sign in to comment.