Beispiel #1
0
 def get(self):
     userID = get_jwt_identity()
     user = UserModel.find_by_id(userID)
     if not user:
         return {'message': 'User Not Found'}, 404
     credit = CreditModel.find_by_user_id(userID)
     if not credit:
         return {'message': 'Credits not found'}, 404
     return {'credit': credit.json()}, 200
Beispiel #2
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
Beispiel #3
0
 def post(self):
     _parser = reqparse.RequestParser()
     _parser.add_argument('categoryTypeID',
                          type=str,
                          required=True,
                          help="This field cannot be blank.")
     _parser.add_argument('imageLink1', type=str, required=False)
     _parser.add_argument('imageLink2', type=str, required=False)
     _parser.add_argument('imageLink3', type=str, required=False)
     _parser.add_argument('imageLink4', type=str, required=False)
     _parser.add_argument('imageLink5', type=str, required=False)
     _parser.add_argument('quantity',
                          type=int,
                          required=True,
                          help="This field cannot be blank.")
     _parser.add_argument('description',
                          type=str,
                          required=True,
                          help="This field cannot be blank.")
     _parser.add_argument('expiryDate', type=str, required=False)
     _parser.add_argument('tags', type=list, required=True, location='json')
     userID = get_jwt_identity()
     data = _parser.parse_args()
     expiryDate = datetime.strptime(
         data['expiryDate'],
         '%Y-%m-%dT%H:%M:%S.%fZ') if data['expiryDate'] else None
     quantityLeft = ItemModel.get_category_type_items_quantity(
         data['categoryTypeID'])
     if quantityLeft == None:
         quantityLeft = 0
     categoryType = CategoryTypeModel.find_by_id(data['categoryTypeID'])
     credits = calculateDonationCredits(quantityLeft, categoryType.price,
                                        data['quantity'])
     credit = CreditModel.find_by_user_id(userID)
     credit.credits += credits
     credit.save_to_db()
     # See if user has already donated an item for that categoryTypeID
     item = ItemModel(userID=userID,
                      categoryTypeID=data['categoryTypeID'],
                      imageLink1=data['imageLink1'],
                      imageLink2=data['imageLink2'],
                      imageLink3=data['imageLink3'],
                      imageLink4=data['imageLink4'],
                      imageLink5=data['imageLink5'],
                      quantity=data['quantity'],
                      description=data['description'],
                      expiryDate=expiryDate)
     item.save_to_db()
     for name in data['tags']:
         tag = TagModel.find_by_name(name)
         if not tag:
             tag = TagModel(name=name)
             tag.save_to_db()
         itemTag = ItemTagModel(itemID=item.id, tagID=tag.id)
         itemTag.save_to_db()
     return {"message": "Item saved successfully"}, 201
Beispiel #4
0
 def put(self):
     _parser = reqparse.RequestParser()
     _parser.add_argument('userID',
                          type=str,
                          required=True,
                          help="This field cannot be blank.")
     _parser.add_argument('creditsToAdd',
                          type=int,
                          required=True,
                          help="This field cannot be blank.")
     data = _parser.parse_args()
     user = UserModel.find_by_id(data['userID'])
     if not user:
         return {'message': 'User not found'}, 404
     credit = CreditModel.find_by_user_id(data['userID'])
     if not credit:
         return {'message': 'Credits not found'}, 404
     credit.credits = credit.credits + int(data['creditsToAdd'])
     credit.save_to_db()
     return {'message': 'User credits successfully updated'}, 200
Beispiel #5
0
 def post(self,deliveryID):
     delivery = DeliveryModel.find_by_id(deliveryID)
     if not delivery:
         return {'message' : 'Delivery not found'}, 404
     _parser = reqparse.RequestParser()
     _parser.add_argument('confirmationCode',type=str,required=True,help="This field cannot be blank.")
     data = _parser.parse_args()
     if data['confirmationCode'] != delivery.confirmationCode:
         return {'message' : 'Incorrect confirmation code'}, 400
     delivery.setDelivered()
     credits = calculateDeliveryCredits(delivery.deliveryDateTime, datetime.now())
     credit = CreditModel.find_by_user_id(delivery.delivererID)
     credit.credits = credit.credits + credits
     credit.save_to_db()
     item = delivery.item
     categoryType = item.categoryType
     quantityLeft = ItemModel.get_category_type_items_quantity(item.categoryTypeID)
     creditsToSubstract = calculateCreditsSpentOnDelivery(quantityLeft, float(categoryType.price), delivery.quantity)
     beneficiaryCredit = delivery.request.user.credit[0]
     beneficiaryCredit.credits -= creditsToSubstract
     beneficiaryCredit.save_to_db()
     return {'message' : 'Delivery updated successfully'}, 200