Пример #1
0
    def put(self):
        '''
		'''
        try:
            args_data = request.args.to_dict()
            post_data = request.get_json()
            print(args_data)
            print(post_data)
            user = args_data.get("user")
            # user = args_data.get("user", "all")
            # print(f'condition: {(not user or post_data)}')

            if not user or not post_data:
                response = {
                    "meta": self.meta,
                    "message": "unable to process request",
                    "status": "failure",
                }
                return response, self.bad_code, self.headers

            users_data.load(post_data, partial=True)

            columns = {"_id": 0}
            queries = {"_id": ObjectId(user)}
            collection = 'common_user_master'
            user_data = FlaskMongo.find(collection, columns, **queries)

            if user_data == []:
                response = {
                    "meta": self.meta,
                    "message": f"user {user} does not exists",
                    "status": "failure",
                }
                return response, self.bad_code, self.headers

            updates = post_data
            queries = {"_id": ObjectId(user)}
            collection = 'common_user_master'
            FlaskMongo.update(collection, updates, **queries)

            response = {
                "meta": self.meta,
                "message": f"user {user} updated successfully",
                "status": "success"
            }
            return response, self.success_code, self.headers

        except Exception as e:
            # raise e
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "reason": str(e)
            }
            return response, self.exception_code, self.headers
Пример #2
0
    def post(self):
        """
		"""
        try:
            post_data = request.get_json()

            # print(post_data)

            testdeletion_data.load(post_data)

            # Check for already exising entry:

            collection1 = 'common_test_master'
            collection2 = 'common_question_master'

            test_id = post_data.get('test_id')
            columns1 = {"_id": 0, "deleted": 0}
            queries1 = {"id": test_id}
            test_data = FlaskMongo.find(collection1, columns1, queries1)

            if not test_data:
                response = {
                    "meta": self.meta,
                    "message": f"test with id {test_id} does not exists",
                    "status": "failure"
                }
                FlaskLogger.log('delete',
                                'undo_del_tests',
                                response,
                                input_data=str(args_data),
                                log_level='info')
                return response, self.bad_code, self.headers
            else:
                queries1 = {"id": test_id}
                updates1 = {"deleted": 0}
                FlaskMongo.update(collection1, updates1, queries1)

                collection2 = 'common_question_master'
                updates2 = {"deleted": 0}
                queries2 = {"testid": test_id}
                FlaskMongo.update(collection2, updates2, queries2)

                response = {
                    "meta": self.meta,
                    "message":
                    f"test with id {test_id} undeleted successfully",
                    "status": "success"
                }
                FlaskLogger.log('delete',
                                'undo_del_tests',
                                response,
                                input_data=str(args_data),
                                log_level='info')
                return response, self.success_code, self.headers

        except ValidationError as e:
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "reason": format_api_error(e.messages)
            }
            FlaskLogger.log('delete',
                            'undo_del_tests',
                            response,
                            input_data=str(args_data),
                            log_level='error')
            return response, self.bad_code, self.headers

        except Exception as e:
            # raise e
            print(e)
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "reason": str(e)
            }
            FlaskLogger.log('delete',
                            'undo_del_tests',
                            response,
                            input_data=str(args_data),
                            log_level='warning')
            return response, self.exception_code, self.headers
Пример #3
0
    def delete(self, **kwargs):
        '''
		'''
        try:
            args_data = request.args.to_dict()
            # testquestion_data.load(args_data, partial=True)

            test_id = args_data.get("testid")
            queries = {'id': test_id, "deleted": 0}
            columns = {'_id': 0}
            collection1 = 'common_test_master'
            test_data = FlaskMongo.find(collection1, columns, queries)

            if not test_data:
                response = {
                    "meta": self.meta,
                    "message": f"test with id {test_id} does not exists",
                    "status": "failure",
                }
                FlaskLogger.log('delete',
                                'del_tests_info',
                                response,
                                input_data=str(args_data),
                                log_level='info')
                return response, self.bad_code, self.headers

            elif test_data:
                test_data = test_data[0]
                if test_data.get('deleted') == 1:
                    response = {
                        "meta": self.meta,
                        "message": f"test with id {test_id} does not exists",
                        "status": "failure",
                    }
                    FlaskLogger.log('delete',
                                    'del_tests_info',
                                    response,
                                    input_data=str(args_data),
                                    log_level='info')
                    return response, self.bad_code, self.headers

            updates1 = {"deleted": 1}
            queries1 = {"id": test_id}
            FlaskMongo.update(collection1, updates1, queries1)

            collection2 = 'common_question_master'
            updates2 = {"deleted": 1}
            queries2 = {"testid": test_id}
            FlaskMongo.update(collection2, updates2, queries2)

            response = {
                "meta": self.meta,
                "message": f"test with id {test_id} has been deleted",
                "status": "success",
            }
            FlaskLogger.log('delete',
                            'del_tests_info',
                            response,
                            input_data=str(args_data),
                            log_level='info')
            return response, self.success_code, self.headers

        except ValidationError as e:
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "reason": format_api_error(e.messages)
            }
            FlaskLogger.log('delete',
                            'del_tests_info',
                            response,
                            input_data=str(args_data),
                            log_level='error')
            return response, self.bad_code, self.headers

        except Exception as e:
            raise e
            print(e)
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "reason": str(e)
            }
            FlaskLogger.log('delete',
                            'del_tests_info',
                            response,
                            input_data=str(args_data),
                            log_level='warning')
            return response, self.exception_code, self.headers
Пример #4
0
    def put(self, **kwargs):
        '''
			Issue: check time validators, works in post request
		'''
        try:
            args_data = request.args.to_dict()
            post_data = request.get_json()

            print(post_data)
            print(args_data)

            args_data.update(post_data)
            testquestion_data.load(post_data, partial=True)

            test_id = args_data.get("testid")
            queries = {'id': test_id, "deleted": 0}
            columns = {'_id': 0}
            collection = 'common_test_master'
            query_data = FlaskMongo.find(collection, columns, queries)

            if not query_data:
                response = {
                    "meta": self.meta,
                    "message": f"test with id {test_id} does not exists",
                    "status": "failure",
                }
                FlaskLogger.log('put',
                                'mod_tests_info',
                                response,
                                input_data=str({
                                    'ad': args_data,
                                    'pd': post_data
                                }),
                                log_level='info')
                return response, self.bad_code, self.headers

            collection1 = 'common_test_master'
            collection2 = 'common_question_master'

            updates1 = {
                "id": test_id,
                "details": post_data.get("details"),
                "schedule": post_data.get("schedule"),
                "duration": post_data.get("duration"),
                "start_time": post_data.get("start_time"),
                "end_time": post_data.get("end_time"),
                "no_mandatory_questions":
                post_data.get("no_mandatory_questions")
            }
            queries1 = {"id": test_id}
            FlaskMongo.update(collection1, updates1, queries1)

            for qna in post_data.get("qna"):
                data2 = {
                    "customerid": post_data.get("customerid"),
                    "testid": test_id,
                    "question": qna.get("question"),
                    "option1": qna.get("options")[0],
                    "option2": qna.get("options")[1],
                    "option3": qna.get("options")[2],
                    "option4": qna.get("options")[3],
                    "answer": qna.get("answer")
                }
                updates2 = data2
                queries2 = {
                    "_id": ObjectId(qna.get("_id")),
                    "testid": test_id,
                    "customerid": data2.get("customerid")
                }
                FlaskMongo.update(collection2, updates2, queries2)

            response = {
                "meta": self.meta,
                "message": f"test with id {test_id} updated successfully",
                "status": "success"
            }
            FlaskLogger.log('put',
                            'mod_tests_info',
                            response,
                            input_data=str({
                                'ad': args_data,
                                'pd': post_data
                            }),
                            log_level='info')
            return response, self.success_code, self.headers

        except ValidationError as e:
            print(f'exception: {e}\n')
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "reason": format_api_error(e.messages)
            }
            FlaskLogger.log('put',
                            'mod_tests_info',
                            response,
                            input_data=str({
                                'ad': args_data,
                                'pd': post_data
                            }),
                            log_level='error')
            return response, self.bad_code, self.headers

        except Exception as e:
            # raise e
            print(e)
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "reason": str(e)
            }
            FlaskLogger.log('put',
                            'mod_tests_info',
                            response,
                            input_data=str({
                                'ad': args_data,
                                'pd': post_data
                            }),
                            log_level='warning')
            return response, self.exception_code, self.headers
Пример #5
0
    def post(self, **kwargs):
        """
		"""
        try:
            args_data = request.args.to_dict()
            post_data = request.get_json()

            # print(post_data)
            print(f'kwargs: {kwargs}')

            testquestion_data.load(post_data)

            # Check for already exising entry:

            db = c_app.config.get('MONGO_DATABASE')
            collection1 = 'common_test_master'
            collection2 = 'common_question_master'
            collection3 = 'common_user_master'

            customer_id = post_data.get('customerid')
            room_name = post_data.get('room_name')

            columns = {"_id": 0, "deleted": 0}
            queries = {"_id": ObjectId(customer_id)}
            user_data = FlaskMongo.find(collection3, columns, queries)

            if not user_data:
                response = {
                    "meta": self.meta,
                    "message": f"user with id {customer_id} does not exists",
                    "status": "failure"
                }
                FlaskLogger.log('post',
                                'add_tests_info',
                                response,
                                input_data=str(post_data),
                                log_level='info')
                return response, self.bad_code, self.headers

            elif user_data[0].get("account_type") != 'teacher':
                response = {
                    "meta": self.meta,
                    "message":
                    f"user with id {customer_id} is not allowed to create tests",
                    "status": "failure"
                }
                FlaskLogger.log('post',
                                'add_tests_info',
                                response,
                                input_data=str(post_data),
                                log_level='info')
                return response, self.process_error_code, self.headers

            elif user_data[0].get("no_free_trial") < 1:
                response = {
                    "meta": self.meta,
                    "message":
                    f"user with id {customer_id} has 0 trials left. Please renew subscription.",
                    "status": "failure"
                }
                FlaskLogger.log('post',
                                'add_tests_info',
                                response,
                                input_data=str(post_data),
                                log_level='info')
                return response, self.process_error_code, self.headers

            else:
                testid = str(uuid.uuid1()).replace("-", "")
                teacher_id = kwargs.get("_id")

                data1 = {
                    "id":
                    testid,
                    "teacher_id":
                    teacher_id,
                    "room_name":
                    room_name,
                    "details":
                    post_data.get("details"),
                    "schedule":
                    post_data.get("schedule"),
                    "duration":
                    post_data.get("duration"),
                    "start_time":
                    post_data.get("start_time"),
                    "end_time":
                    post_data.get("end_time"),
                    "no_mandatory_questions":
                    post_data.get("no_mandatory_questions"),
                    "deleted":
                    0
                }
                FlaskMongo.insert(db, collection1, data1)

                for qna in post_data.get("qna"):
                    data2 = {
                        "customerid": post_data.get("customerid"),
                        "testid": testid,
                        "question": qna.get("question"),
                        "option1": qna.get("options")[0],
                        "option2": qna.get("options")[1],
                        "option3": qna.get("options")[2],
                        "option4": qna.get("options")[3],
                        "answer": qna.get("answer"),
                        "deleted": 0
                    }
                    FlaskMongo.insert(db, collection2, data2)

                no_free_trial = user_data[0].get("no_free_trial")
                if no_free_trial > 0:
                    no_free_trial = no_free_trial - 1

                    queries = {"_id": ObjectId(customer_id)}
                    updates = {"no_free_trial": no_free_trial}
                    FlaskMongo.update(collection3, updates, queries)

                response = {
                    "meta": self.meta,
                    "message":
                    f"new test with id {testid} created successfully",
                    "status": "success"
                }
                FlaskLogger.log('post',
                                'add_tests_info',
                                response,
                                input_data=str(post_data),
                                log_level='info')
                return response, self.success_code, self.headers

        except ValidationError as e:
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "reason": format_api_error(e.messages)
            }
            FlaskLogger.log('post',
                            'add_tests_info',
                            response,
                            input_data=str(post_data),
                            log_level='error')
            return response, self.bad_code, self.headers

        except Exception as e:
            # raise e
            # print(e)
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "reason": str(e)
            }
            FlaskLogger.log('post',
                            'add_tests_info',
                            response,
                            input_data=str(post_data),
                            log_level='warning')
            return response, self.exception_code, self.headers
Пример #6
0
    def put(self):
        '''
		'''
        try:
            args_data = request.args.to_dict()
            post_data = request.get_json()
            post_data.update(args_data)

            testattempt_data.load(post_data, partial=True)

            # Check for already exising entry:

            db = c_app.config.get('MONGO_DATABASE')
            collection1 = 'common_test_student'
            collection2 = 'common_test_student_ques_answers'
            test_id = args_data.get("test_id")
            student_id = args_data.get("student_id")

            columns = {"_id": 0}
            queries = {
                "test_id": test_id,
                "student_id": student_id  #, "is_complete": True
            }
            test_data = FlaskMongo.find(collection1, columns, queries)

            if not test_data:
                response = {
                    "meta":
                    self.meta,
                    "message":
                    "unable to process request",
                    "status":
                    "failure",
                    "reason":
                    f"No test with id {test_id} found for student id {student_id}"
                }
                FlaskLogger.log('put',
                                'mod_test_complete_info',
                                response,
                                input_data=str(args_data, post_data),
                                log_level='info')
                return response, self.bad_code, self.headers
            else:
                updates1 = {'is_complete': post_data.get('is_complete')}
                queries1 = {"student_id": student_id, "test_id": test_id}
                FlaskMongo.update(collection1, updates1, queries1)

                response = {
                    "meta": self.meta,
                    "message":
                    f"Test with id {test_id} updated for student id {student_id}",
                    "status": "success"
                }
                FlaskLogger.log('put',
                                'mod_test_complete_info',
                                response,
                                input_data=str(args_data, post_data),
                                log_level='info')
                return response, self.success_code, self.headers

        except ValidationError as e:
            # raise e
            # print(e)
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "errors": format_api_error(e.messages)
            }
            FlaskLogger.log('put',
                            'mod_test_complete_info',
                            response,
                            input_data=str(args_data, post_data),
                            log_level='error')
            return response, self.bad_code, self.headers

        except Exception as e:
            # raise e
            print(e)
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "reason": str(e)
            }
            FlaskLogger.log('put',
                            'mod_test_complete_info',
                            response,
                            input_data=str(args_data, post_data),
                            log_level='warning')
            return response, self.exception_code, self.headers
Пример #7
0
    def put(self):
        '''
			A Test Attempt can be new or redo
		'''
        try:
            args_data = request.args.to_dict()
            post_data = request.get_json()
            post_data.update(args_data)

            print(post_data)

            testattempt_data.load(post_data, partial=True)
            student_name = post_data.get("student_name")
            phone_no = post_data.get("phone_no")
            is_complete = post_data.get("is_complete")

            print(post_data.keys())

            # Check for already exising entry:

            db = c_app.config.get('MONGO_DATABASE')
            collection1 = 'common_test_student'
            collection2 = 'common_test_student_ques_answers'
            test_id = args_data.get("test_id")
            student_id = args_data.get("student_id")

            columns = {"_id": 0}
            queries = {
                "test_id": test_id,
                "student_id": student_id  #, "is_complete": True
            }

            print(post_data)

            test_data = FlaskMongo.find(collection1, columns, queries)
            test_qna_data = FlaskMongo.find(collection2, columns, queries)

            print(post_data)

            if not test_data and not test_qna_data:
                response = {
                    "meta": self.meta,
                    "message": f"No data found for Test with {test_id}",
                    "status": "failure",
                }
                FlaskLogger.log('put',
                                'mod_test_attempt_info',
                                response,
                                input_data=str(args_data, post_data),
                                log_level='info')
                return response, self.bad_code, self.headers
            else:
                queries1 = {"student_id": student_id, "test_id": test_id}
                updates1 = {
                    "student_name": student_name,
                    "phone_no": phone_no,
                    # "is_complete": is_complete
                }
                # data1["created"] = datetime.now().isoformat()

                FlaskMongo.update(collection1, updates1, queries1)

                for qa in post_data.get('qna'):
                    queries2 = {
                        "student_id": student_id,
                        "test_id": test_id,
                        "question_id": qa.get("_id"),
                        "question": qa.get("question"),
                    }
                    updates2 = {
                        "answer": qa.get("answer"),
                        "is_correct": qa.get("is_correct")
                    }

                    # Update Question Answers Here:

                    FlaskMongo.update(collection2, updates2, queries2)

            response = {
                "meta": self.meta,
                "message": f"test attempt with {test_id} updated successfully",
                "status": "success"
            }
            FlaskLogger.log('put',
                            'mod_test_attempt_info',
                            response,
                            input_data=str(args_data, post_data),
                            log_level='info')
            return response, self.success_code, self.headers

        except ValidationError as e:
            # raise e
            # print(e)
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "errors": format_api_error(e.messages)
            }
            FlaskLogger.log('put',
                            'mod_test_attempt_info',
                            response,
                            input_data=str(args_data, post_data),
                            log_level='error')
            return response, self.bad_code, self.headers

        except Exception as e:
            # raise e
            print(e)
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "reason": str(e)
            }
            FlaskLogger.log('put',
                            'mod_test_attempt_info',
                            response,
                            input_data=str(args_data, post_data),
                            log_level='warning')
            return response, self.exception_code, self.headers
Пример #8
0
    def post(self):
        '''
		'''
        try:
            args_data = request.args.to_dict()
            post_data = request.get_json()
            reset_trial_data.load(post_data)

            user_id = post_data.get("userid")
            collection = "common_user_master"
            queries = {"_id": ObjectId(user_id)}
            columns = {"_id": 0}

            user_data = FlaskMongo.find(collection, columns, queries)

            if not user_data:
                response = {
                    "meta": self.meta,
                    "message": f"user with id {user_id} does not exists",
                    "status": "failure"
                }
                # FlaskLogger.log('post', 'reset_trial', response, log_level='debug')
                FlaskLogger.log('post',
                                'reset_trial',
                                response,
                                log_level='info')
                # FlaskLogger.log('post', 'reset_trial', response, log_level='warning')
                # FlaskLogger.log('post', 'reset_trial', response, log_level='error')
                # FlaskLogger.log('post', 'reset_trial', response, log_level='critical')
                return response, self.bad_code, self.headers
            else:
                if args_data:
                    no_free_trial = int(args_data.get('no_free_trial'))
                else:
                    no_free_trial = 2

                updates = {"no_free_trial": no_free_trial}
                FlaskMongo.update(collection, updates, queries)

                response = {
                    "meta": self.meta,
                    "message":
                    f"trial for user with id {user_id} has been reset",
                    "status": "success"
                }
                FlaskLogger.log('post',
                                'reset_trial',
                                response,
                                log_level='info')
                return response, self.success_code, self.headers

        except ValidationError as e:
            print(e)
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "reason": format_api_error(e.messages)
            }
            FlaskLogger.log('post', 'reset_trial', response, log_level='error')
            return response, self.bad_code, self.headers

        except Exception as e:
            # raise e
            # print(e)
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "reason": str(e)
            }
            FlaskLogger.log('post',
                            'reset_trial',
                            response,
                            log_level='warning')
            return response, self.exception_code, self.headers
Пример #9
0
    def delete(self, **kwargs):
        '''
		'''
        try:
            args_data = request.args.to_dict()
            post_data = request.get_json()
            print(args_data)
            # print(post_data)

            teacherusers_data.load(args_data, partial=True)
            userid = args_data.get("userid")
            # user = args_data.get("user", "all")
            # print(f'condition: {(not user or post_data)}')

            columns = {"_id": 0}
            queries = {"_id": ObjectId(userid)}
            collection = "common_user_master"

            user_data = FlaskMongo.find(collection, columns, queries)
            print(f'user_data: {user_data}')

            if not user_data:
                response = {
                    "meta": self.meta,
                    "message": f"user with id {userid} does not exists",
                    "status": "failure",
                }
                FlaskLogger.log('delete',
                                f'del_{self.account_type}_info',
                                response,
                                input_data=str(args_data),
                                log_level='info')
                return response, self.bad_code, self.headers

            elif user_data:
                user_data = user_data[0]
                if user_data.get('deleted') == 1:
                    response = {
                        "meta": self.meta,
                        "message": f"user with id {userid} does not exists",
                        "status": "failure",
                    }
                    FlaskLogger.log('delete',
                                    f'del_{self.account_type}_info',
                                    response,
                                    input_data=str(args_data),
                                    log_level='info')
                    return response, self.bad_code, self.headers

            updates = {"deleted": 1}
            queries = {"_id": ObjectId(userid)}
            collection = 'common_user_master'
            FlaskMongo.update(collection, updates, queries)

            response = {
                "meta": self.meta,
                "message": f"user with id {userid} deleted successfully",
                "status": "success"
            }
            FlaskLogger.log('delete',
                            f'del_{self.account_type}_info',
                            response,
                            input_data=str(args_data),
                            log_level='info')
            return response, self.success_code, self.headers

        except ValidationError as e:
            # raise e
            print(e)
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "errors": format_api_error(e.messages)
            }
            FlaskLogger.log('delete',
                            f'del_{self.account_type}_info',
                            response,
                            input_data=str(args_data),
                            log_level='error')
            return response, self.bad_code, self.headers

        except Exception as e:
            # raise e
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "reason": str(e)
            }
            FlaskLogger.log('delete',
                            f'del_{self.account_type}_info',
                            response,
                            input_data=str(args_data),
                            log_level='warning')
            return response, self.exception_code, self.headers
Пример #10
0
    def put(self, **kwargs):
        '''
		'''
        try:
            args_data = request.args.to_dict()
            post_data = request.get_json()
            print(args_data)
            print(post_data)

            teacherusers_data.load(post_data, partial=True)
            user = args_data.get("user")

            columns = {"_id": 0}
            queries = {"_id": ObjectId(user)}
            collection = 'common_user_master'
            user_data = FlaskMongo.find(collection, columns, queries)

            if user_data == []:
                response = {
                    "meta": self.meta,
                    "message": f"user {user} does not exists",
                    "status": "failure",
                }
                FlaskLogger.log('put',
                                f'mod_{self.account_type}_info',
                                response,
                                input_data=str(args_data, post_data),
                                log_level='info')
                return response, self.bad_code, self.headers

            updates = post_data
            queries = {"_id": ObjectId(user)}
            collection = 'common_user_master'
            FlaskMongo.update(collection, updates, queries)

            response = {
                "meta": self.meta,
                "message": f"user {user} updated successfully",
                "status": "success"
            }
            FlaskLogger.log('put',
                            f'mod_{self.account_type}_info',
                            response,
                            input_data=str(args_data, post_data),
                            log_level='info')
            return response, self.success_code, self.headers

        except ValidationError as e:
            # raise e
            # print(e)
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "errors": format_api_error(e.messages)
            }
            FlaskLogger.log('put',
                            f'mod_{self.account_type}_info',
                            response,
                            input_data=str(args_data, post_data),
                            log_level='error')
            return response, self.bad_code, self.headers

        except Exception as e:
            # raise e
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "reason": str(e)
            }
            FlaskLogger.log('put',
                            f'mod_{self.account_type}_info',
                            response,
                            input_data=str(args_data, post_data),
                            log_level='warning')
            return response, self.exception_code, self.headers
Пример #11
0
    def delete(self, **kwargs):
        '''
		'''
        try:
            args_data = request.args.to_dict()
            post_data = request.get_json()
            print(args_data)
            # print(post_data)
            room_id = args_data.get("room_id")

            columns = {"_id": 0}
            queries = {"room_id": room_id, "deleted": 0}
            collection = "common_room_master"

            room_data = FlaskMongo.find(collection, columns, queries)
            print(f'room_data: {room_data}')

            if not room_data:
                response = {
                    "meta": self.meta,
                    "message": f"room with id {room_id} does not exists",
                    "status": "failure",
                }
                FlaskLogger.log('delete',
                                'del_rooms_info',
                                response,
                                input_data=str(args_data),
                                log_level='info')
                return response, self.bad_code, self.headers

            elif room_data:
                room_data = room_data[0]
                if room_data.get('deleted') == 1:
                    response = {
                        "meta": self.meta,
                        "message": f"room with id {room_id} does not exists",
                        "status": "failure",
                    }
                    FlaskLogger.log('delete',
                                    'del_rooms_info',
                                    response,
                                    input_data=str(args_data),
                                    log_level='info')
                    return response, self.bad_code, self.headers

            updates = {"deleted": 1}
            queries = {"room_id": room_id}
            collection = 'common_room_master'
            FlaskMongo.update(collection, updates, queries)

            response = {
                "meta": self.meta,
                "message": f"room with id {room_id} deleted successfully",
                "status": "success"
            }
            FlaskLogger.log('delete',
                            'del_rooms_info',
                            response,
                            input_data=str(args_data),
                            log_level='info')
            return response, self.success_code, self.headers

        except Exception as e:
            # raise e
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "reason": str(e)
            }
            FlaskLogger.log('delete',
                            'del_rooms_info',
                            response,
                            input_data=str(args_data),
                            log_level='warning')
            return response, self.exception_code, self.headers
Пример #12
0
    def put(self, **kwargs):
        '''
		'''
        try:
            args_data = request.args.to_dict()
            post_data = request.get_json()
            print(args_data)
            print(post_data)
            room_id = args_data.get("roomid")

            rooms_data.load(post_data, partial=True)

            ######

            db = c_app.config.get('MONGO_DATABASE')
            collection = 'common_room_master'
            collection2 = 'common_user_master'
            teacher_id = args_data.get("teacherid")
            # room_name = post_data.get("room_name")

            columns = {"_id": 0}
            queries = {"teacher_id": teacher_id, "room_id": room_id}
            queries2 = {"_id": ObjectId(teacher_id)}
            room_data = FlaskMongo.find(collection, columns, queries)
            user_data = FlaskMongo.find(collection2, columns, queries2)

            print(post_data)
            print(f'room_data: {room_data}')

            if user_data == []:
                response = {
                    "meta": self.meta,
                    "message": f"teacher with id {teacher_id} does not exists",
                    "status": "failure",
                }
                FlaskLogger.log('put',
                                'mod_rooms_info',
                                response,
                                input_data=str({
                                    'ad': args_data,
                                    'pd': post_data
                                }),
                                log_level='info')
                return response, self.bad_code, self.headers

            if room_data == []:
                response = {
                    "meta": self.meta,
                    "message": f"room with id {room_id} does not exists",
                    "status": "failure",
                }
                FlaskLogger.log('put',
                                'mod_rooms_info',
                                response,
                                input_data=str({
                                    'ad': args_data,
                                    'pd': post_data
                                }),
                                log_level='info')
                return response, self.bad_code, self.headers

            ######

            updates = post_data
            queries = {"room_id": room_id}
            collection = 'common_room_master'
            FlaskMongo.update(collection, updates, queries)

            response = {
                "meta": self.meta,
                "message": f"room with id {room_id} updated successfully",
                "status": "success"
            }
            FlaskLogger.log('put',
                            'mod_rooms_info',
                            response,
                            input_data=str({
                                'ad': args_data,
                                'pd': post_data
                            }),
                            log_level='info')
            return response, self.success_code, self.headers

        except ValidationError as e:
            # raise e
            # print(e)
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "errors": format_api_error(e.messages)
            }
            FlaskLogger.log('put',
                            'mod_rooms_info',
                            response,
                            input_data=str({
                                'ad': args_data,
                                'pd': post_data
                            }),
                            log_level='error')
            return response, self.bad_code, self.headers

        except Exception as e:
            # raise e
            response = {
                "meta": self.meta,
                "message": "unable to process request",
                "status": "failure",
                "reason": str(e)
            }
            FlaskLogger.log('put',
                            'mod_rooms_info',
                            response,
                            input_data=str({
                                'ad': args_data,
                                'pd': post_data
                            }),
                            log_level='warning')
            return response, self.exception_code, self.headers