Beispiel #1
0
def login():
    username = ''
    if request.method == 'POST':
        username = get_str('name')
        password = get_str('password')
        if username and password:
            user = User.query.filter_by(name=username).first()
            if user and password_verify(password, user.password):
                session['user'] = user.id
                return redirect(url_for('cms_page2'))

    return render_template('user/login.html', username=username)
Beispiel #2
0
def login():
    username = ''
    if request.method == 'POST':
        username = get_str('name')
        password = get_str('password')
        if username and password:
            user = User.query.filter_by(name=username).first()
            if user and password_verify(password, user.password):
                session['user'] = user.id
                return redirect(url_for('cms_page2'))

    return render_template('user/login.html', username=username)
Beispiel #3
0
def user_edit(id):
    user = User.query.filter_by(id=id).first()
    perms = permission_list
    if not user:
        abort(404)
    if request.method == 'POST':
        user.name = get_str('name')
        db.session.add(user)
        # permissions
        p_in = []
        p_out = []
        for perm, rest in perms:
            if get_bool(perm):
                p_in.append(perm)
            else:
                p_out.append(perm)
        current = [
            p.permission for p in UserPerms.query.filter_by(user_id=id).all()
        ]
        for perm in p_in:
            if perm not in current:
                perm = UserPerms(user_id=id, permission=perm)
                db.session.add(perm)
        for perm in p_out:
            UserPerms.query.filter_by(user_id=id, permission=perm).delete()

        db.session.commit()
        return redirect(url_for_admin('user_edit', id=id))

    user_perms = [
        p.permission for p in UserPerms.query.filter_by(user_id=id).all()
    ]
    return render_template('user/edit.html', user=user, perms=perms,
                           user_perms=user_perms)
Beispiel #4
0
def menu_edit(id):
    set_menu()
    lang = get_admin_lang()
    menu_item = MenuItem.query.filter_by(menu_id=id, lang=lang).first()
    if not menu_item and lang != 'en':
        menu_item = MenuItem.query.filter_by(menu_id=id, lang='en').first()
        if not menu_item:
            abort(404)
    if not menu_item:
        abort(404)
    if request.method == 'POST' and 'title' in request.form:
        if menu_item.lang != lang:
            menu_item = reedit(menu_item)
        menu_item.title = request.form['title']
        db.session.add(menu_item)
        db.session.commit()
        # Update locked fields
        if lang == 'en':
            locked = {
                'active': get_bool('active'),
                'private': get_bool('private'),
                'item': get_str('item'),
                'order': get_int('order', 99),
            }
            MenuItem.query.filter_by(
                menu_id=id
            ).update(locked)
        db.session.commit()
        return redirect(url_for_admin('menu_list'))
    if lang != 'en':
        trans = MenuItem.query.filter_by(menu_id=menu_item.menu_id,
                                         lang='en').first()
    else:
        trans = {}
    translations = get_trans(id)
    options = []
    if lang == 'en':
        options = [
            {'value': 'orgs', 'name': 'Special: Organisation search'},
            {'value': 'res', 'name': 'Special: Resources'},
        ]

        menu_items = db.session.query(Cms.url, Cms.title).filter_by(
            lang='en', active=True, status='publish'
        )
        for i in menu_items:
            options.append({'value': i.url, 'name': 'Page: %s ~ %s'
                            % (i.title, i.url)})
    elif menu_item.lang == 'en':
        menu_item = MenuItem()
    return render_template('admin/menu_edit.html', menuitem=menu_item,
                           trans=trans, options=options,
                           translations=translations)
Beispiel #5
0
def user_edit(id):
    user = User.query.filter_by(id=id).first()
    perms = permission_list
    if not user:
        abort(404)
    if request.method == 'POST':
        user.name = get_str('name')
        db.session.add(user)
        # permissions
        p_in = []
        p_out = []
        for perm, rest in perms:
            if get_bool(perm):
                p_in.append(perm)
            else:
                p_out.append(perm)
        current = [
            p.permission for p in UserPerms.query.filter_by(user_id=id).all()
        ]
        for perm in p_in:
            if perm not in current:
                perm = UserPerms(user_id=id, permission=perm)
                db.session.add(perm)
        for perm in p_out:
            UserPerms.query.filter_by(user_id=id, permission=perm).delete()

        db.session.commit()
        return redirect(url_for_admin('user_edit', id=id))

    user_perms = [
        p.permission for p in UserPerms.query.filter_by(user_id=id).all()
    ]
    return render_template('user/edit.html',
                           user=user,
                           perms=perms,
                           user_perms=user_perms)
Beispiel #6
0
def org_edit(id):
    set_menu()
    lang = get_admin_lang()
    permission_content(lang)
    errors = []
    org = Organisation.query.filter_by(org_id=id, lang=lang,
                                       current=True).first()
    if not org and lang != 'en':
        org = Organisation.query.filter_by(org_id=id, lang='en',
                                           current=True).first()
        if not org:
            abort(404)
    if not org:
        abort(404)
    if request.method == 'POST' and 'name' in request.form:
        if org.lang != lang:
            # No translation
            org = org_reedit(org)
        if (org.name != get_str('name')
                or org.description != get_str('description')):
            if org.status == 'publish':
                org = org_reedit(org)
            org.name = get_str('name')
            org.description = get_str('description')
            org.status = 'edit'
            trans_need_update(org)

        if lang == 'en':
            org.address = get_str('address')
            org.contact = get_str('contact')
            org.phone = get_str('phone')
            org.email = get_str('email')
            org.pgp_key = get_str('pgp_key')
            org.website = get_str('website')
            org.private = get_bool('private')
            org.active = get_bool('active')

            if get_bool('logo_remove'):
                org.image = None

            logo = request.files['logo']
            if logo:
                extension = os.path.splitext(logo.filename)[1]
                if extension and extension.lower(
                ) in config.ALLOWED_IMAGE_TYPES:
                    filename = unicode(uuid.uuid4())
                    filename += extension
                    logo.save(
                        os.path.join(app.config['UPLOAD_FOLDER'], filename))
                    org.image = filename
                else:
                    errors.append(
                        'The image uploaded is not of an allowed type')

        db.session.add(org)
        if lang == 'en':
            # codes
            codes_data = all_codes('en', 'org')
            cats = [cat for cat in codes_data if cat['active']]
            cat_codes = []
            for cat in cats:
                cat_codes += cat['codes']
            codes = [code['code'] for code in cat_codes if code['active']]
            c_in = []
            c_out = []
            for code in codes:
                if get_bool(code):
                    c_in.append(code)
                else:
                    c_out.append(code)
            current = [
                c.code for c in OrgCodes.query.filter_by(org_id=id).all()
            ]
            for code in c_in:
                if code not in current:
                    code = OrgCodes(org_id=id, code=code)
                    db.session.add(code)
            for code in c_out:
                OrgCodes.query.filter_by(org_id=id, code=code).delete()

        db.session.commit()
        if lang == 'en':
            update_translations(org)
        if not errors:
            return redirect(url_for_admin('org_preview', id=id))
    if lang != 'en':
        trans = Organisation.query.filter_by(org_id=org.org_id,
                                             lang='en',
                                             current=True).first()
    else:
        trans = {}

    if lang == 'en':
        codes = all_codes('en', 'org')
        current = [c.code for c in OrgCodes.query.filter_by(org_id=id).all()]
    else:
        codes = []
        current = []
    translations = get_trans(id)
    if lang != org.lang:
        org = {}
    return render_template('admin/org_edit.html',
                           org=org,
                           trans=trans,
                           codes=codes,
                           current=current,
                           errors=errors,
                           translations=translations)
Beispiel #7
0
def cms_edit(id):
    set_menu()
    lang = get_admin_lang()
    permission_content(lang)
    errors = []
    page = Cms.query.filter_by(page_id=id, lang=lang, current=True)
    page = page.first()
    if not page and lang != 'en':
        page = Cms.query.filter_by(page_id=id, lang='en', current=True)
        page = page.first()
        if not page:
            abort(404)
    if not page:
        abort(404)
    if request.method == 'POST' and 'title' in request.form:
        if page.lang != lang:
            # no translation
            page = page_reedit(page)

        if (page.title != get_str('title')
                or page.content != get_str('content')):
            if page.status == 'publish':
                page = page_reedit(page)

            page.title = get_str('title')
            page.content = get_str('content')
            page.status = 'edit'
            trans_need_update(page)
        if lang == 'en':
            page.active = get_bool('active')
            page.private = get_bool('private')

            url = get_str('url')
            if url:
                check = Cms.query.filter(Cms.page_id != id, Cms.url == url)
                check = check.filter(
                    db.or_(Cms.status == 'publish', Cms.current == True))
                if check.count():
                    errors.append(
                        'The url is already used by another page choose ' + \
                        'a new url or change the url of the existing page ' + \
                        'first. The url has been reset in this form.'
                    )
                elif url in config.DISALLOWED_URLS:
                    errors.append(
                        'The url provided is not allowed please choose ' + \
                        'a new one. The url has been reset in this form.'
                    )
                else:
                    page.url = url

            if get_bool('logo_remove'):
                page.image = None

            logo = request.files['logo']
            if logo:
                extension = os.path.splitext(logo.filename)[1]
                if extension and extension.lower(
                ) in config.ALLOWED_IMAGE_TYPES:
                    filename = unicode(uuid.uuid4())
                    filename += extension
                    logo.save(
                        os.path.join(app.config['UPLOAD_FOLDER'], filename))
                    page.image = filename
                else:
                    errors.append(
                        'The image uploaded is not of an allowed type')

        db.session.add(page)
        db.session.commit()
        if lang == 'en':
            update_translations(page)
        if not errors:
            return redirect(url_for_admin('cms_preview', id=id))
    if lang != 'en':
        trans = Cms.query.filter_by(page_id=id, lang='en',
                                    current=True).first()
    else:
        trans = {}
    if lang != page.lang:
        page = {}
    translations = get_trans(id)
    return render_template('admin/cms_edit.html',
                           page=page,
                           trans=trans,
                           translations=translations,
                           errors=errors)
Beispiel #8
0
def org_edit(id):
    set_menu()
    lang = get_admin_lang()
    permission_content(lang)
    errors = []
    org = Organisation.query.filter_by(
        org_id=id, lang=lang, current=True
    ).first()
    if not org and lang != 'en':
        org = Organisation.query.filter_by(
            org_id=id, lang='en', current=True
        ).first()
        if not org:
            abort(404)
    if not org:
        abort(404)
    if request.method == 'POST' and 'name' in request.form:
        if org.lang != lang:
            # No translation
            org = org_reedit(org)
        if (org.name != get_str('name')
                or org.description != get_str('description')):
            if org.status == 'publish':
                org = org_reedit(org)
            org.name = get_str('name')
            org.description = get_str('description')
            org.status = 'edit'
            trans_need_update(org)

        if lang == 'en':
            org.address = get_str('address')
            org.contact = get_str('contact')
            org.phone = get_str('phone')
            org.email = get_str('email')
            org.pgp_key = get_str('pgp_key')
            org.website = get_str('website')
            org.private = get_bool('private')
            org.active = get_bool('active')

            if get_bool('logo_remove'):
                org.image = None

            logo = request.files['logo']
            if logo:
                extension = os.path.splitext(logo.filename)[1]
                if extension and extension.lower() in config.ALLOWED_IMAGE_TYPES:
                    filename = unicode(uuid.uuid4())
                    filename += extension
                    logo.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
                    org.image = filename
                else:
                    errors.append(
                        'The image uploaded is not of an allowed type'
                    )


        db.session.add(org)
        if lang == 'en':
            # codes
            codes_data = all_codes('en', 'org')
            cats = [cat for cat in codes_data if cat['active']]
            cat_codes = []
            for cat in cats:
                cat_codes += cat['codes']
            codes = [code['code'] for code in cat_codes if code['active']]
            c_in = []
            c_out = []
            for code in codes:
                if get_bool(code):
                    c_in.append(code)
                else:
                    c_out.append(code)
            current = [
                c.code for c in OrgCodes.query.filter_by(org_id=id).all()
            ]
            for code in c_in:
                if code not in current:
                    code = OrgCodes(org_id=id, code=code)
                    db.session.add(code)
            for code in c_out:
                OrgCodes.query.filter_by(org_id=id, code=code).delete()

        db.session.commit()
        if lang == 'en':
            update_translations(org)
        if not errors:
            return redirect(url_for_admin('org_preview', id=id))
    if lang != 'en':
        trans = Organisation.query.filter_by(
            org_id=org.org_id, lang='en', current=True
        ).first()
    else:
        trans = {}

    if lang == 'en':
        codes = all_codes('en', 'org')
        current = [
            c.code for c in OrgCodes.query.filter_by(org_id=id).all()
        ]
    else:
        codes = []
        current = []
    translations = get_trans(id)
    if lang != org.lang:
        org = {}
    return render_template('admin/org_edit.html', org=org, trans=trans,
                           codes=codes, current=current, errors=errors,
                           translations=translations)
Beispiel #9
0
def cms_edit(id):
    set_menu()
    lang = get_admin_lang()
    permission_content(lang)
    errors = []
    page = Cms.query.filter_by(page_id=id, lang=lang, current=True)
    page = page.first()
    if not page and lang != 'en':
        page = Cms.query.filter_by(page_id=id, lang='en', current=True)
        page = page.first()
        if not page:
            abort(404)
    if not page:
        abort(404)
    if request.method == 'POST' and 'title' in request.form:
        if page.lang != lang:
            # no translation
            page = page_reedit(page)

        if (page.title != get_str('title')
                or page.content != get_str('content')):
            if page.status == 'publish':
                page = page_reedit(page)

            page.title = get_str('title')
            page.content = get_str('content')
            page.status = 'edit'
            trans_need_update(page)
        if lang == 'en':
            page.active = get_bool('active')
            page.private = get_bool('private')

            url = get_str('url')
            if url:
                check = Cms.query.filter(Cms.page_id != id, Cms.url == url)
                check = check.filter(db.or_(
                    Cms.status == 'publish', Cms.current == True
                ))
                if check.count():
                    errors.append(
                        'The url is already used by another page choose ' + \
                        'a new url or change the url of the existing page ' + \
                        'first. The url has been reset in this form.'
                    )
                elif url in config.DISALLOWED_URLS:
                    errors.append(
                        'The url provided is not allowed please choose ' + \
                        'a new one. The url has been reset in this form.'
                    )
                else:
                    page.url = url

            if get_bool('logo_remove'):
                page.image = None

            logo = request.files['logo']
            if logo:
                extension = os.path.splitext(logo.filename)[1]
                if extension and extension.lower() in config.ALLOWED_IMAGE_TYPES:
                    filename = unicode(uuid.uuid4())
                    filename += extension
                    logo.save(
                        os.path.join(app.config['UPLOAD_FOLDER'], filename)
                    )
                    page.image = filename
                else:
                    errors.append(
                        'The image uploaded is not of an allowed type'
                    )

        db.session.add(page)
        db.session.commit()
        if lang == 'en':
            update_translations(page)
        if not errors:
            return redirect(url_for_admin('cms_preview', id=id))
    if lang != 'en':
        trans = Cms.query.filter_by(page_id=id, lang='en',
                                    current=True).first()
    else:
        trans = {}
    if lang != page.lang:
        page = {}
    translations = get_trans(id)
    return render_template('admin/cms_edit.html', page=page, trans=trans,
                           translations=translations, errors=errors)