Пример #1
0
def new(*args, **kwargs):
    currentUser = User.get().filter_by(
        name=kwargs['token']['name']).first_or_404()

    if not currentUser.role.permissions.add_post:
        return make_response(
            jsonify({
                'operation': 'error',
                'error': 'Missing permissions'
            }), 401)

    if not request.form['data']:
        return make_response(
            jsonify({
                'operation': 'error',
                'error': 'Missing data'
            }), 401)

    data = json.loads(str(request.form['data']))

    if not data['title'] or not data['content'] or not data['tags']:
        return make_response(
            jsonify({
                'operation': 'error',
                'error': 'Missing data'
            }), 401)

    index = str(db.session.execute(Sequence('post_id_seq')))
    thumbnail_link = None
    if data['image']:
        thumbnail = SaveImage(index)
        thumbnail_link = url_for('static',
                                 filename='thumbail_post/{}'.format(thumbnail))
    else:
        thumbnail_link = 'none'

    lang = translate.getLanguageForText(
        str(cleanhtml(data['content'])).encode('utf-8-sig'))

    langQuery = Languages.get().filter_by(code=lang.iso_tag).first()

    if langQuery is None:
        new_lang = Languages(name=lang.language, code=lang.iso_tag)
        new_lang.add()
        langQuery = new_lang

    tags_ids = []
    tags = []

    for tag in data['tags']:
        check = Post_Tag.get().filter_by(name=tag).first()

        if check is None:
            new_tag = Post_Tag(name=tag, count=1)
            new_tag.add()
            check = new_tag
        else:
            setattr(check, 'count', Post_Tag.count + 1)
            check.save()

        tags_ids.append(check.id)

    for tag_id in tags_ids:
        tags.append({"post": index, "tag_id": tag_id})

    nPost = NewPostSchema().load({
        "id":
        int(index),
        "title":
        data['title'],
        "read_time":
        str(readtime.of_html(data['content'])),
        "author_id":
        currentUser.id,
        "language_id":
        langQuery.id,
        "info": {
            "thumbnail": thumbnail_link,
            "text": data['content'],
            "tags": tags
        },
        "link":
        '/post/' + (str(data['title']).replace(' ', '-')).replace('?', '') +
        '-' + str(index)
    })

    nPost.add()

    for user in currentUser.followed:
        not_id = str(db.session.execute(Sequence('notification_id_seq')))
        notification = Notification(
            id=int(not_id),
            author=currentUser.id,
            user=user.user,
            type=5,
            title=nPost.title,
            body='{} shared a new post'.format(currentUser.name),
            link=nPost.link + '?notification_id=' + str(not_id))
        send_notification(
            user.user, {
                'text': '{} shared a new post'.format(currentUser.name),
                'link': nPost.link + '?notification_id=' + str(not_id),
                'icon': currentUser.info.avatar_img,
                'id': int(not_id)
            })
        notification.add()

    return make_response(jsonify({
        'operation': 'success',
        'link': nPost.link
    }), 200)
Пример #2
0
from models import User, User_Pers, User_Info, User_Role, Role_Permissions, Status_Types, Languages, Like_Type, Notification_Type

offline = Status_Types(name='Offline', color='#e60b00')
online = Status_Types(name='Online', color='#29f500')

offline.add()
online.add()

language = Languages(name='English', code='en')

language.add()

heart = Like_Type(name="Heart", color="#ff2b2b", icon="heart")

heart.add()

notification_types = [
    Notification_Type(name="follow"),
    Notification_Type(name="like"),
    Notification_Type(name="comment"),
    Notification_Type(name="reply"),
    Notification_Type(name="share"),
    Notification_Type(name="mention")
]

for n in notification_types:
    n.add()

admin_role = User_Role(name="Admin")
admin_role.add()