Example #1
0
def category_delete(category_id, cat_type):
    # Prevent delet if there are codes for the category
    if Code.query.filter_by(category_id=category_id).all():
        return redirect(url_for_admin('category_list', cat_type=cat_type))
    Category.query.filter_by(category_id=category_id).delete()
    db.session.commit()
    return redirect(url_for_admin('category_list', cat_type=cat_type))
Example #2
0
def category_edit(id, cat_type):
    set_menu(cat_type)
    lang = get_admin_lang()
    category = Category.query.filter_by(category_id=id, lang=lang).first()
    if not category and lang != 'en':
        category = Category.query.filter_by(category_id=id, lang='en').first()
        if not category:
            abort(404)
        return category_trans(id, cat_type)
    if not category:
        abort(404)
    if lang != 'en':
        trans = Category.query.filter_by(category_id=category.category_id,
                                         lang='en').first()
    else:
        trans = {}
    if request.method == 'POST':
        category.title = request.form['title']
        category.description = request.form['description']
        db.session.add(category)
        # Update locked fields
        if lang == 'en':
            locked = {
                'active': get_bool('active'),
                'order': get_int('order', 99),
            }
            Category.query.filter_by(
                category_id=category.category_id
            ).update(locked)
        db.session.commit()
        return redirect(url_for_admin('category_list', cat_type=cat_type))
    translations = get_cat_trans(category.category_id)
    return render_template('admin/category_edit.html', category=category,
                           trans=trans, cat_type=cat_type,
                           translations=translations)
Example #3
0
File: user.py Project: rrymrrsn/hrd
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)
Example #4
0
File: cms.py Project: whythawk/hrd
def cms_new():
    permission('content_manage')
    lang = 'en'
    page = Cms(lang=lang)
    page.status = 'edit'
    db.session.add(page)
    db.session.commit()
    return redirect(url_for_admin('cms_edit', id=page.page_id))
Example #5
0
def code_new(category_id, cat_type):
    lang = 'en'
    code = Code(lang=lang)
    code.status = 'edit'
    code.category_id = category_id
    db.session.add(code)
    db.session.commit()
    return redirect(url_for_admin('code_edit', id=code.code_id, cat_type=cat_type))
Example #6
0
File: cms.py Project: rrymrrsn/hrd
def cms_new():
    permission('content_manage')
    lang = 'en'
    page = Cms(lang=lang)
    page.status = 'edit'
    db.session.add(page)
    db.session.commit()
    return redirect(url_for_admin('cms_edit', id=page.page_id))
Example #7
0
def org_new():
    permission('content_manage')
    lang = 'en'
    org = Organisation(lang=lang)
    org.status = 'edit'
    db.session.add(org)
    db.session.commit()
    return redirect(url_for_admin('org_edit', id=org.org_id))
Example #8
0
def org_new():
    permission('content_manage')
    lang = 'en'
    org = Organisation(lang=lang)
    org.status = 'edit'
    db.session.add(org)
    db.session.commit()
    return redirect(url_for_admin('org_edit', id=org.org_id))
Example #9
0
def menu_new(id=None):
    lang = 'en'
    menu_item = MenuItem(lang=lang)
    menu_item.active = False
    if id:
        menu_item.parent_menu_id = id
    db.session.add(menu_item)
    db.session.commit()
    return redirect(url_for_admin('menu_edit', id=menu_item.menu_id))
Example #10
0
def category_new(cat_type):
    check_cat_type(cat_type)
    lang = 'en'
    category = Category(lang=lang)
    category.status = 'edit'
    category.cat_type = cat_type
    db.session.add(category)
    db.session.commit()
    return redirect(url_for_admin('category_edit',
                            id=category.category_id,
                            cat_type=cat_type))
Example #11
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)
Example #12
0
def code_trans(id, cat_type):
    lang = get_admin_lang()
    code = Code.query.filter_by(code_id=id, lang='en').first()
    if not code:
        abort(404)
    exists = Code.query.filter_by(code_id=code.code_id, lang=lang).first()
    if exists:
        abort(403)
    trans = Code(lang=lang)
    trans.status = 'edit'
    trans.code_id = id
    trans.category_id = code.category_id
    db.session.add(trans)
    db.session.commit()
    return redirect(url_for_admin('code_edit', id=id, cat_type=cat_type))
Example #13
0
File: cms.py Project: rrymrrsn/hrd
def cms_state(id, state):
    lang = get_admin_lang()
    permission_content(lang)
    if state not in STATES:
        abort(403)
    page = Cms.query.filter_by(page_id=id, status=STATES[state], lang=lang,
                               current=True).first()
    if not page:
        abort(403)
    if state == 'publish':
        page.published = True
        old_page = Cms.query.filter_by(page_id=id, status='publish',
                                       lang=lang).first()
        if old_page:
            old_page.status = 'archive'
            db.session.add(old_page)
    page.status = state
    db.session.add(page)
    db.session.commit()
    return redirect(url_for_admin('cms_preview', id=id))
Example #14
0
File: cms.py Project: whythawk/hrd
def cms_state(id, state):
    lang = get_admin_lang()
    permission_content(lang)
    if state not in STATES:
        abort(403)
    page = Cms.query.filter_by(page_id=id,
                               status=STATES[state],
                               lang=lang,
                               current=True).first()
    if not page:
        abort(403)
    if state == 'publish':
        page.published = True
        old_page = Cms.query.filter_by(page_id=id, status='publish',
                                       lang=lang).first()
        if old_page:
            old_page.status = 'archive'
            db.session.add(old_page)
    page.status = state
    db.session.add(page)
    db.session.commit()
    return redirect(url_for_admin('cms_preview', id=id))
Example #15
0
def org_state(id, state):
    lang = get_admin_lang()
    permission_content(lang)
    if state not in STATES:
        abort(403)
    org = Organisation.query.filter_by(
        org_id=id, status=STATES[state], lang=lang, current=True
    ).first()
    if not org:
        abort(403)
    if state == 'publish':
        org.published = True
        old_org = Organisation.query.filter_by(
            org_id=id, status='publish', lang=lang
        ).first()
        if old_org:
            old_org.status = 'archive'
            db.session.add(old_org)
    org.status = state
    db.session.add(org)
    db.session.commit()
    return redirect(url_for_admin('org_preview', id=id))
Example #16
0
def org_state(id, state):
    lang = get_admin_lang()
    permission_content(lang)
    if state not in STATES:
        abort(403)
    org = Organisation.query.filter_by(org_id=id,
                                       status=STATES[state],
                                       lang=lang,
                                       current=True).first()
    if not org:
        abort(403)
    if state == 'publish':
        org.published = True
        old_org = Organisation.query.filter_by(org_id=id,
                                               status='publish',
                                               lang=lang).first()
        if old_org:
            old_org.status = 'archive'
            db.session.add(old_org)
    org.status = state
    db.session.add(org)
    db.session.commit()
    return redirect(url_for_admin('org_preview', id=id))
Example #17
0
def code_edit(id, cat_type):
    set_menu(cat_type)
    lang = get_admin_lang()
    code = Code.query.filter_by(code_id=id, lang=lang).first()
    if not code and lang != 'en':
        code = Code.query.filter_by(code_id=id, lang='en').first()
        if not code:
            abort(404)
        return code_trans(id, cat_type)
    if not code:
        abort(404)
    if request.method == 'POST':
        code.title = request.form['title']
        code.description = request.form['description']
        db.session.add(code)
        if lang == 'en':
            locked = {
                'active': get_bool('active'),
                'order': get_int('order', 99),
            }
            Code.query.filter_by(code_id=code.code_id).update(locked)
        db.session.commit()
        return redirect(url_for_admin('category_list', cat_type=cat_type))

    if lang != 'en':
        trans = Code.query.filter_by(code_id=code.code_id, lang='en').first()
    else:
        trans = {}
    cats = Category.query.filter_by(lang='en', current=True)
    categories = []
    for cat in cats:
        categories.append({'name': cat.title, 'value': cat.category_id})

    translations = get_code_trans(code.code_id)
    return render_template('admin/code_edit.html', code=code, trans=trans,
                           categories=categories, cat_type=cat_type,
                           translations=translations)
Example #18
0
File: user.py Project: whythawk/hrd
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)
Example #19
0
File: cms.py Project: whythawk/hrd
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)
Example #20
0
def set_menu():
    request.environ['MENU_PATH'] = url_for_admin('org_list')[3:]
Example #21
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)
Example #22
0
def set_menu():
    request.environ['MENU_PATH'] = url_for_admin('org_list')[3:]
Example #23
0
def org_delete(id):
    permission('content_manage')
    Organisation.query.filter_by(org_id=id).delete()
    db.session.commit()
    return redirect(url_for_admin('org_list'))
Example #24
0
def set_menu(cat_type):
    request.environ['MENU_PATH'] = url_for_admin('category_list',
                                                 cat_type=cat_type)[3:]
Example #25
0
File: cms.py Project: rrymrrsn/hrd
def cms_delete(id):
    permission('content_manage')
    Cms.query.filter_by(page_id=id).delete()
    db.session.commit()
    return redirect(url_for_admin('cms_list'))
Example #26
0
def code_delete(code_id, cat_type):
    Code.query.filter_by(code_id=code_id).delete()
    db.session.commit()
    return redirect(url_for_admin('category_list', cat_type=cat_type))
Example #27
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)
Example #28
0
File: cms.py Project: rrymrrsn/hrd
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)
Example #29
0
File: cms.py Project: whythawk/hrd
def cms_delete(id):
    permission('content_manage')
    Cms.query.filter_by(page_id=id).delete()
    db.session.commit()
    return redirect(url_for_admin('cms_list'))
Example #30
0
def org_delete(id):
    permission('content_manage')
    Organisation.query.filter_by(org_id=id).delete()
    db.session.commit()
    return redirect(url_for_admin('org_list'))
Example #31
0
def menu_delete(id):
    permission('content_manage')
    MenuItem.query.filter_by(menu_id=id).delete()
    db.session.commit()
    return redirect(url_for_admin('menu_list'))