Esempio n. 1
0
def v_report_edit(report_id):
    a_report = ReportApi()
    a_answer = AnswerApi()
    a_risk_factor = RiskFactorApi()
    a_lang = LangApi()
    try:
        existing_report = a_report.read(report_id)
    except DatabaseItemDoesNotExist:
        flash(_('No report with id {0} exists.').format(report_id))
        return redirect(url_for('admin.v_report_list'))
    # Do not use fallback_locale for the answers and risk_factor choices: if they don't exist, the administrator
    # must create them.
    report_lang = a_lang.read(existing_report.lang_id)
    return render_template('admin/report/edit.html', report=existing_report,
                           all_risk_factors=a_risk_factor.by_lang(report_lang.lang),
                           all_answers=a_answer.by_lang(report_lang.lang), languages=lang_api.list())
Esempio n. 2
0
class PageApi(GenericApi):
    simple_params = ['menu_link_id', 'content', 'lang_id']
    complex_params = []
    required_params = ['menu_link_id']
    possible_params = simple_params + complex_params

    def __init__(self):
        self.lang_api = LangApi()
        self.menu_link_api = MenuLinkApi()

    def create(self, input_data):
        cleaned_data = self.parse_input_data(input_data)
        if not cleaned_data['lang_id']:
            cleaned_data['lang_id'] = 1
        # Language
        existing_lang = self.lang_api.read(cleaned_data['lang_id'])
        # Menu link
        existing_menu_link = self.menu_link_api.read(cleaned_data['menu_link_id'])
        existing_page = Page.query.filter(and_(Page.menu_link_id == existing_menu_link.id,
                                               Page.lang_id == existing_lang.id)).first()
        # There can not be two pages with the same lang_id and menu_link_id
        if existing_page:
            raise DatabaseItemAlreadyExists(_('A page for menu_link {0} in language {1} already exists.')
                                            .format(cleaned_data['menu_link_id'], cleaned_data['lang_id']))

        new_page = Page(content=cleaned_data['content'])
        new_page.lang = existing_lang
        new_page.menu_link = existing_menu_link
        db.session.add(new_page)
        db.session.commit()
        return new_page

    def read(self, page_id):
        existing_page = Page.query.filter(Page.id == page_id).first()
        if not existing_page:
            raise DatabaseItemDoesNotExist(_e['item_not_exists'].format(Page, page_id))
        return existing_page

    def update(self, page_id, input_data):
        cleaned_data = self.parse_input_data(input_data)
        existing_page = self.read(page_id)
        existing_page.content = cleaned_data['content']
        # Language
        if not cleaned_data['lang_id']:
            cleaned_data['lang_id'] = 1
        existing_lang = self.lang_api.read(cleaned_data['lang_id'])
        # Menu link
        existing_menu_link = self.menu_link_api.read(cleaned_data['menu_link_id'])
        existing_page.lang = existing_lang
        existing_page.menu_link = existing_menu_link
        db.session.commit()
        return existing_page

    def delete(self, page_id):
        existing_page = self.read(page_id)
        db.session.delete(existing_page)
        db.session.commit()
        return True

    def list(self):
        all_pages = Page.query.all()
        return all_pages

    def by_lang(self, lang):
        # Language
        existing_lang = self.lang_api.by_lang(lang)
        return Page.query.filter(Page.lang_id == existing_lang.id).all()

    def by_menu_link(self, menu_link):
        # Menu link
        existing_menu_link = self.menu_link_api.by_menu_link(menu_link)
        return Page.query.filter(Page.menu_link_id == existing_menu_link.id).all()

    def by_menu_link_and_lang(self, menu_link, lang):
        # Language
        existing_lang = self.lang_api.by_lang(lang)
        # Menu link
        existing_menu_link = self.menu_link_api.by_menu_link(menu_link)
        existing_page = Page.query.filter(and_(Page.menu_link_id == existing_menu_link.id,
                                               Page.lang_id == existing_lang.id)).first()
        if not existing_page:
            raise DatabaseItemDoesNotExist(_('A page for menu_link {0} in language {1} does not exist.')
                                           .format(menu_link, lang))
        return existing_page

    def parse_input_data(self, input_data):
        return self.clean_input_data(Page, input_data, possible_params=self.possible_params,
                                     complex_params=self.complex_params,
                                     required_params=self.required_params)
Esempio n. 3
0
class DocumentApi(GenericApi):
    simple_params = [
        'filename', 'original_filename', 'description', 'lang_id', 'name'
    ]
    complex_params = []
    required_params = ['lang_id', 'name']
    possible_params = simple_params + complex_params

    def __init__(self):
        self.lang_api = LangApi()

    def create(self, input_data):
        cleaned_data = self.parse_input_data(input_data)
        if not cleaned_data['lang_id']:
            cleaned_data['lang_id'] = 1
        existing_lang = self.lang_api.read(cleaned_data['lang_id'])
        existing_document = Document.query.filter(
            and_(Document.name == cleaned_data['name'],
                 Document.lang_id == cleaned_data['lang_id'])).first()
        if existing_document:
            raise DatabaseItemAlreadyExists(
                _('No two documents can have the same name and lang_id!'))
        new_document = Document(
            name=cleaned_data['name'],
            filename=cleaned_data['filename'],
            original_filename=cleaned_data['original_filename'],
            description=cleaned_data['description'])
        new_document.lang = existing_lang
        db.session.add(new_document)
        db.session.commit()
        return new_document

    def read(self, item_id):
        existing_document = Document.query.filter(
            Document.id == item_id).first()
        if not existing_document:
            raise DatabaseItemDoesNotExist(_e['item_not_exists'].format(
                Document, item_id))
        return existing_document

    def update(self, item_id, input_data):
        existing_document = self.read(item_id)
        cleaned_data = self.parse_input_data(input_data)
        existing_lang = self.lang_api.read(cleaned_data['lang_id'])
        existing_document = self.update_simple_attributes(
            existing_document,
            self.simple_params,
            cleaned_data,
            to_skip=['lang_id', 'filename', 'original_filename'])
        existing_document.lang = existing_lang
        db.session.commit()
        return existing_document

    def delete(self, item_id):
        existing_document = self.read(item_id)
        db.session.delete(existing_document)
        db.session.commit()
        return True

    def list(self):
        all_documents = Document.query.all()
        return all_documents

    def parse_input_data(self, input_data):
        return self.clean_input_data(Document,
                                     input_data,
                                     possible_params=self.possible_params,
                                     complex_params=self.complex_params,
                                     required_params=self.required_params)

    def set_filenames(self, item_id, filename=None, original_filename=None):
        """
        Update the filename and original_filename attributes
        :param item_id:
        :param filename:
        :param original_filename:
        :return:
        """
        existing_document = self.read(item_id)
        if filename:
            existing_document.filename = filename
        if original_filename:
            existing_document.original_filename = original_filename
        db.session.commit()
        return existing_document

    def by_filename(self, filename):
        """
        Return all items that have filename set to @param filename
        :param filename:
        :return:
        """
        all_documents = Document.query.filter(
            Document.filename == filename).all()
        return all_documents

    def by_lang(self, language):
        lang_api = LangApi()
        db_lang = lang_api.by_lang(language)
        all_documents = Document.query.filter(
            Document.lang_id == db_lang.id).all()
        return all_documents