Exemplo n.º 1
0
 def post(self):
     _parser = reqparse.RequestParser()
     _parser.add_argument('raterID',
                          type=str,
                          required=True,
                          help="This field cannot be blank.")
     _parser.add_argument('rateeID',
                          type=str,
                          required=True,
                          help="This field cannot be blank.")
     _parser.add_argument('deliveryID',
                          type=str,
                          required=True,
                          help="This field cannot be blank.")
     _parser.add_argument('rating',
                          type=int,
                          required=True,
                          help="This field cannot be blank.")
     _parser.add_argument('feedback',
                          type=str,
                          required=True,
                          help="This field cannot be blank.")
     _parser.add_argument('date')
     data = _parser.parse_args()
     d = datetime.strptime(data['date'], '%Y-%m-%dT%H:%M:%S.%fZ')
     delivery = DeliveryModel.find_by_id(data['deliveryID'])
     if not delivery:
         return {'message': 'Delivery not found'}, 404
     if not delivery.delivered:
         return {
             'message': 'Delivery needs to be confirmed by deliverer first'
         }, 400
     rating = RatingModel.find_by_delivery_id(data['deliveryID'])
     if rating:
         return {'message': 'Delivery already rated'}, 400
     ratee = UserModel.find_by_id(data['rateeID'])
     if not ratee:
         return {'message': 'Ratee not found'}
     rating = RatingModel(raterID=data['raterID'],
                          rateeID=data['rateeID'],
                          deliveryID=data['deliveryID'],
                          rating=data['rating'],
                          feedback=data['feedback'],
                          date=d)
     rating.save_to_db()
     if not ratee.noOfRatings:
         ratee.totalRating = data['rating']
         ratee.noOfRatings = 1
     else:
         ratee.totalRating = ratee.totalRating + data['rating']
         ratee.noOfRatings = ratee.noOfRatings + 1
     ratee.save_to_db()
     credits = calculateFeedbackCredits(data['rating'])
     credit = CreditModel.find_by_user_id(data['rateeID'])
     credit.credits = credit.credits + credits
     credit.save_to_db()
     return {"message": "Rating saved successfully"}, 200
Exemplo n.º 2
0
    def post(self, course_id):
        data = self.parser.parse_args()
        data['course_id'] = course_id

        rating = RatingModel(**data)
        try:
            rating.save()
        except:
            return {"message": "An error occurred while inserting Rating."}, 500

        return rating.json(), 201
Exemplo n.º 3
0
    def get(self, course_id, rating_id):
        rating = RatingModel.find_by_id(rating_id)

        if rating is None:
            return {"message": "No rating found."}, 404

        return rating.json(), 200
Exemplo n.º 4
0
    def delete(self, course_id, rating_id):
        rating = RatingModel.find_by_id(rating_id)

        if rating is None:
            return {"message": "No rating found."}, 404

        try:
            rating.delete()
        except:
            return {"message": "An error occurred while deleting Rating."}, 500

        return {"message": "Rating deleted."}, 200
Exemplo n.º 5
0
 def get(self):
     userID = get_jwt_identity()
     user = UserModel.find_by_id(userID)
     if not user:
         return {'message': 'User not found'}, 404
     ratings = RatingModel.get_ratee_ratings(userID)
     temp = []
     for rating in ratings:
         rater = rating.rater
         temp.append({
             'raterFirstName': rater.firstName,
             'raterLastName': rater.lastName,
             'id': rating.id,
             'rating': rating.rating,
             'date': rating.date.isoformat(),
             'feedback': rating.feedback
         })
     return {
         'firstName': user.firstName,
         'lastName': user.lastName,
         'totalRating': user.totalRating,
         'noOfRatings': user.noOfRatings,
         'ratings': temp
     }
Exemplo n.º 6
0
    def put(self, course_id, rating_id):
        data = self.parser.parse_args()
        data['course_id'] = course_id

        rating = RatingModel.find_by_id(rating_id)

        if rating is None:
            new_rating = RatingModel(**data)
            try:
                new_rating.save()

                return new_rating.json(), 201
            except:
                return {"message": "An error occurred while updating Rating."}, 500

        try:
            rating.update(**data)
        except:
            return {"message": "An error occurred while updating Rating."}, 500

        return rating.json(), 200
Exemplo n.º 7
0
def test_db():
    from models.user import UserModel, RoleModel

    student = RoleModel('student')
    instructor = RoleModel('instructor')
    db.session.add_all([student, instructor])
    db.session.commit()
    bart = UserModel('bart',
                     'bart',
                     'bart',
                     'student',
                     firstname=None,
                     lastname=None,
                     picture=None)
    lisa = UserModel('lisa',
                     'lisa',
                     'lisa',
                     'student',
                     firstname=None,
                     lastname=None,
                     picture=None)
    millhouse = UserModel('millhouse',
                          'millhouse',
                          'millhouse',
                          'student',
                          firstname=None,
                          lastname=None,
                          picture=None)
    edna = UserModel('edna',
                     'edna',
                     'edna',
                     'instructor',
                     firstname=None,
                     lastname=None,
                     picture=None)
    simour = UserModel('simour',
                       'simour',
                       'simour',
                       'instructor',
                       firstname=None,
                       lastname=None,
                       picture=None)
    db.session.add_all([bart, lisa, millhouse, edna, simour])
    db.session.commit()
    print('::: test roles and users ::: ok')
    ###################
    print('students')
    for student in student.users:
        print('>>> {}'.format(student.username))
    print('instructors')
    for instructor in instructor.users:
        print('>>> {}'.format(instructor.username))
    ###################

    from models.course import CourseModel
    from models.category import CategoryModel

    maths = CategoryModel('maths', fr_label=None, en_label=None, picture=None)
    english = CategoryModel('english',
                            fr_label=None,
                            en_label=None,
                            picture=None)
    db.session.add_all([maths, english])
    db.session.commit()
    math_course = CourseModel('mathematics',
                              'basic operations',
                              simour.id,
                              maths.id,
                              picture=None)
    english_course = CourseModel('english',
                                 'grammar',
                                 edna.id,
                                 english.id,
                                 picture=None)
    db.session.add_all([math_course, english_course])
    db.session.commit()
    math_course.register_student(bart.id)
    math_course.register_student(lisa.id)
    english_course.register_student(lisa.id)
    english_course.register_student(millhouse.id)
    db.session.commit()
    print('::: test categories and courses ::: ok')
    ###################
    print('students enrolled in math')
    for student in math_course.students:
        print('>>> {}'.format(student.username))
    print('author of math_course')
    print('>>> {}'.format(math_course.author.username))
    print('edna published courses')
    for course in edna.published_courses:
        print('>>> {} - {}'.format(course.title, course.category.name))
    ###################

    from models.chapter import ChapterModel
    from models.quiz import QuizModel
    from models.question import QuestionModel
    from models.answer import AnswerModel

    chapter1 = ChapterModel('adds', '2 + 2 = 4', 1, math_course.id)
    chapter2 = ChapterModel('subs', '2 - 2 = 0', 2, math_course.id)
    db.session.add_all([chapter1, chapter2])
    db.session.commit()
    quiz1 = QuizModel('first grade', 1, math_course.id)
    quiz2 = QuizModel('second grade', 2, math_course.id)
    db.session.add(quiz1)
    db.session.add(quiz2)
    db.session.commit()
    question1 = QuestionModel('1 + 1?', 1, 2, quiz1.id)
    db.session.add(question1)
    db.session.commit()
    answer1 = AnswerModel('0', 1, question1.id)
    db.session.add(answer1)
    answer2 = AnswerModel('2', 2, question1.id)
    db.session.add(answer2)
    question2 = QuestionModel('3 - 1?', 2, 1, quiz1.id)
    db.session.add(question2)
    answer3 = AnswerModel('2', 1, question2.id)
    db.session.add(answer3)
    answer4 = AnswerModel('4', 2, question2.id)
    db.session.add(answer4)
    db.session.commit()
    print('::: test chapters and quizzes and questions and answers ::: ok')
    ###################
    print('chapters in math_course')
    for chapter in math_course.chapters:
        print('>>> {}'.format(chapter.title))
    print('quizzes in math_course')
    for quiz in math_course.quizzes:
        print('>>> {}'.format(quiz.title))
    print('questions in quiz1')
    for question in quiz1.questions:
        print('>>> {}'.format(question.question))
    print('answers in question1')
    for answer in question1.answers:
        print('>>> {}'.format(answer.answer))
    print('for question1 the good answer is number {}'.format(
        question1.good_answer))
    current_question = question1.question
    good_answer = AnswerModel.query.filter_by(
        number=question1.good_answer).first()
    print('question: {} | response: {}'.format(current_question,
                                               good_answer.answer))
    ###################

    from models.comment import CommentModel
    from models.rating import RatingModel

    comment1 = CommentModel('hay caramba', 'hay caramba', math_course.id,
                            bart.id)
    comment2 = CommentModel('retention', 'retention', math_course.id,
                            simour.id)
    comment3 = CommentModel('eat my short', 'eat my short', math_course.id,
                            bart.id)
    db.session.add_all([comment1, comment2, comment3])
    db.session.commit()
    rate1 = RatingModel(5, english_course.id, lisa.id)
    rate2 = RatingModel(3, english_course.id, millhouse.id)
    db.session.add_all([rate1, rate2])
    db.session.commit()
    print('::: test comments and ratings ::: ok')
    ###################
    print('comments in math_course')
    for comment in math_course.comments:
        print('>>> {} by {}'.format(comment.title, comment.author.username))
    print('ratings in english_course')
    for rate in english_course.ratings:
        print('>>> {}/5 by {}'.format(rate.rate, rate.author.username))
    ###################

    from models.badge import BadgeModel
    from models.score import ScoreModel

    score = ScoreModel(2, 2, lisa.id, quiz1.id)
    db.session.add(score)
    db.session.commit()
    badge = BadgeModel('honor', math_course.id, lisa.id)
    db.session.add(badge)
    db.session.commit()
    print('::: test scores and badges ::: ok')
    ###################
    print('badges earned by lisa')
    for badge in lisa.badges:
        print('>>> {}'.format(badge.name))
    print('scores in quiz1')
    for score in quiz1.scores:
        print('>>> {} by {}'.format(score.score, score.student.username))
Exemplo n.º 8
0
    def get(self, course_id):
        ratings = RatingModel.find_by_course(course_id)

        return [rating.json() for rating in ratings], 200