Example #1
0
    def update_specific_list(self, updated_object, old_user, old_list):
        specified_document = self.users.find_one({
            'user': old_user.lower(),
            'list': old_list.lower()
        })
        id_to_be_used = specified_document['_id']
        updated_object = self.update_fields(specified_document, updated_object)
        try:
            result = self.users.update_one({'_id': id_to_be_used}, {
                '$set': {
                    "user": updated_object.username,
                    "list": updated_object.playlist,
                    "dateupdated": updated_object.date_updated,
                    "beverages": updated_object.beverages,
                    "imageUrl": updated_object.image_url,
                    "displayName": updated_object.display_name
                }
            },
                                           upsert=False)

            if result.modified_count == 1:
                return
            else:
                raise InvalidUsage('No list was updated', status_code=400)
        except errors.ConnectionFailure:
            raise InvalidUsage('Could not connect to mongo', status_code=503)
        except errors.OperationFailure:
            raise InvalidUsage('Update of list has failed', status_code=400)
Example #2
0
 def insert_new_list(self, post_data):
     try:
         self.users.insert_one(post_data)
     except errors.ConnectionFailure:
         raise InvalidUsage('Could not connect to mongo', status_code=503)
     except errors.OperationFailure:
         raise InvalidUsage('The insertion of a new list has failed',
                            status_code=400)
Example #3
0
 def delete_all_user_lists(self, to_remove_user):
     try:
         self.users.delete_many({"user": to_remove_user})
         return
     except errors.ConnectionFailure:
         raise InvalidUsage('Could not connect to mongo', status_code=503)
     except errors.OperationFailure:
         raise InvalidUsage('Deletion of lists has failed', status_code=400)
Example #4
0
 def delete_specific_list(self, to_remove_user, to_remove_list):
     try:
         self.users.delete_one({
             "user": to_remove_user,
             "list": to_remove_list
         })
         return
     except errors.ConnectionFailure:
         raise InvalidUsage('Could not connect to mongo', status_code=503)
     except errors.OperationFailure:
         raise InvalidUsage('Deletion of list has failed', status_code=400)
Example #5
0
 def get_all_user_lists(self, username):
     description_lists = []
     try:
         query = self.users.find({'user': username})
         mapper = CursorMapper()
         for result in query:
             res = mapper.map_cursor_to_object(result, username)
             description_lists.append(res)
         return description_lists
     except errors.ConnectionFailure:
         raise InvalidUsage('Could not connect to mongo', status_code=503)
     except errors.InvalidOperation:
         raise InvalidUsage('The mongo operation was not valid',
                            status_code=400)
Example #6
0
 def get_frontpage_beverages(self, list_name):
     try:
         specified_document = self.frontpage.find_one({'list': list_name})
         if specified_document is None:
             raise InvalidUsage('List could not be found', status_code=404)
         mapper = CursorMapper()
         front_page_model = mapper.map_cursor_to_object(
             specified_document, 'frontpage')
         return front_page_model
     except errors.ConnectionFailure:
         raise InvalidUsage('Could not connect to mongo', status_code=503)
     except errors.InvalidOperation:
         raise InvalidUsage('The mongo operation was not valid',
                            status_code=400)
Example #7
0
 def check_if_userlist_can_be_found(self, user_name, user_list):
     specified_document = self.users.find_one({
         'user': user_name.lower(),
         'list': user_list.lower()
     })
     if specified_document is None:
         raise InvalidUsage('List could not be found', status_code=404)
Example #8
0
 def test_invalid_usage_redis(self):
     error = InvalidUsage('hello from pytest', 400)
     sut = public_controller.handle_invalid_usage(error)
     message = sut.response
     status = sut.status_code
     assert 'pytest' in str(message[0])
     assert status == 400
Example #9
0
 def test_invalid_usage_randomize(self):
     error = InvalidUsage('hello from pytest', 400)
     sut = randomize_controller.handle_invalid_usage(error)
     message = sut.response
     status = sut.status_code
     assert 'pytest' in message[0]
     assert status == 400
Example #10
0
 def check_if_userlist_exists(self, user_name, user_list):
     specified_document = self.users.find_one({
         'user': user_name.lower(),
         'list': user_list.lower()
     })
     if specified_document is not None:
         raise InvalidUsage('User and list combination already exists',
                            status_code=400)
Example #11
0
def validate_schema(schema, file):
    v = Validator(schema)
    valid = v.validate(file, schema)
    if not valid:
        raise InvalidUsage('Errors occured when validating',
                           status_code=400,
                           meta=v.errors)
    return
Example #12
0
 def count_rolled_drinks(self, redis_col, drink_to_incr):
     try:
         col = redis_col
         drink = drink_to_incr
         key = col + ":" + drink
         temp = r_.get(key)
         if temp is None:
             r_.set(key, 0)
         r_.incr(key)
         return
     except redis.exceptions.ConnectionError:
         raise InvalidUsage(
             'An error has occurred with the Redis connection',
             status_code=503)
     except redis.exceptions.RedisError:
         raise InvalidUsage(
             'An error has occurred when incrementing drinks',
             status_code=503)
Example #13
0
 def get_specific_list(self, user_name, list_name):
     specified_document = self.users.find_one({
         'user': user_name,
         'list': list_name
     })
     if specified_document is None:
         raise InvalidUsage('List could not be found', status_code=404)
     users_model = CursorMapper.map_cursor_to_object(
         specified_document, user_name)
     return users_model
Example #14
0
 def validate_user_is_not_reserved_keywords(self, user_name):
     reserved_keywords = [
         'global', 'frontpage', 'bevrand', 'bevragerandomizer'
     ]
     if user_name.lower() in reserved_keywords:
         raise InvalidUsage(
             user_name +
             ' is a reserved username and cannot be used for creation or deletion',
             status_code=403)
     return
Example #15
0
 def update_list(self, user_name, list_name, json_body):
     self.validate_misc_fields_in_json(json_body)
     mongo_users = UsersDb(api.mongo.db)
     mongo_users.check_if_userlist_can_be_found(user_name.lower(),
                                                list_name.lower())
     mongo_object = ObjectMapper.map_json_to_object(json_body, user_name,
                                                    list_name)
     self.validate_user_is_not_reserved_keywords(mongo_object.username)
     if mongo_object.username != user_name:
         raise InvalidUsage('You can only update your own lists',
                            status_code=400)
     mongo_users.update_specific_list(mongo_object, user_name, list_name)
Example #16
0
 def get_top_list(self, redis_col):
     try:
         pattern = redis_col + "*"
         output = r_.keys(pattern)
         drinks = []
         for out in output:
             count = r_.get(out)
             jsonvalue = {'drink': out, 'rolled': int(count)}
             drinks.append(jsonvalue)
         sorted_list = sorted(drinks,
                              key=itemgetter('rolled'),
                              reverse=True)
         success_model = SuccessModelRedis(sorted_list)
         return success_model
     except redis.exceptions.ConnectionError:
         raise InvalidUsage(
             'An error has occurred with the Redis connection',
             status_code=503)
     except redis.exceptions.RedisError:
         raise InvalidUsage(
             'An error has occurred when incrementing drinks',
             status_code=503)
Example #17
0
 def test_no_status_code_gives_back_400(self):
     sut = InvalidUsage('hello from pytest')
     assert sut.status_code == 400
Example #18
0
 def check_if_user_exists(self, username):
     specified_document = self.users.find_one({'user': username})
     if specified_document is None:
         raise InvalidUsage('User could not be found', status_code=404)
Example #19
0
def raise_error(message, status, meta=None):
    raise InvalidUsage(message=message, status_code=status, meta=meta)