<commit_before>"OrderPortal: Form pages."

from __future__ import print_function, absolute_import

import logging

import tornado.web

from . import constants
from . import saver
from . import settings
from . import utils
from .fields import Fields
from .requesthandler import RequestHandler


class FormSaver(saver.Saver):
    doctype = constants.FORM

    def initialize(self):
        super(FormSaver, self).initialize()
        self.doc['fields'] = []

    def setup(self):
        self.fields = Fields(self.doc)

    def add_field(self):
        identifier = self.rqh.get_argument('identifier')
        if not constants.ID_RX.match(identifier):
            raise tornado.web.HTTPError(400, reason='invalid identifier')
        if self.rqh.get_argument('type') not in constants.TYPES:
            raise tornado.web.HTTPError(400, reason='invalid type')
        if identifier in self.fields:
            raise tornado.web.HTTPError(409, reason='identifier already exists')
        self.changed['fields'] = self.fields.add(identifier, self.rqh)

    def update_field(self, identifier):
        if identifier not in self.fields:
            raise tornado.web.HTTPError(404, reason='no such field')
        self.changed['fields'] = self.fields.update(identifier, self.rqh)

    def clone_fields(self, form):
        "Clone all fields from the given form."
        for field in form['fields']:
            self.fields.clone(field)
        self.changed['copied'] = "from {}".format(form['_id'])

    def delete_field(self, identifier):
        if identifier not in self.fields:
            raise tornado.web.HTTPError(404, reason='no such field')
        self.fields.delete(identifier)
        self.changed['fields'] = dict(identifier=identifier, action='deleted')


class FormMixin(object):
    "Mixin providing form-related methods."

    def are_fields_editable(self, form):
        "Are the form fields editable? Checks status only."
        return form['status'] == constants.PENDING

    def check_fields_editable(self, form):
        "Check if the form fields can be edited. Checks status only."
        if not self.are_fields_editable(form):
            raise tornado.web.HTTPError(409, reason='form is not editable')


class Forms(RequestHandler):
    "Forms list page."

    @tornado.web.authenticated
    def get(self):
        self.check_admin()
        view = self.db.view('form/modified', descending=True, include_docs=True)
        title = 'Recent forms'
        forms = [r.doc for r in view]
        self.render('forms.html', title=title, forms=forms)


class Form(FormMixin, RequestHandler):
    "Form page."

    @tornado.web.authenticated
    def get(self, iuid):
        self.check_admin()
        form = self.get_entity(iuid, doctype=constants.FORM)
        self.render('form.html',
                    form=form,
                    fields=Fields(form),
                    is_deletable=self.is_deletable(form),
                    are_fields_editable=self.are_fields_editable(form),
                    logs=self.get_logs(form['_id']))

    @tornado.web.authenticated
    def post(self, iuid):
        self.check_xsrf_cookie()
        self.check_admin()
        if self.get_argument('_http_method', None) == 'delete':
            self.delete(iuid)
            return
        raise tornado.web.HTTPError(405, reason='POST only allowed for DELETE')

    @tornado.web.authenticated
    def delete(self, iuid):
        self.check_admin()
        form = self.get_entity(iuid, doctype=constants.FORM)
        if not self.is_deletable(form):
            raise tornado.web.HTTPError(400, reason='form cannot be deleted')
        self.delete_logs(form['_id'])
        self.db.delete(form)
        self.see_other('forms')

    def is_deletable(self, form):
        "Can the form be deleted?."
        if form['status'] == constants.PENDING: return True
        view = self.db.view('order/form')
        if not list(view[form['_id']]): return True
        return False


class FormLogs(RequestHandler):
    "Form log entries page."

    @tornado.web.authenticated
    def get(self, iuid):
        self.check_admin()
        form = self.get_entity(iuid, doctype=constants.FORM)
        self.render('logs.html',
                    title="Logs for form'{}'".format(form['title']),
                    entity=form,
                    logs=self.get_logs(form['_id']))


class FormCreate(RequestHandler):
    "Page for creating an form."

    @tornado.web.authenticated
    def get(self):
        self.check_admin()
        self.render('form_create.html')<commit_msg>Fixed bug allowing delete of enabled form. Closes #182.<commit_after>"OrderPortal: Form pages."

from __future__ import print_function, absolute_import

import logging

import tornado.web

from . import constants
from . import saver
from . import settings
from . import utils
from .fields import Fields
from .requesthandler import RequestHandler


class FormSaver(saver.Saver):
    doctype = constants.FORM

    def initialize(self):
        super(FormSaver, self).initialize()
        self.doc['fields'] = []

    def setup(self):
        self.fields = Fields(self.doc)

    def add_field(self):
        identifier = self.rqh.get_argument('identifier')
        if not constants.ID_RX.match(identifier):
            raise tornado.web.HTTPError(400, reason='invalid identifier')
        if self.rqh.get_argument('type') not in constants.TYPES:
            raise tornado.web.HTTPError(400, reason='invalid type')
        if identifier in self.fields:
            raise tornado.web.HTTPError(409, reason='identifier already exists')
        self.changed['fields'] = self.fields.add(identifier, self.rqh)

    def update_field(self, identifier):
        if identifier not in self.fields:
            raise tornado.web.HTTPError(404, reason='no such field')
        self.changed['fields'] = self.fields.update(identifier, self.rqh)

    def clone_fields(self, form):
        "Clone all fields from the given form."
        for field in form['fields']:
            self.fields.clone(field)
        self.changed['copied'] = "from {}".format(form['_id'])

    def delete_field(self, identifier):
        if identifier not in self.fields:
            raise tornado.web.HTTPError(404, reason='no such field')
        self.fields.delete(identifier)
        self.changed['fields'] = dict(identifier=identifier, action='deleted')


class FormMixin(object):
    "Mixin providing form-related methods."

    def are_fields_editable(self, form):
        "Are the form fields editable? Checks status only."
        return form['status'] == constants.PENDING

    def check_fields_editable(self, form):
        "Check if the form fields can be edited. Checks status only."
        if not self.are_fields_editable(form):
            raise tornado.web.HTTPError(409, reason='form is not editable')


class Forms(RequestHandler):
    "Forms list page."

    @tornado.web.authenticated
    def get(self):
        self.check_admin()
        view = self.db.view('form/modified', descending=True, include_docs=True)
        title = 'Recent forms'
        forms = [r.doc for r in view]
        self.render('forms.html', title=title, forms=forms)


class Form(FormMixin, RequestHandler):
    "Form page."

    @tornado.web.authenticated
    def get(self, iuid):
        self.check_admin()
        form = self.get_entity(iuid, doctype=constants.FORM)
        self.render('form.html',
                    form=form,
                    fields=Fields(form),
                    is_deletable=self.is_deletable(form),
                    are_fields_editable=self.are_fields_editable(form),
                    logs=self.get_logs(form['_id']))

    @tornado.web.authenticated
    def post(self, iuid):
        self.check_xsrf_cookie()
        self.check_admin()
        if self.get_argument('_http_method', None) == 'delete':
            self.delete(iuid)
            return
        raise tornado.web.HTTPError(405, reason='POST only allowed for DELETE')

    @tornado.web.authenticated
    def delete(self, iuid):
        self.check_admin()
        form = self.get_entity(iuid, doctype=constants.FORM)
        if not self.is_deletable(form):
            raise tornado.web.HTTPError(400, reason='form cannot be deleted')
        self.delete_logs(form['_id'])
        self.db.delete(form)
        self.see_other('forms')

    def is_deletable(self, form):
        "Can the form be deleted?."
        if form['status'] == constants.PENDING: return True
        if form['status'] == constants.ENABLED: return False
        view = self.db.view('order/form')
        if not list(view[form['_id']]): return True
        return False


class FormLogs(RequestHandler):
    "Form log entries page."

    @tornado.web.authenticated
    def get(self, iuid):
        self.check_admin()
        form = self.get_entity(iuid, doctype=constants.FORM)
        self.render('logs.html',
                    title="Logs for form'{}'".format(form['title']),
                    entity=form,
                    logs=self.get_logs(form['_id']))


class FormCreate(RequestHandler):
    "Page for creating an form."

    @tornado.web.authenticated
    def get(self):
        self.check_admin()
        self.render('form_create.html')
{'fraction_non_alphanumeric': 0.11514758352254298, 'fraction_numerical': 0.005514109633473889, 'mean_word_length': 2.6503551696921863, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7418823', 'n_tokens_mistral': 2786, 'n_tokens_neox': 2689, 'n_words': 630}