Пример #1
0
    def post(self, user_id):
        args = parser.parse_args()

        # Check if the email is already taken or not
        email = args['email']
        user = User().where('email', email).first()
        if user.exists() and user.ATTRIBUTES['id'] != user_id:
            return response({
                'errors': 'This email is already taken'
            }, 400)

        # Update user
        user = User().where('id', '=', user_id).first()
        if user.exists() is True:
            user.update({
                'name': args['name'],
                'email': args['email'],
                'slug': user.generateSlug(name=args['name']),
            })
            return response({
                'user': user.data()
            })

        return response({
            'errors': [
                'User could not found'
            ]
        }, 404)
Пример #2
0
    def post(self):
        args = parser.parse_args()
        user_id = get_jwt_identity()['id']

        # Check if passwords are the same
        if args['password'] is not None and args['passwordConfirm'] != args[
                'password']:
            return response(
                {'errors': ['Password and Confirm Password must be same']},
                400)

        # Check if the email is already taken or not
        email = args['email']
        user = User().where('email', email).first()
        if user.exists() and user.ATTRIBUTES['id'] != user_id:
            return response({'errors': ['This email is already taken']}, 400)

        # Update user
        user = User().where('id', '=', user_id).first()
        if user.exists() is True:
            user.update({
                'name':
                args['name'],
                'email':
                args['email'],
                'slug':
                user.generateSlug(name=args['name']),
                'password':
                bcrypt.generate_password_hash(args['password']).decode('utf-8')
            })
            return response({'user': user.data()})

        return response({'errors': ['User could not found']}, 404)
Пример #3
0
    def post(self):
        args = parser.parse_args()
        password = args['password']
        password_confirm = args['passwordConfirm']

        if password != password_confirm:
            return response({'errors': ['Passwords do not match']}, 401)

        # Rule 2
        user = User()
        user.create({
            'name':
            args['name'],
            'email':
            args['email'],
            'password':
            bcrypt.generate_password_hash(args['password']).decode('utf-8'),
            'slug':
            user.generateSlug(args['name'])
        })
        if user.validate() is False:
            return response({'errors': user.getErrors()}, 401)

        user.save()
        return response({
            'user':
            user.plus('token',
                      user.generateToken()['jwt']).plus(
                          'admin', user.hasRole('admin')).data()
        })
Пример #4
0
    def post(self, note_id):
        args = parser.parse_args()
        title = args['title']
        content = args['content']
        lecturer = args['lecturer']
        link = args['link']
        course_id = args['course_id']
        course_code = args['course_code']
        english = args['english']
        term_id = args['term_id']

        note = Note().where('id', '=', note_id).first()

        if note.exists() is False:
            return response({
                'message': 'That note does not exist'
            }, 401)

        note.update({
            'title': title,
            'content': content,
            'lecturer': lecturer,
            'link': link,
            'course_id': course_id,
            'course_code': course_code,
            'english': english,
            'term_id': term_id,
            'slug': note.generateSlug(name=title)
        })
        return response({
            'message': 'Note successfully updated!'
        }, 200)
Пример #5
0
 def post(self, comment_id):
     user_id = get_jwt_identity()['id']
     comment = Comment().where([['id', '=', comment_id],
                                ['user_id', '=', user_id]]).first()
     if comment.exists():
         comment.delete()
         return response({'message': 'Comment deleted successfully'}, 202)
     return response({'message': 'Comment not found in the database'}, 404)
Пример #6
0
    def get(self, slug):
        user = User().where('slug', slug).first()
        if user.exists() is True:
            return response({
                'user': user.data()
            })

        return response({
            'errors': ['User could not found']
        })
Пример #7
0
 def post(self, comment_id):
     comment = Comment().where('id', '=', comment_id).first()
     if comment.exists():
         comment.delete()
         return response({
             'message': 'Comment deleted successfully'
         }, 202)
     return response({
         'message': 'Comment not found in the database'
     }, 404)
Пример #8
0
 def post(self, event_id):
     event = Event().where('id', '=', event_id).first()
     if event.exists():
         event.delete()
         return response({
             'message': 'Event deleted successfully'
         }, 202)
     return response({
         'message': 'Event does not exist'
     }, 404)
Пример #9
0
 def post(self, event_id):
     user_id = get_jwt_identity()['id']
     event = Event().where([['id', '=', event_id],
                            ['user_id', '=', user_id]]).first()
     if event.exists():
         event.delete()
         return response({'message': 'Event deleted successfully'}, 202)
     return response(
         {'message': 'Event does not exist or it is not yours to delete'},
         404)
Пример #10
0
    def post(self, note_id):
        note = Note().where('id', '=', note_id).first()

        if note.exists():
            note.delete()
            return response({
                'message': 'Note deleted successfully'
            }, 202)
        return response({
            'message': 'Note does not exist'
        }, 404)
Пример #11
0
    def post(self, user_id):
        user_id = get_jwt_identity()['id']
        user = User().where('id', user_id).first()
        if user.exists() is True:
            Comment().where('user_id', user_id).get().delete()
            GradeDistribution().where('user_id', user_id).get().delete()
            Lecturer().where('user_id', user_id).get().delete()
            Note().where('user_id', user_id).get().delete()
            user.delete()
            return response({'message': 'User deleted with success'}, 200)

        return response({'errors': ['User could not found!']}, 401)
Пример #12
0
    def post(self, note_id):
        user_id = get_jwt_identity()['id']
        note = Note().where([['id', '=', note_id],
                               ['user_id', '=', user_id]]).first()

        if note.exists():
            note.delete()
            return response({
                'message': 'Note deleted successfully'
            }, 202)
        return response({
            'message': 'Note does not exist or it is not yours to delete'
        }, 404)
Пример #13
0
    def post(self, dist_id):
        #args = parser.parse_args()
        user_id = get_jwt_identity()['id']
        grade_distribution = GradeDistribution().where(
            [['id', '=', dist_id], ['user_id', '=', user_id]]).first()

        if grade_distribution.exists() is True:
            grade_distribution.delete()
            return response(
                {'message': 'Grade Distribution has deleted successfully'},
                202)

        return response({'errors': ['Grade distribution could not found']},
                        404)
Пример #14
0
    def post(self, lecturer_id):
        args = parser.parse_args()
        user_id = get_jwt_identity()['id']
        lecturer = Lecturer().where([['id', '=', lecturer_id],
                                     ['user_id', '=', user_id]]).first()
        if lecturer.exists() is True:
            lecturer.update({
                'name': args['name'],
                'email': args['email'],
                'slug': lecturer.generateSlug(name=args['name'])
            })
            return response({'lecturer': lecturer.data()})

        return response({'errors': ['Lecturer could not found']}, 404)
Пример #15
0
    def post(self, comment_id):
        args = parser.parse_args()
        commentText = args['comment']

        comment = Comment().where('id', '=', comment_id).first()
        if comment.exists() is False:
            return response({
                'message': 'That comment does not exist'
            }, 401)
        comment.update({
            'comment': commentText,
        })
        return response({
            'message': 'Comment successfully updated!'
        }, 200)
Пример #16
0
    def post(self, course_id):
        args = parser.parse_args()
        name = args['name']

        course = Course().where('id', '=', course_id).first()
        if course.exists() is False:
            return response({
                'message': 'That course does not exist'
            }, 401)
        course.update({
            'name': name,
        })
        return response({
            'message': 'Course successfully updated!'
        }, 200)
Пример #17
0
def before_request():
    path = request.path.split(sep='/')
    if path[2] == 'admin':
        if request.method != "OPTIONS":
            current_user = get_jwt_identity()
            if current_user is None or User().where('id', current_user['id']).first().hasRole('admin') is False:
                return response({'errors': ['Please login']}, 401)
Пример #18
0
    def get(self):
        events = Event().where().orderBy().get().data()
        for event in events:
            user = User().where('id', event['user_id']).first()
            event['user'] = user.data()

        return response({'events': events}, 200)
Пример #19
0
    def get(self, slug):
        lecturer = Lecturer().where('slug', slug).first()

        if lecturer.exists() is True:
            comments = Comment().where([['type_id', '=', lecturer.ATTRIBUTES['id']], ['type', '=', 'lecturers']])\
                .get().data()
            grade_distributions = GradeDistribution().where(
                'lecturer_id', lecturer.ATTRIBUTES['id']).get().data()

            return response({
                'lecturer':
                lecturer.plus('comments',
                              comments).plus('grade_distributions',
                                             grade_distributions).data()
            })

        return response({'errors': ['Lecturer could not found!']})
Пример #20
0
    def post(self, term_id):
        term = Term().where('id', '=', term_id).first()
        notes = Note().where('term_id', term_id).get()
        for note in notes:
            note.delete()
        grade_dists = GradeDistribution().where('term_id', term_id).get()
        for grade_dist in grade_dists:
            grade_dist.delete()

        if term.exists():
            term.delete()
            return response({
                'message': 'Term deleted successfully'
            }, 202)
        return response({
            'message': 'Term does not exist'
        }, 404)
Пример #21
0
    def post(self, course_id):
        course = Course().where('id', '=', course_id).first()
        notes = Note().where('course_id', course_id).get()
        for note in notes:
            note.delete()
        grade_dists = GradeDistribution().where('course_id', course_id).get()
        for grade_dist in grade_dists:
            grade_dist.delete()

        if course.exists():
            course.delete()
            return response({
                'message': 'Course deleted successfully'
            }, 202)
        return response({
            'message': 'Course does not exist'
        }, 404)
Пример #22
0
    def post(self, lecturer_id):
        lecturer = Lecturer().where('id', lecturer_id).first()

        if lecturer.exists() is True:
            Comment().where([['type', '=', 'lecturers'], ['type_id', '=', lecturer_id]]).get().delete()
            GradeDistribution().where('lecturer_id', '=', lecturer_id).get().delete()

            lecturer.delete()
            return response({
                'message': 'Lecturer deleted'
            })

        return response({
            'errors': [
                'Lecturer could not found'
            ]
        }, 401)
Пример #23
0
    def post(self, type_id, comment_id):
        args = parser.parse_args()
        commentText = args['comment']
        user_id = get_jwt_identity()['id']

        comment = Comment().where([['id', '=', comment_id],
                                   ['user_id', '=', user_id]]).first()
        if comment.exists() is False or comment.validate() is False:
            return response(
                {
                    'message':
                    'That comment does not exist or it does not belong to you'
                }, 401)
        comment.update({
            'comment': commentText,
        })
        return response({'message': 'Comment successfully updated!'}, 200)
Пример #24
0
    def post(self, term_id):
        args = parser.parse_args()
        season = args['season']
        term_year = args['term_year']

        term = Term().where('id', '=', term_id).first()
        if term.exists() is False:
            return response({
                'message': 'That term does not exist'
            }, 401)
        term.update({
            'season': season,
            'term_year': term_year,
        })
        return response({
            'message': 'Term successfully updated!'
        }, 200)
Пример #25
0
    def post(self):
        args = parser.parse_args()
        user_id = get_jwt_identity()['id']
        file = args['image']

        if file:
            grade_distribution = GradeDistribution()
            filename = grade_distribution.generateImageName()
            grade_distribution.create({
                'lecturer_id': args['lecturer_id'],
                'user_id': user_id,
                'term_id': args['term_id'],
                'course_id': args['course_id'],
                'course_code': args['course_code'],
                'english': args['english'],
                'image': filename
            })

            imgdata = base64.b64decode(file.split(';base64,', 1)[1])

            with open(server.config['UPLOAD_FOLDER'] + '/' + filename,
                      'wb') as f:
                f.write(imgdata)

            file_size = os.stat(
                os.path.join(server.config['UPLOAD_FOLDER'], filename)).st_size

            if file_size > 5000000:
                os.remove(
                    os.path.join(server.config['UPLOAD_FOLDER'], filename))
                return response(
                    {'errors': ['File size must be lower than 5 MB']}, 400)

            if grade_distribution.validate() is True:
                grade_distribution.save()

                return response(
                    {'grade_distribution': grade_distribution.data()})

            return response({'errors': grade_distribution.getErrors()}, 400)

        return response({
            'errors': ['File could not found!'],
            'file': file
        }, 400)
Пример #26
0
    def get(self, type_id):
        comments = Comment().where([['type', '=', 'notes'],
                                    ['type_id', '=',
                                     type_id]]).orderBy().get().data()
        for comment in comments:
            user = User().where('id', comment['user_id']).first()
            comment['user'] = user.data()

        return response({'comments': comments}, 200)
Пример #27
0
    def get(self, type_id):
        comments = Comment().where([['type', '=', 'lecturers'],
                                    ['type_id', '=',
                                     type_id]]).orderBy().get().data()
        for comment in comments:
            user = User().where('id', comment['user_id']).first()
            comment['user'] = {'id': user.ATTRIBUTES['id']}

        return response({'comments': comments}, 200)
Пример #28
0
    def post(self, lecturer_id):
        user_id = get_jwt_identity()['id']
        lecturer = Lecturer().where([['id', '=', lecturer_id],
                                     ['user_id', '=', user_id]]).first()

        comments = Comment().where([['type_id', '=', lecturer_id],
                                    ['type', '=', 'lecturers']]).get()
        for comment in comments:
            comment.delete()

        dists = GradeDistribution().where('lecturer_id',
                                          lecturer_id).get().delete()

        if lecturer.exists() is True:
            lecturer.delete()
            return response({'message': 'Lecturer deleted'})

        return response({'errors': ['Lecturer could not found']}, 401)
Пример #29
0
    def post(self):
        args = parser.parse_args()
        name = args['name']

        course = Course()
        course.create({
            'name': name,
        })

        if course.validate() is True:
            course.save()
            return response({
                'course': course.data()
            }, 200)

        return response({
            'errors': course.getErrors()
        }, 400)
Пример #30
0
    def post(self):
        args = parser.parse_args()
        email = args['email']
        password = args['password']

        user = User().where([['email', '=', email]]).first()

        if user.exists() and bcrypt.check_password_hash(
                user.HIDDEN['password'], password):
            return response({
                'user':
                user.plus('token',
                          user.generateToken()['jwt']).plus(
                              'admin', user.hasRole('admin')).data()
            })

        return response(
            {'errors': ['Credentials do not match with our records.']}, 401)