예제 #1
0
def admin_users_edit_post():
    payload = get_payload(request)
    user_id = payload.get('user_id')
    if user_id:
        user = User.get(user_id)
        return_value = success('All profile values have been updated.')
    else:
        user = User()
        user.insert()
        return_value = success('User created.')
        if not payload.get('password'):
            return jsonify(failure('You must set a password for new users'))
        if not payload.get('email'):
            return jsonify(failure('You must set an email for new users'))
        if not payload.get('name'):
            return jsonify(failure('You must set a name for new users'))

    for key in payload:
        if key == 'password':
            user.set_password(payload[key])
        elif key != 'user_id':
            setattr(user, key, payload[key])

    g.user.insert()
    safe_commit()

    return jsonify(return_value)
예제 #2
0
def create_content():
    return_value = success('The content was created.')
    payload = get_payload(request)

    editing = False
    if payload.get('id'):
        content = Content.get(payload.get('id'))
        editing = True
        return_value = success('The content was updated.')
    else:
        content = Content()

    content.type = payload.get('type').lower()
    content.template = '{}.html'.format(content.type)
    content.title = payload.get('title')
    content.body = payload.get('body') or ''
    content.theme = payload.get('theme')
    content.preview = payload.get('preview') or ''
    content.user_id = payload.get('user_id')
    tags = [t.strip() for t in payload.get('tags', '').split(',') if t.strip()]
    for tag in tags:
        count = Tag.filter(Tag.name == tag).count()
        if not count:
            new_tag = Tag(name=tag)
            new_tag.insert()

    content.tags = ",".join(tags)
    content.parser = payload.get('parser', 'markdown')

    published = json.loads(payload.get('published', 'false'))
    content.published = published

    menu_item = json.loads(payload.get('menu_item', 'false'))
    content.menu_item = menu_item

    if not editing:
        content.slug = make_slug(content.title)
    else:
        published_on = payload.get('published_on')
        if published_on:
            content.published_on = parser.parse(published_on)

    valid = content.validate()
    if valid['success'] or editing:
        print(content.to_dict())
        content.insert()
        safe_commit()
        return_value['id'] = content.id
        # with app.context():
        #    cache.clear()
    else:
        return_value = valid

    return jsonify(return_value)
예제 #3
0
def retrieve_content():
    return_value = success('The content was retrieved.')
    return_value['contents'] = []

    payload = get_payload(request)

    content_id = payload.get('id')
    if content_id:
        content = Content.get(content_id)
        if content:
            return_value['contents'] = [content.to_dict(camel_case=True)]
        else:
            return_value['success'] = False
            return_value['messages'] = ['No content found with that ID.']
    else:
        # No ID passed... we should return more than one result.
        current_page = payload.get('current_page', 1)
        page_size = payload.get('page_size', 5)
        content_type = payload.get('content_type', 'post')
        published = payload.get('published', True)
        contents = Content.filter(Content.type == content_type)\
                          .filter(Content.published == published)\
                          .order_by(Content.published_on.desc())

        contents, maxpages = paginate(contents, current_page, page_size)
        if contents:
            return_value['contents'] = results_to_dict(
                contents, camel_case=True)

    return jsonify(return_value)
예제 #4
0
def upload_ajax():
    return_value = success('The file was uploaded.')
    payload = get_payload(request)
    ufile = request.files['file']
    file_id = upload_file(payload, ufile)
    return_value['id'] = file_id
    return jsonify(return_value)
예제 #5
0
def retrieve_content():
    return_value = success('The content was retrieved.')
    return_value['contents'] = []

    payload = get_payload(request)

    content_id = payload.get('id')
    if content_id:
        content = Content.get(content_id)
        if content:
            return_value['contents'] = [content.to_dict(camel_case=True)]
        else:
            return_value['success'] = False
            return_value['messages'] = ['No content found with that ID.']
    else:
        # No ID passed... we should return more than one result.
        current_page = payload.get('current_page', 1)
        page_size = payload.get('page_size', 5)
        content_type = payload.get('content_type', 'post')
        published = payload.get('published', True)
        contents = Content.filter(Content.type == content_type)\
                          .filter(Content.published == published)\
                          .order_by(Content.published_on.desc())

        contents, maxpages = paginate(contents, current_page, page_size)
        if contents:
            return_value['contents'] = results_to_dict(contents, camel_case=True)

    return jsonify(return_value)
예제 #6
0
def upload_ajax():
    return_value = success('The file was uploaded.')
    payload = get_payload(request)
    ufile = request.files['file']
    file_id = upload_file(payload, ufile)
    return_value['id'] = file_id
    return jsonify(return_value)
예제 #7
0
def create_content():
    return_value = success('The content was created.')
    payload = get_payload(request)

    editing = False
    if payload.get('id'):
        content = Content.get(payload.get('id'))
        editing = True
        return_value = success('The content was updated.')
    else:
        content = Content()

    content.type = payload.get('type').lower()
    content.title = payload.get('title')
    content.body = payload.get('body')
    content.user_id = payload.get('user_id')
    tags = [t.strip() for t in payload.get('tags', '').split(',') if t.strip()]
    for tag in tags:
        count = Tag.filter(Tag.name == tag).count()
        if not count:
            new_tag = Tag(name=tag)
            new_tag.insert()

    content.tags = ",".join(tags)
    content.parser = payload.get('parser', 'markdown')
    content.published = bool(payload.get('published', False))

    if not editing:
        content.slug = make_slug(content.title)
    else:
        created_on = payload.get('created_on')
        if created_on:
            content.created_on = parser.parse(created_on)

    valid = content.validate()
    if valid['success'] or editing:
        content.insert()
        safe_commit()
        return_value['id'] = content.id
    else:
        return_value = valid

    return jsonify(return_value)
예제 #8
0
    def validate(self):
        return_value = success()
        not_unique = Content.filter(Content.title == self.title).count()
        if not_unique:
            return_value['success'] = False
            return_value['messages'].append('That post or page exists already.')
        if not self.title:
            return_value['success'] = False
            return_value['messages'].append("A title is required to create a post or a page.")

        return return_value
예제 #9
0
    def validate(self):
        return_value = success()
        not_unique = User.filter(User.email == self.email).count()
        if not_unique:
            return_value['success'] = False
            return_value['messages'].append("That user exists already.")
        if not self.email:
            return_value['success'] = False
            return_value['messages'].append("An email address is required to create a user.")

        return return_value
예제 #10
0
def admin_content_delete():
    return_value = success('The content has been deleted.')
    payload = get_payload(request)
    content = Content.get(payload.get('id'))
    if content:
        content.delete()
        safe_commit()
    else:
        return_value = failure('Content not found.')

    return jsonify(return_value)
예제 #11
0
def admin_settings_post():
    return_value = success('All settings have been updated.')
    payload = get_payload(request)

    for key in payload:
        setting = Setting.filter(Setting.name == key).first()
        setting.value = payload[key]
        setting.insert()

    safe_commit()

    return jsonify(return_value)
예제 #12
0
def retrieve_user():
    return_value = success('The user was retrieved.')
    payload = get_payload(request)
    user = User.get(payload.get('id'))

    if not user:
        return_value = failure('That user does not exist.')
    else:
        return_value['user'] = user.to_dict(camel_case=True)
        return_value['user'].pop('password')
        return_value['user'].pop('openid')

    return jsonify(return_value)
예제 #13
0
def retrieve_user():
    return_value = success('The user was retrieved.')
    payload = get_payload(request)
    user = User.get(payload.get('id'))

    if not user:
        return_value = failure('That user does not exist.')
    else:
        return_value['user'] = user.to_dict(camel_case=True)
        return_value['user'].pop('password')
        return_value['user'].pop('openid')

    return jsonify(return_value)
예제 #14
0
def delete_user():
    return_value = success('The user was deleted.')
    payload = get_payload(request)

    if not g.user or g.user.id != payload.get('id'):
        user = User.filter(User.id == payload.get('id')).first()
        if user:
            user.delete()
            safe_commit()
        else:
            return_value = failure('That user does not exist.')
    else:
        return_value = failure('You cannot delete the current user.')

    return jsonify(return_value)
예제 #15
0
def delete_user():
    return_value = success('The user was deleted.')
    payload = get_payload(request)

    if not g.user or g.user.id != payload.get('id'):
        user = User.filter(User.id == payload.get('id')).first()
        if user:
            user.active = False
            user.insert()
            safe_commit()
        else:
            return_value = failure('That user does not exist.')
    else:
        return_value = failure('You cannot delete the current user.')

    return jsonify(return_value)
예제 #16
0
def create_user():
    return_value = success('The user was created.')
    payload = get_payload(request)

    user = User(username=payload.get('email'), password=payload.get('password'),
                name=payload.get('name'))

    valid = user.validate()

    if valid['success']:
        user.insert()
        safe_commit()
        return_value['id'] = user.id
    else:
        del(user)
        return_value = valid

    return jsonify(return_value)
예제 #17
0
def admin_files_delete():
    return_value = success('The file has been deleted.')
    payload = get_payload(request)
    afile = File.get(payload.get('id'))
    if afile:
        try:
            os.unlink(afile.path)
        except OSError:
            pass
        try:
            os.unlink(afile.thumbnail_path)
        except OSError:
            pass
        afile.delete()
        safe_commit()
    else:
        return_value = failure('File not found.')

    return jsonify(return_value)
예제 #18
0
def update_user():
    return_value = success('The user was updated.')
    payload = get_payload(request)
    user = User.get(payload.get('id'))

    if not user:
        return_value = failure('That user does not exist.')
    else:
        if payload.get('password'):
            user.set_password(payload.get('password'))
        if payload.get('email'):
            user.email = payload.get('email')
        if payload.get('name'):
            user.name = payload.get('name')

        safe_commit()
        return_value['user'] = user.to_dict(camel_case=True)

    return jsonify(return_value)
예제 #19
0
def update_user():
    return_value = success('The user was updated.')
    payload = get_payload(request)
    user = User.get(payload.get('id'))

    if not user:
        return_value = failure('That user does not exist.')
    else:
        if payload.get('password'):
            hashed_password = generate_password_hash(payload.get('password'))
        if payload.get('email'):
            user.email = payload.get('email')
        if payload.get('name'):
            user.name = payload.get('name')

        user.password = hashed_password
        safe_commit()
        return_value['user'] = user.to_dict(camel_case=True)

    return jsonify(return_value)
예제 #20
0
def create_user():
    return_value = success('The user was created.')
    payload = get_payload(request)

    hashed_password = generate_password_hash(payload.get('password'))

    user = User()
    user.email = payload.get('email')
    user.name = payload.get('name')
    user.password = hashed_password
    valid = user.validate()

    if valid['success']:
        user.insert()
        safe_commit()
        return_value['id'] = user.id
    else:
        del(user)
        return_value = valid

    return jsonify(return_value)