Ejemplo n.º 1
0
def delete(path):
    if not ModuleAPI.can_write('page'):
        return abort(403)

    page = Page.get_by_path(path)
    if not page:
        flash(_('The page you tried to delete does not exist.'), 'danger')
        return redirect(url_for('page.get_page', path=path))
        abort(404)
    rev = page.get_latest_revision()

    class DeleteForm(Form):
        title = StringField(_('Page title'))

    form = DeleteForm(request.form)

    if form.validate_on_submit():
        if rev.title == form.title.data:
            db.session.delete(page)
            db.session.commit()
            flash(_('The page has been deleted'), 'success')
            return redirect(url_for('home.home'))
        else:
            flash(_('The given title does not match the page title.'),
                  'warning')
    else:
        flash_form_errors(form)

    return render_template('page/delete.htm', rev=rev, form=form)
Ejemplo n.º 2
0
    def get_seo_fields(language="nl", module_name=None, request_path=None):
        """Get the seo fields as dict."""
        # Check if the module and path are set.
        if module_name is None:
            module_name = request.blueprint

        if request_path is None:
            request_path = request.path

        # Check which type of seo fields should be retrieved, based on
        # the module name.
        if module_name == "activity":
            # Get activity id
            activity_id = re.search(r"\/([0-9]+)\/", request_path)

            # Check seo existance
            if activity_id is not None:
                # Get the seo object of an activity
                seo = SEO.get_by_activity(activity_id.group(1))
            else:
                # No seo was found for this activity
                seo = None

        elif module_name == "page":
            # Retrieve the page for its id
            path = request_path[1:]
            page = Page.get_by_path(path)

            # Retrieve the revision by page id
            if page is not None:
                seo = SEO.get_by_page(page.id)
            else:
                seo = None
        else:
            # Retrieve seo fields based on the module name.
            seo = SEO.get_by_url(module_name)

        # Retrieve the seo fields based on the seo object
        # or global set values.
        if seo is not None:

            # Retrieve the language specific SEO fields
            if language == "nl":
                return {"title": seo.nl_title, "description": seo.nl_description, "tags": seo.nl_tags}
            elif language == "en":
                return {"title": seo.en_title, "description": seo.en_description, "tags": seo.en_tags}
        else:
            # TODO, good standard tags
            return {
                "title": "via",
                "description": "Studievereniging via - Informatica, "
                + "Informatiekunde, Informatica, "
                + "University of Amsterdam",
                "tags": "Studievereniging,via, informatica, " + "informatiekunde, University of Amsterdam",
            }
Ejemplo n.º 3
0
    def get_resources(module_name=None, request_path=None):
        # Check if the module and path are set.
        if module_name is None:
            module_name = request.blueprint

        if request_path is None:
            request_path = request.path

        # Empty resources for all resources that are not retrieved.
        page = None
        page_id = None
        activity = None
        activity_id = None
        path = None

        # Check which type of seo fields should be retrieved, based on
        # the module name.
        if module_name == "activity":
            # Regex search for acitivity id
            activity_result = re.search(r"\/([0-9]+)\/", request_path)

            # Fetch id from regex
            activity_id = activity_result.group(1)

            # Find activity
            activity = Activity.query.filter(Activity.id == activity_id).first()
            print("lalalala")

        elif module_name == "page":
            # Retrieve the page for its id
            path = request_path[1:]
            page = Page.get_by_path(path)

            # Retrieve the revision by page id
            if page is not None:
                page_id = page.id
                print("lalala")
        else:
            # Retrieve seo fields based on the module name.
            path = module_name

        return {"page": page, "page_id": page_id, "activity": activity, "activity_id": activity_id, "url": path}
Ejemplo n.º 4
0
    def get_seo(self, module_name=None, request_path=None):
        # Check if the module and path are set.
        if module_name is None:
            module_name = request.blueprint

        if request_path is None:
            request_path = request.path

        # Check which type of seo fields should be retrieved, based on
        # the module name.
        if module_name == "activity":
            # Get activity id
            activity_id = re.search(r"\/([0-9]+)\/", request_path)

            # Check seo existance
            if activity_id is not None:
                # Get the seo object of an activity
                return SEO.get_by_activity(activity_id.group(1))
            else:
                # No seo was found for this activity
                return None

        elif module_name == "page":
            # Retrieve the page for its id
            path = request_path[1:]
            page = Page.get_by_path(path)

            # Retrieve the revision by page id
            if page is not None:
                return SEO.get_by_page(page.id)
            else:
                return None
        else:
            # Retrieve seo fields based on the module name.
            seo = SEO.get_by_url(module_name)

            return seo

        return None
Ejemplo n.º 5
0
def get_page(path=''):
    path = Page.strip_path(path)
    page = Page.get_by_path(path)

    if not page:
        # Try if this might be a redirect.
        print("not page")
        redirection = Redirect.query.filter(Redirect.fro == path).first()
        if redirection:

            # get GET parameters so they can be applied to the redirected
            # URL
            if request.args:
                redir_url = redirection.to + '?'
                for key in request.args:
                    redir_url += key + '=' + \
                        request.args[key] + '&'
                print(redir_url)

                # this is necssary to prevent incorrect escaping
                return redirect(iri_to_uri(redir_url))

            return redirect(redirection.to)

        return abort(404)

    if not PageAPI.can_read(page):
        return abort(403)

    revision = page.get_latest_revision()

    if not revision:
        return abort(500)

    return render_template('%s/view_single.htm' % (page.type), page=page,
                           revision=revision, title=revision.title,
                           context=revision.__class__.context)
Ejemplo n.º 6
0
def get_page_history(path=''):
    form = HistoryPageForm(request.form)

    page = Page.get_by_path(path)

    if not page:
        return abort(404)

    if not PageAPI.can_write(page):
        return abort(403)

    revisions = page.revision_cls.get_query()\
        .filter(page.revision_cls.page_id == page.id)\
        .all()

    form.previous.choices = [(revision.id, '') for revision in revisions]
    form.current.choices = [(revision.id, '') for revision in revisions]

    if form.validate_on_submit():
        previous = request.form['previous']
        current = request.form['current']

        previous_revision = page.revision_cls.get_query()\
            .filter(page.revision_cls.id == previous).first()
        current_revision = page.revision_cls.get_query()\
            .filter(page.revision_cls.id == current).first()

        prev = previous_revision.get_comparable()
        cur = current_revision.get_comparable()
        diff = htmldiff(prev, cur)

        return render_template('page/compare_page_history.htm', diff=diff)

    return render_template('page/get_page_history.htm', form=form,
                           revisions=zip(revisions, form.previous,
                                         form.current))
Ejemplo n.º 7
0
def edit_page(path=''):
    if not ModuleAPI.can_write('page'):
        return abort(403)

    page = Page.get_by_path(path)
    form = request.form

    if page:
        revision = page.get_latest_revision()

        # Add the `needs_paid` option to the revision, so it will be inside
        # the form.
        revision.needs_paid = revision.page.needs_paid

        form = PageForm(form, revision)
    else:
        form = PageForm()

    groups = Group.query.all()

    # on page submit (edit or create)
    if form.validate_on_submit():
        # if there was no page we want to create an entire new page (and not
        # just a revision)
        if not page:
            page = Page(path)

        page.needs_paid = form['needs_paid'].data

        db.session.add(page)
        db.session.commit()

        custom_form_id = int(form.custom_form_id.data)
        if custom_form_id == 0:
            custom_form_id = None

        new_revision = PageRevision(page,
                                    form.nl_title.data.strip(),
                                    form.en_title.data.strip(),
                                    form.comment.data.strip(),
                                    current_user,
                                    form.nl_content.data.strip(),
                                    form.en_content.data.strip(),
                                    'filter_html' in form,
                                    custom_form_id)

        db.session.add(new_revision)
        db.session.commit()

        # Enter permission in db
        for form_entry, group in zip(form.permissions, groups):
            permission_entry = PagePermission.query\
                .filter(PagePermission.group_id == group.id,
                        PagePermission.page_id == page.id).first()

            permission_level = form_entry.select.data

            if permission_entry:
                permission_entry.permission = permission_level
            else:
                permission_entry = PagePermission(group.id, page.id,
                                                  permission_level)

            db.session.add(permission_entry)
            db.session.commit()

        flash(_('The page has been saved'), 'success')

        # redirect newly created page
        return redirect(url_for('page.get_page', path=path))
    else:
        flash_form_errors(form)
        for group in groups:
            permission = None
            if page:
                permission = PagePermission.query\
                    .filter(PagePermission.group_id == group.id,
                            PagePermission.page_id == page.id)\
                    .first()

            if permission:
                form.permissions\
                    .append_entry({'select': permission.permission})
            else:
                form.permissions.append_entry({})

    return render_template('page/edit_page.htm', page=page, form=form,
                           path=path, groups=zip(groups, form.permissions))
Ejemplo n.º 8
0
def edit_committee(committee=''):
    if not ModuleAPI.can_write('committee'):
        return abort(403)

    path = 'commissie/' + committee

    page = Page.get_by_path(path)

    form = request.form
    if page:
        revision = page.get_latest_revision()
        form = CommitteeForm(form, revision)
    else:
        revision = None
        form = CommitteeForm()

    try:
        url_group_id = int(request.args.get('group_id', None))
    except:
        url_group_id = None

    form.group_id.choices = [(group.id, group.name) for group in
                             Group.query.order_by(Group.name).all()]

    if len(request.form) == 0:
        if revision:
            selected_group_id = revision.group_id
        elif url_group_id is not None:
            selected_group_id = url_group_id
        else:
            selected_group_id = form.group_id.choices[0][0]
    else:
        selected_group_id = int(form.group_id.data)

    form.group_id.data = selected_group_id

    selected_group = Group.query.get(selected_group_id)
    form.coordinator_id.choices = [
        (user.id, user.name) for user in
        selected_group.users.order_by(User.first_name, User.last_name).all()]

    form.nl_title.data = selected_group.name

    if form.validate_on_submit():
        committee_nl_title = form.nl_title.data.strip()
        committee_en_title = form.en_title.data.strip()

        if not page:
            root_entry_url = url_for('committee.list').rstrip('/')
            root_entry = NavigationEntry.query\
                .filter(NavigationEntry.url == root_entry_url)\
                .first()

            # Check whether the root navigation entry exists.
            if not root_entry:
                last_root_entry = NavigationEntry.query\
                    .filter(NavigationEntry.parent_id == None)\
                    .order_by(NavigationEntry.position.desc()).first()  # noqa

                root_entry_position = 1
                if last_root_entry:
                    root_entry_position = last_root_entry.position + 1

                root_entry = NavigationEntry(
                    None, 'Commissies', 'Committees', root_entry_url, False,
                    False, root_entry_position)

                db.session.add(root_entry)
                db.session.commit()

            page = Page(path, 'committee')

            # Never needs paid.
            page.needs_paid = False

            # Create a navigation entry for the new committee.
            last_navigation_entry = NavigationEntry.query\
                .filter(NavigationEntry.parent_id == root_entry.id)\
                .first()

            entry_position = 1
            if last_navigation_entry:
                entry_position = last_navigation_entry.position + 1

            navigation_entry = NavigationEntry(
                root_entry, committee_nl_title, committee_en_title, '/' + path,
                False, False, entry_position)

            db.session.add(navigation_entry)
            db.session.commit()

            # Sort these navigation entries.
            NavigationAPI.alphabeticalize(root_entry)

            # Assign the navigation entry to the new page (committee).
            page.navigation_entry_id = navigation_entry.id

            db.session.add(page)
            db.session.commit()

            # Assign read rights to all, and edit rights to BC.
            all_group = Group.query.filter(Group.name == 'all').first()
            bc_group = Group.query.filter(Group.name == 'BC').first()

            all_entry = PagePermission(all_group.id, page.id, 1)
            bc_entry = PagePermission(bc_group.id, page.id, 2)

            db.session.add(all_entry)
            db.session.add(bc_entry)
            db.session.commit()
        else:
            # If the committee's title has changed, the navigation needs to be
            # updated. Look for the entry, compare the titles, and change where
            # necessary.
            entry = NavigationEntry.query\
                .filter(NavigationEntry.url == '/' + path).first()
            if entry.title != committee_nl_title:
                entry.title = committee_nl_title
                db.session.add(entry)
                db.session.commit()

        group_id = int(form.group_id.data)
        coordinator_id = int(form.coordinator_id.data)

        # Add coordinator to BC
        bc_group = Group.query.filter(Group.name == "BC").first()
        if bc_group is not None:
            new_coordinator = User.query.filter(
                User.id == coordinator_id).first()
            bc_group.add_user(new_coordinator)

        new_revision = CommitteeRevision(
            page, committee_nl_title, committee_en_title,
            form.comment.data.strip(), current_user.id,
            form.nl_description.data.strip(), form.en_description.data.strip(),
            group_id, coordinator_id, form.interim.data)

        db.session.add(new_revision)
        db.session.commit()

        flash(_('The committee has been saved.'), 'success')

        return redirect(url_for('page.get_page', path=path))
    else:
        flash_form_errors(form)

    return render_template('committee/edit.htm', page=page,
                           form=form, path=path)