Пример #1
0
def lib_login():
    """
    :function: lib_login
    模拟登录图书馆
    """
    LoginUrl = lib_login_url
    TestUrl = lib_login_test_url

    hashstr = request.headers.get('Authorization')
    if hashstr is None:
        raise ForbiddenError()
    base64_hashstr = hashstr[6:]
    id_password = base64.b64decode(base64_hashstr)
    sid, password = id_password.split(':')

    s = requests.Session()
    s.post(LoginUrl, {
        'number': sid,
        'passwd': password,
        'select': 'cert_no'
    },
           headers=headers,
           proxies=proxy)

    r = s.get(TestUrl)
    if '123456' in r.content:
        raise ForbiddenError()
    else:
        return s, sid
Пример #2
0
def update_item_in_category(new_item, category_id, item_id):
    # Check existences of category and item
    category = Category.find_by_id(category_id)
    if not category:
        raise RecordNotFoundError('category', category_id)
    item = category.items.filter_by(id=item_id).first()
    if not item:
        raise ItemNotFoundError(item_id)

    # Check permission
    if item.user.id != get_jwt_identity():
        raise ForbiddenError()

    # Save title of item for notification
    old_title = item.title

    # Check existences of item title
    title = new_item.title
    old_item = Item.find_by_title(title)
    if old_item and old_item.id != item_id:
        raise DuplicateValueError('item', 'title', title)

    # Update final result
    item.update_from_copy(new_item)
    item.save_to_db()
    return message('Item "{}" was updated.'.format(old_title))
Пример #3
0
def delete_category(category_id):
    # Check existence of category
    category = Category.find_by_id(category_id)
    if not category:
        raise RecordNotFoundError('category', category_id)

    # Check permission
    if category.user.id != get_jwt_identity():
        raise ForbiddenError()

    name = category.name
    category.delete_from_db()
    return message('Category "{}" was deleted'.format(name))
Пример #4
0
def delete_item_in_category(category_id, item_id):
    # Check existences of category and item
    category = Category.find_by_id(category_id)
    if not category:
        raise RecordNotFoundError('category', category_id)
    item = category.items.filter_by(id=item_id).first()
    if not item:
        raise ItemNotFoundError(item_id)

    # Check permission
    if item.user.id != get_jwt_identity():
        raise ForbiddenError()

    item.delete_from_db()
    return message('Item "{}" was deleted.'.format(item.title))
Пример #5
0
 def _exception_for_response(self, response):
     if response.status_code == 404:
         return NotFoundError(response.reason)
     elif response.status_code == 400 and 'OAuthException' in response.text:
         return InvalidAccessTokenError(response.reason)
     elif response.status_code == 401:
         return UnauthorizedError(response.reason)
     elif response.status_code == 403:
         return ForbiddenError(response.reason)
     elif response.status_code == 429:
         return RateLimitExceededError(response.reason)
     else:
         return ResponseError(u'{} error: {}\nresponse: {}'.format(
             response.status_code,
             response.reason,
             response.text,
         ))
Пример #6
0
def update_category(new_category, category_id):
    # Check existence of category
    category = Category.find_by_id(category_id)
    if not category:
        raise RecordNotFoundError('category', category_id)

    # Check permission
    if category.user.id != get_jwt_identity():
        raise ForbiddenError()

    # Save category name for notification
    new_name = new_category.name

    # Check existence of category name
    old_category = Category.find_by_name(new_name)
    if old_category and old_category.id != category_id:
        raise DuplicateValueError('category', 'name', new_name)

    # Update final result
    category.update_from_copy(new_category)
    category.save_to_db()

    return message('Category "{}" was updated.'.format(new_name))
Пример #7
0
def require_https():
    if not c.secure:
        abort(ForbiddenError(errors.HTTPS_REQUIRED))