def get_subjects(self): payload = [] count = 0 try: subjects_to_find = self.requests.get("subjects", None) with DBConnection() as session: if len(subjects_to_find): for subject in subjects_to_find: query = session.query(Subjects).filter( Subjects.subject_id == subject) data = query.all() if data: payload1, message, count = get_subject_payload( data, count) payload.append(payload1[0]) else: payload.append({ "subject_id": subject, "message": "Subject doesn't exists." }) message = str(count) + " subjects fetched." else: query = session.query(Subjects).order_by( Subjects.subject_code) data = query.all() payload, message, count = get_subject_payload(data, count) except Exception as e: print(e) raise e return payload, message
def get_admin(self): payload = [] count = 0 message = "" try: admin_to_get = self.requests.get("admins", None) with DBConnection() as session: if len(admin_to_get): for admin in admin_to_get: query = session.query(Admin).filter( Admin.admin_id == admin) data = query.all() if data: payload1, message, count = get_admin_payload( data, count) payload.append(payload1[0]) else: payload.append({ "admin_id": admin, "message": "Admin doesn't exists." }) else: query = session.query(Admin) data = query.all() payload, message, count = get_admin_payload(data, count) except Exception as e: print(e) raise e return payload, message
def get_sem_subjects(self): payload = [] count = 0 try: subjects_to_find = self.requests.get("subjects", None) with DBConnection() as session: for subject in subjects_to_find: query = session.query(Subjects).filter(Subjects.branch_id == subject["branch_id"])\ .filter(Subjects.sem == subject["sem"]) data = query.all() if data: payload1, message, count = get_subject_payload( data, count) payload = payload1 else: payload.append({ "branch_id": subject["branch_id"], "sem": subject["sem"], "message": "Subject doesn't exists." }) message = str(count) + " subject fetched." except Exception as e: print(e) raise e return payload, message
def delete_admin(self): payload = [] count = 0 try: admin_to_delete = self.requests.get("admins", None) with DBConnection() as session: for admin in admin_to_delete: query = session.query(Admin).filter( Admin.admin_id == admin).delete( synchronize_session=False) if query: count += 1 payload.append({ "admin_id": admin, "message": "Deleted successfully." }) session.commit() else: payload.append({ "admin_id": admin, "message": "Admin doesn't exists." }) except Exception as e: print(e) raise e return payload, str(count) + " admin deleted."
def delete_faculties(self): payload = [] count = 0 try: faculties_to_delete = self.requests.get('faculties', None) with DBConnection() as session: for faculty in faculties_to_delete: query = session.query(Faculties.faculty_id).filter(Faculties.faculty_id == faculty) \ .delete(synchronize_session=False) if query: count += 1 payload.append({ "faculty_id": faculty, "message": "Faculty deleted successfully." }) session.commit() else: payload.append({ "faculty_id": faculty, "message": "Faculty doesn't exists." }) except Exception as e: print(e) raise e return payload, str(count) + " faculty deleted."
def get_subject_payload(data, count): try: payload = [] for subject in data: with DBConnection() as session: try: query = session.query(Branches).filter( Branches.branch_id == subject.branch_id) data1 = query.all() if data1: for branch in data1: branch_name = branch.branch_name except Exception as e: print(e) raise e new_subject = { "subject_id": subject.subject_id, "subject_name": subject.subject_name, "subject_code": subject.subject_code, "branch_id": branch_name, "sem": subject.sem, "year": subject.year, } payload.append(new_subject) count += 1 except Exception as e: print(e) raise e return payload, str(count) + " subject fetched.", count
def delete_document(self): payload = [] count = 0 try: document_to_delete = self.requests.get('document', None) with DBConnection() as session: for document in document_to_delete: query = session.query(Documents).filter( Documents.document_id == document) data = query.all() query = session.query(Documents).filter(Documents.document_id == document) \ .delete(synchronize_session=False) if query: count += 1 path_ = os.getcwd( ) + "/media/" + data[0].document_url.split("/")[4] os.remove(path_) payload.append({ "document_id": document, "message": "Document deleted successfully." }) session.commit() else: payload.append({ "document_id": document, "message": "Document doesn't exists." }) except Exception as e: print(e) raise e return payload, str(count) + " document deleted."
def get_faculties(self): payload = [] count = 0 message = "" try: faculties_to_find = self.requests.get("faculties", None) with DBConnection() as session: if len(faculties_to_find): for faculty in faculties_to_find: query = session.query(Faculties).filter( Faculties.faculty_id == faculty) data = query.all() if data: payload1, message, count = get_faculty_payload( data, count) payload.append(payload1[0]) else: payload.append({ "faculty_id": faculty, "message": "Faculty doesn't exists." }) else: query = session.query(Faculties) data = query.all() payload, message, count = get_faculty_payload(data, count) except Exception as e: print(e) raise e return payload, message
def get_student_payload(data, count): payload = [] try: for student in data: with DBConnection() as session: try: query = session.query(Branches).filter( Branches.branch_id == student.branch_id) data1 = query.all() if data1: for branch in data1: branch_name = branch.branch_name except Exception as e: print(e) raise e new_user = { "student_id": student.student_id, "name": student.name, "usn": student.usn, "email": student.email, "branch": branch_name, "sem": student.sem, "year": student.year, } payload.append(new_user) count += 1 except Exception as e: print(e) raise e return payload, str(count) + " student fetched.", count
def update_branches(self): payload = [] count = 0 try: branches_to_update = self.requests.get("branches", None) with DBConnection() as session: for branch in branches_to_update: columns_to_update = { Branches.branch_name: branch["update_data"]["branch_name"] } try: query = session.query(Branches).filter(Branches.branch_id == branch['branch_id']) \ .update(columns_to_update, synchronize_session=False) session.commit() if query: count += 1 payload.append({"branch_id": branch['branch_id'], "message": "Branch updated successfully."}) else: payload.append({"branch_id": branch['branch_id'], "message": "Branch doesn't exist."}) except SQLAlchemyError as e: print(e) payload.append( {"branch_id": branch['branch_id'], "message": str(e._message).split(": ")[1].split("\\")[0]}) session.rollback() except Exception as e: print(e) raise e return payload, str(count) + " branch updated."
def get_document(self): payload = [] count = 0 try: document_to_find = self.requests.get("document", None) with DBConnection() as session: if len(document_to_find): for document in document_to_find: query = session.query(Documents).filter( Documents.document_id == document) data = query.all() if data: payload1, message, count = get_document_payload( data, count) payload.append(payload1[0]) else: payload.append({ "document_id": document, "message": "Document doesn't exists." }) message = str(count) + " document fetched." else: query = session.query(Documents).order_by( Documents.document_title) data = query.all() payload, message, count = get_document_payload(data, count) except Exception as e: print(e) raise e return payload, message
def get_faculty_payload(data, count): payload = [] try: for faculty in data: with DBConnection() as session: try: query = session.query(Branches).filter( Branches.branch_id == faculty.branch_id) data1 = query.all() if data1: for branch in data1: branch_name = branch.branch_name except Exception as e: print(e) raise e new_user = { "faculty_id": faculty.faculty_id, "name": faculty.name, "email": faculty.email, "branch_name": branch_name } payload.append(new_user) count += 1 except Exception as e: print(e) raise e return payload, str(count) + " faculty fetched.", count
def update_subject(self): payload =[] count = 0 try: subject_to_update = self.requests.get("subject", None) with DBConnection() as session: for subject in subject_to_update: columns_to_update = {} for key,value in subject["update_data"].items(): columns_to_update[subject_columns[key]] = value columns_to_update[Subject.modified_by] = subject['subject_id'] columns_to_update[Subject.modified_on] = datetime.now() try: query = session.query(Subject).filter(Subject.subject_id == subject['subject_id']) \ .update(columns_to_update, synchronize_session=False) session.commit() if query: count += 1 payload.append({"subject_id": subject['subject_id'], "message": "Updated successfully."}) else: payload.append({"subject_id": subject['subject_id'], "message": "Subject doesn't exist."}) except SQLAlchemyError as e: print(e) payload.append( {"subject_id": subject['subject_id'], "message": str(e._message).split("Key (")[1].split(")")[0] + " already exists."}) session.rollback() except Exception as e: print(e) raise e return payload, str(count) + " subject updated."
def create_subject(self): payload = [] count = 0 try: subjects_to_create = self.requests.get("subjects", None) with DBConnection() as session: for subject in subjects_to_create: _id = str(uuid.uuid4()) try: new_user = Subject( subject_id=_id, subject_name=subject['subject_name'], subject_code=subject['subject_code'].lower(), sem=subject['sem'], year=subject['year'], branch_id=subject['branch_id'], created_by=_id, created_on=datetime.now(), modified_by=_id, modified_on=datetime.now(), ) session.add(new_user) session.commit() payload.append({"subject_id": _id, "message": "Subject added successfully."}) count += 1 except SQLAlchemyError as e: print(e) payload.append({"subject_id": _id, "message": str(e._message).split(": ")[1].split("\\")[0]}) session.rollback() except Exception as e: print(e) raise e finally: return payload, str(count) + " subjects created."
def delete_subjects(self): payload = [] count = 0 try: subjects_to_delete = self.requests.get("subjects", None) with DBConnection() as session: for subject in subjects_to_delete: query = session.query(Subjects).filter( Subjects.subject_id == subject).delete( synchronize_session=False) if query: count += 1 payload.append({ "subject_id": subject, "message": "Subject deleted successfully." }) session.commit() else: payload.append({ "subject_id": subject, "message": "Subject doesn't exists." }) except Exception as e: print(e) raise e return payload, str(count) + " subject deleted."
def get_admin_payload(data, count): try: payload = [] for admin in data: with DBConnection() as session: try: query = session.query(Branches).filter( Branches.branch_id == admin.branch_id) data1 = query.all() if data1: for branch in data1: branch_name = branch.branch_name except Exception as e: print(e) raise e new_user = { "admin_id": admin.admin_id, "name": admin.name, "email": admin.email, "branch": branch_name, } payload.append(new_user) count += 1 except Exception as e: print(e) raise e return payload, str(count) + " admin fetched.", count
def get_branches(self): payload = [] count = 0 try: branches_to_find = self.requests.get("branches", None) with DBConnection() as session: if len(branches_to_find): for branch in branches_to_find: query = session.query(Branches).filter( Branches.branch_id == branch) data = query.all() if data: payload1, message, count = get_branches_payload( data, count) payload.append(payload1[0]) else: payload.append({ "branch_id": branch, "message": "Branch doesn't exists." }) message = str(count) + " branch fetched." else: query = session.query(Branches) data = query.all() payload, message, count = get_branches_payload(data, count) except Exception as e: print(e) raise e return payload, message
def create_branches(self): payload = [] count = 0 try: branches_to_create = self.requests.get("branches", None) with DBConnection() as session: for branch in branches_to_create: _id = str(uuid.uuid4()) try: new_branch = Branches( branch_id=_id, branch_name=branch['branch_name']) session.add(new_branch) session.commit() payload.append({ "branch_id": _id, "message": "Branch added successfully." }) count += 1 except SQLAlchemyError as e: print(e) payload.append({ "branch_id": _id, "message": str(e._message).split(": ")[1].split("\\")[0] }) session.rollback() except Exception as e: print(e) raise e finally: return payload, str(count) + " branches created."
def delete_branches(self): payload = [] count = 0 try: branches_to_delete = self.requests.get('branches', None) with DBConnection() as session: for branch in branches_to_delete: query = session.query(Branches).filter(Branches.branch_id == branch) \ .delete(synchronize_session=False) if query: count += 1 payload.append({ "branch_id": branch, "message": "Branch deleted successfully." }) session.commit() else: payload.append({ "branch_id": branch, "message": "Branch doesn't exists." }) except Exception as e: print(e) raise e return payload, str(count) + " branch deleted."
def get_student(self): payload = [] count = 0 message = "" try: student_to_find = self.requests.get("students", None) with DBConnection() as session: if len(student_to_find): for student in student_to_find: query = session.query(Student).filter( Student.student_id == student) data = query.all() if data: payload1, message, count = get_student_payload( data, count) payload.append(payload1[0]) else: payload.append({ "student_id": student, "message": "Student doesn't exists." }) else: query = session.query(Student) data = query.all() payload, message, count = get_student_payload(data, count) except Exception as e: print(e) raise e return payload, message
def update_students(self): payload = [] count = 0 try: students_to_update = self.requests.get("students", None) with DBConnection() as session: for student in students_to_update: columns_to_update = {} for key, value in student["update_data"].items(): if key == "password": value = pbkdf2_sha256.encrypt(value, rounds=1200, salt_size=32) columns_to_update[student_columns[key]] = value columns_to_update[student_columns[key]] = value columns_to_update[ Students.modified_by] = student['student_id'] columns_to_update[Students.modified_on] = datetime.now() try: query = session.query(Students).filter(Students.student_id == student['student_id']) \ .update(columns_to_update, synchronize_session=False) session.commit() if query: count += 1 payload.append({ "student_id": student['student_id'], "message": "Student updated successfully." }) else: payload.append({ "student_id": student['student_id'], "message": "Student doesn't exist." }) except SQLAlchemyError as e: print(e) payload.append({ "student_id": student['student_id'], "message": str(e._message).split(": ")[1].split("\\")[0] }) session.rollback() except Exception as e: print(e) raise e return payload, str(count) + " student updated."
def create_student(self): payload = [] count = 0 try: students_to_create = self.requests.get("students", None) with DBConnection() as session: for student in students_to_create: _id = str(uuid.uuid4()) try: new_user = Student( student_id=_id, name=student['name'], usn=student['usn'].lower(), email=student['email'], password=pbkdf2_sha256.encrypt(student['password'], rounds=1200, salt_size=32), sem=student['sem'], year=student['year'], branch_id=student['branch_id'], created_by=_id, created_on=datetime.now(), modified_by=_id, modified_on=datetime.now(), ) session.add(new_user) session.commit() payload.append({ "student_id": _id, "message": "Student added successfully." }) count += 1 except SQLAlchemyError as e: print(e) payload.append({ "student_id": _id, "message": str(e._message).split(": ")[1].split("\\")[0] }) session.rollback() except Exception as e: print(e) raise e finally: return payload, str(count) + " students created."
def update_admin(self): payload = [] count = 0 try: admin_to_update = self.requests.get("admins", None) with DBConnection() as session: for admin in admin_to_update: columns_to_update = {} for key, value in admin["update_data"].items(): if key == "password": value = pbkdf2_sha256.encrypt(value, rounds=1200, salt_size=32) columns_to_update[admin_columns[key]] = value columns_to_update[admin_columns[key]] = value columns_to_update[ Admin.modified_by] = admin['admin_id'] columns_to_update[Admin.modified_on] = datetime.now() try: query = session.query(Admin).filter(Admin.admin_id == admin['admin_id']) \ .update(columns_to_update, synchronize_session=False) session.commit() if query: count += 1 payload.append({ "admin_id": admin['admin_id'], "message": "Updated successfully." }) else: payload.append({ "admin_id": admin['admin_id'], "message": "Faculty doesn't exist." }) except SQLAlchemyError as e: print(e) payload.append({ "admin_id": admin['admin_id'], "message": str(e._message) }) session.rollback() except Exception as e: print(e) raise e return payload, str(count) + " Faculty updated."
def update_subjects(self): payload = [] count = 0 try: subjects_to_update = self.requests.get("subjects", None) with DBConnection() as session: for subject in subjects_to_update: columns_to_update = {} for key, value in subject["update_data"].items(): if key == 'subject_code': val = value.upper() columns_to_update[subject_columns[key]] = val else: columns_to_update[subject_columns[key]] = value try: query = session.query(Subjects).filter(Subjects.subject_id == subject['subject_id']) \ .update(columns_to_update, synchronize_session=False) session.commit() if query: count += 1 payload.append({ "subject_id": subject['subject_id'], "message": "Subject updated successfully." }) else: payload.append({ "subject_id": subject['subject_id'], "message": "Subject doesn't exist." }) except SQLAlchemyError as e: print(e) payload.append({ "subject_id": subject['subject_id'], "message": str(e._message).split(": ")[1].split("\\")[0] }) session.rollback() except Exception as e: print(e) raise e return payload, str(count) + " subject updated."
def update_document(self): payload = [] count = 0 try: document_to_update = self.requests.get("document", None) with DBConnection() as session: for document in document_to_update: columns_to_update = {} for key, value in document["update_data"].items(): columns_to_update[document_columns[key]] = value try: query = session.query(Documents).filter(Documents.document_id == document['document_id']) \ .update(columns_to_update, synchronize_session=False) session.commit() if query: count += 1 payload.append({ "document_id": document['document_id'], "message": "Document updated successfully." }) else: payload.append({ "document_id": document['document_id'], "message": "Document doesn't exist." }) except SQLAlchemyError as e: print(e) payload.append({ "document_id": document['document_id'], "message": str(e._message).split(": ")[1].split("\\")[0] }) session.rollback() except Exception as e: print(e) raise e return payload, str(count) + " document updated."
def create_admin(self): payload = [] count = 0 try: admin_to_create = self.requests.get("admins", None) with DBConnection() as session: for admin in admin_to_create: _id = str(uuid.uuid4()) try: new_user = Admin( admin_id=_id, name=admin['name'], email=admin['email'], password=pbkdf2_sha256.encrypt(admin['password'], rounds=1200, salt_size=32), branch_id=admin['branch_id'], created_by=_id, created_on=datetime.now(), modified_by=_id, modified_on=datetime.now(), ) session.add(new_user) session.commit() payload.append({ "admin_id": _id, "message": "Admin added successfully." }) count += 1 except SQLAlchemyError as e: print(e) payload.append({ "admin_id": _id, "message": str(e._message).split(": ")[1].split("\\")[0] }) session.rollback() except Exception as e: print(e) raise e finally: return payload, str(count) + " admins created."
def get_document_payload(data, count): try: payload = [] for document in data: with DBConnection() as session: try: query = session.query(Subjects).filter( Subjects.subject_id == document.subject_id) data1 = query.all() if data1: for subject in data1: subject_name = subject.subject_name subject_code = subject.subject_code sem = subject.sem year = subject.year branch_id = subject.branch_id query1 = session.query(Branches).filter( Branches.branch_id == branch_id) data2 = query1.all() if data1: for branch in data2: branch_name = branch.branch_name except Exception as e: print(e) raise e new_document = { "document_id": document.document_id, "document_title": document.document_title, "document_url": document.document_url, "module": document.module, "branch_name": branch_name, "sem": sem, "year": year, "subject_name": subject_name, "subject_code": subject_code } payload.append(new_document) count += 1 except Exception as e: print(e) raise e return payload, str(count) + " document fetched.", count
def login_student(self): payload = [] message = "" student = self.requests.get("student", None) try: with DBConnection() as session: try: query = session.query(Students.student_id, Students.usn, Students.password) \ .filter(Students.usn == student[0]["usn"].lower()) data = query.all() if data: if pbkdf2_sha256.verify(student[0]["password"], data[0][2]): payload.append({"student_id": data[0][0]}) message = "Logged in successfully." except Exception as e: print(e) raise e except Exception as e: print(e) raise e return payload, message
def login_faculty(self): payload = [] message = "" faculty = self.requests.get("faculty", None) try: with DBConnection() as session: try: query = session.query(Faculties.faculty_id, Faculties.email, Faculties.password) \ .filter(Faculties.email == faculty[0]["email"]) data = query.all() if data: if pbkdf2_sha256.verify(faculty[0]["password"], data[0][2]): payload.append({"faculty_id": data[0][0]}) message = "Logged in successfully." except Exception as e: print(e) raise e except Exception as e: print(e) raise e return payload, message
def create_document(self): payload = [] count = 0 try: document_to_create = self.requests.get("document", None) with DBConnection() as session: for document in document_to_create: _id = str(uuid.uuid4()) try: new_document = Documents( document_id=_id, document_title=document['document_title'], document_url=document['document_url'], subject_id=document['subject_id'], module=document['module']) session.add(new_document) session.commit() payload.append({ "document_id": _id, "message": "Document added successfully." }) count += 1 except SQLAlchemyError as e: print(e) payload.append({ "document_id": _id, "message": str(e._message).split(": ")[1].split("\\")[0] }) session.rollback() except Exception as e: print(e) raise e finally: return payload, str(count) + " document created."