コード例 #1
0
 def validate(self) -> bool:
     valid = Form.validate(self)
     from_date = DateMapper.form_to_datetime64(self.begin_year.data,
                                               self.begin_month.data,
                                               self.begin_day.data)
     to_date = DateMapper.form_to_datetime64(self.end_year.data,
                                             self.end_month.data,
                                             self.end_day.data, True)
     if from_date and to_date and from_date > to_date:
         self.begin_year.errors.append(
             _('Begin dates cannot start after end dates.'))
         valid = False
     return valid
コード例 #2
0
ファイル: link.py プロジェクト: craws/OpenAtlas
 def set_dates(self, form):
     self.begin_from = None
     self.begin_to = None
     self.begin_comment = None
     self.end_from = None
     self.end_to = None
     self.end_comment = None
     if form.begin_year_from.data:  # Only if begin year is set create a begin date or time span
         self.begin_from = DateMapper.form_to_datetime64(
             form.begin_year_from.data, form.begin_month_from.data, form.begin_day_from.data)
         self.begin_to = DateMapper.form_to_datetime64(
             form.begin_year_to.data, form.begin_month_to.data, form.begin_day_to.data, True)
         self.begin_comment = form.begin_comment.data
     if form.end_year_from.data:  # Only if end year is set create a year date or time span
         self.end_from = DateMapper.form_to_datetime64(
             form.end_year_from.data, form.end_month_from.data, form.end_day_from.data)
         self.end_to = DateMapper.form_to_datetime64(
             form.end_year_to.data, form.end_month_to.data, form.end_day_to.data, True)
         self.end_comment = form.end_comment.data
コード例 #3
0
 def set_dates(self, form: FlaskForm) -> None:
     self.begin_from = None
     self.begin_to = None
     self.begin_comment = None
     self.end_from = None
     self.end_to = None
     self.end_comment = None
     if form.begin_year_from.data:  # Only if begin year is set create a begin date or time span
         self.begin_from = DateMapper.form_to_datetime64(
             form.begin_year_from.data, form.begin_month_from.data, form.begin_day_from.data)
         self.begin_to = DateMapper.form_to_datetime64(
             form.begin_year_to.data, form.begin_month_to.data, form.begin_day_to.data, True)
         self.begin_comment = form.begin_comment.data
     if form.end_year_from.data:  # Only if end year is set create a year date or time span
         self.end_from = DateMapper.form_to_datetime64(
             form.end_year_from.data, form.end_month_from.data, form.end_day_from.data)
         self.end_to = DateMapper.form_to_datetime64(
             form.end_year_to.data, form.end_month_to.data, form.end_day_to.data, True)
         self.end_comment = form.end_comment.data
コード例 #4
0
    def validate(self) -> bool:
        valid = FlaskForm.validate(self)

        # Check date format, if valid put dates into a list called "dates"
        dates = {}
        for prefix in ['begin_', 'end_']:
            for postfix in ['_from', '_to']:
                if getattr(self, prefix + 'year' + postfix).data:
                    date = DateMapper.form_to_datetime64(
                        getattr(self, prefix + 'year' + postfix).data,
                        getattr(self, prefix + 'month' + postfix).data,
                        getattr(self, prefix + 'day' + postfix).data)
                    if not date:
                        getattr(self, prefix + 'day' + postfix).errors.append(
                            _('not a valid date'))
                        valid = False
                    else:
                        dates[prefix + postfix.replace('_', '')] = date

        # Check for valid date combination e.g. begin not after end
        if valid:
            for prefix in ['begin', 'end']:
                if prefix + '_from' in dates and prefix + '_to' in dates:
                    if dates[prefix + '_from'] > dates[prefix + '_to']:
                        field = getattr(self, prefix + '_day_from')
                        field.errors.append(
                            _('First date cannot be after second.'))
                        valid = False
        if valid and 'begin_from' in dates and 'end_from' in dates:
            field = getattr(self, 'begin_day_from')
            if len(dates) == 4:  # All dates are used
                if dates['begin_from'] > dates['end_from'] or dates[
                        'begin_to'] > dates['end_to']:
                    field.errors.append(
                        _('Begin dates cannot start after end dates.'))
                    valid = False
            else:
                first = dates['begin_to'] if 'begin_to' in dates else dates[
                    'begin_from']
                second = dates['end_from'] if 'end_from' in dates else dates[
                    'end_to']
                if first > second:
                    field.errors.append(
                        _('Begin dates cannot start after end dates.'))
                    valid = False
        return valid
コード例 #5
0
    def validate(self, extra_validators=None):
        valid = Form.validate(self)
        fields = {}  # put date form values in a dictionary
        for name in ['begin', 'end']:
            for item in ['year', 'month', 'day', 'year2', 'month2', 'day2']:
                value = getattr(self, 'date_' + name + '_' + item).data
                fields[name + '_' + item] = int(value) if value else ''

        # Check date format, if valid put dates into a dictionary
        dates = {}
        for name in ['begin', 'end']:
            for postfix in ['', '2']:
                if fields[name + '_' + 'year' + postfix]:
                    date = DateMapper.form_to_datetime64(
                        fields[name + '_' + 'year' + postfix],
                        fields[name + '_' + 'month' + postfix],
                        fields[name + '_' + 'day' + postfix])
                    if not date:
                        field = getattr(self, 'date_' + name + '_' + 'day' + postfix)
                        field.errors.append(_('not a valid date'))
                        valid = False
                    else:
                        dates[name + postfix] = date

        # Check for valid date combination e.g. begin not after end
        if valid:
            for name in ['begin', 'end']:
                if name in dates and name + '2' in dates:
                    if dates[name] > dates[name + '2']:
                        field = getattr(self, 'date_' + name + '_day')
                        field.errors.append(_('First date cannot be after second.'))
                        valid = False
        if valid and 'begin' in dates and 'end' in dates:
            field = getattr(self, 'date_begin_day')
            if len(dates) == 4:  # All dates are used
                if dates['begin'] > dates['end'] or dates['begin2'] > dates['end2']:
                    field.errors.append(_('Begin dates cannot start after end dates.'))
                    valid = False
            else:
                first = dates['begin2'] if 'begin2' in dates else dates['begin']
                second = dates['end'] if 'end' in dates else dates['end2']
                if first > second:
                    field.errors.append(_('Begin dates cannot start after end dates.'))
                    valid = False
        return valid
コード例 #6
0
ファイル: date.py プロジェクト: craws/OpenAtlas
    def validate(self, extra_validators=None):
        valid = Form.validate(self)

        # Check date format, if valid put dates into a list called "dates"
        dates = {}
        for prefix in ['begin_', 'end_']:
            for postfix in ['_from', '_to']:
                if getattr(self, prefix + 'year' + postfix).data:
                    date = DateMapper.form_to_datetime64(
                        getattr(self, prefix + 'year' + postfix).data,
                        getattr(self, prefix + 'month' + postfix).data,
                        getattr(self, prefix + 'day' + postfix).data)
                    if not date:
                        getattr(self, prefix + 'day' + postfix).errors.append(_('not a valid date'))
                        valid = False
                    else:
                        dates[prefix + postfix.replace('_', '')] = date

        # Check for valid date combination e.g. begin not after end
        if valid:
            for prefix in ['begin', 'end']:
                if prefix + '_from' in dates and prefix + '_to' in dates:
                    if dates[prefix + '_from'] > dates[prefix + '_to']:
                        field = getattr(self,  prefix + '_day_from')
                        field.errors.append(_('First date cannot be after second.'))
                        valid = False
        if valid and 'begin_from' in dates and 'end_from' in dates:
            field = getattr(self, 'begin_day_from')
            if len(dates) == 4:  # All dates are used
                if dates['begin_from'] > dates['end_from'] or dates['begin_to'] > dates['end_to']:
                    field.errors.append(_('Begin dates cannot start after end dates.'))
                    valid = False
            else:
                first = dates['begin_to'] if 'begin_to' in dates else dates['begin_from']
                second = dates['end_from'] if 'end_from' in dates else dates['end_to']
                if first > second:
                    field.errors.append(_('Begin dates cannot start after end dates.'))
                    valid = False
        return valid
コード例 #7
0
    def search(form: FlaskForm) -> ValuesView[Entity]:
        if not form.term.data:
            return {}.values()
        sql = EntityMapper.build_sql() + """
            {user_clause} WHERE (LOWER(e.name) LIKE LOWER(%(term)s) {description_clause})
            AND {user_clause2} (""".format(
            user_clause="""
                LEFT JOIN web.user_log ul ON e.id = ul.entity_id """
            if form.own.data else '',
            description_clause="""
                OR lower(e.description) LIKE lower(%(term)s) """
            if form.desc.data else '',
            user_clause2=' ul.user_id = %(user_id)s AND '
            if form.own.data else '')
        sql_where = []
        for name in form.classes.data:
            if name in ['source', 'event']:
                sql_where.append("e.class_code IN ({codes})".format(
                    codes=str(app.config['CLASS_CODES'][name])[1:-1]))
            elif name == 'actor':
                codes = app.config['CLASS_CODES'][name] + ['E82']  # Add alias
                sql_where.append(" e.class_code IN ({codes})".format(
                    codes=str(codes)[1:-1]))
            elif name == 'place':
                sql_where.append(
                    "(e.class_code = 'E41' OR e.system_type = 'place')")
            elif name == 'feature':
                sql_where.append("e.system_type = 'feature'")
            elif name == 'stratigraphic unit':
                sql_where.append("e.system_type = 'stratigraphic unit'")
            elif name == 'find':
                sql_where.append("e.class_code = 'E22'")
            elif name == 'reference':
                sql_where.append(
                    " e.class_code IN ({codes}) AND e.system_type != 'file'".
                    format(codes=str(app.config['CLASS_CODES']['reference'])
                           [1:-1]))
            elif name == 'file':
                sql_where.append(" e.system_type = 'file'")
        sql += ' OR '.join(sql_where) + ") GROUP BY e.id ORDER BY e.name;"
        g.execute(sql, {
            'term': '%' + form.term.data + '%',
            'user_id': current_user.id
        })

        # Prepare date filter
        from_date = DateMapper.form_to_datetime64(form.begin_year.data,
                                                  form.begin_month.data,
                                                  form.begin_day.data)
        to_date = DateMapper.form_to_datetime64(form.end_year.data,
                                                form.end_month.data,
                                                form.end_day.data, True)

        # Refill form in case dates were completed
        if from_date:
            string = str(from_date)
            if string.startswith('-') or string.startswith('0000'):
                string = string[1:]
            parts = string.split('-')
            form.begin_month.raw_data = None
            form.begin_day.raw_data = None
            form.begin_month.data = int(parts[1])
            form.begin_day.data = int(parts[2])

        if to_date:
            string = str(to_date)
            if string.startswith('-') or string.startswith('0000'):
                string = string[1:]  # pragma: no cover
            parts = string.split('-')
            form.end_month.raw_data = None
            form.end_day.raw_data = None
            form.end_month.data = int(parts[1])
            form.end_day.data = int(parts[2])

        entities = []
        for row in g.cursor.fetchall():
            entity = None
            if row.class_code == 'E82':  # If found in actor alias
                entity = LinkMapper.get_linked_entity(row.id, 'P131', True)
            elif row.class_code == 'E41':  # If found in place alias
                entity = LinkMapper.get_linked_entity(row.id, 'P1', True)
            elif row.class_code == 'E18':
                if row.system_type in form.classes.data:
                    entity = Entity(row)
            else:
                entity = Entity(row)

            if not entity:  # pragma: no cover
                continue

            if not from_date and not to_date:
                entities.append(entity)
                continue

            # Date criteria present but entity has no dates
            if not entity.begin_from and not entity.begin_to and not entity.end_from \
                    and not entity.end_to:
                if form.include_dateless.data:  # Include dateless entities
                    entities.append(entity)
                continue

            # Check date criteria
            dates = [
                entity.begin_from, entity.begin_to, entity.end_from,
                entity.end_to
            ]
            begin_check_ok = False
            if not from_date:
                begin_check_ok = True  # pragma: no cover
            else:
                for date in dates:
                    if date and date >= from_date:
                        begin_check_ok = True

            end_check_ok = False
            if not to_date:
                end_check_ok = True  # pragma: no cover
            else:
                for date in dates:
                    if date and date <= to_date:
                        end_check_ok = True

            if begin_check_ok and end_check_ok:
                entities.append(entity)
        return {d.id: d
                for d in entities
                }.values()  # Remove duplicates before returning
コード例 #8
0
ファイル: entity.py プロジェクト: stefaneichert/openAtlas
    def search(form):
        if not form.term.data:
            return []
        sql = EntityMapper.build_sql() + """
            {user_clause} WHERE (LOWER(e.name) LIKE LOWER(%(term)s) {description_clause})
            AND {user_clause2} (""".format(
            user_clause="""
                LEFT JOIN web.user_log ul ON e.id = ul.entity_id """ if form.own.data else '',
            description_clause="""
                OR lower(e.description) LIKE lower(%(term)s) """ if form.desc.data else '',
            user_clause2=' ul.user_id = %(user_id)s AND ' if form.own.data else '')
        sql_where = []
        for name in form.classes.data:
            if name in ['source', 'event']:
                sql_where.append("e.class_code IN ({codes})".format(
                    codes=str(app.config['CLASS_CODES'][name])[1:-1]))
            elif name == 'actor':
                codes = app.config['CLASS_CODES'][name] + ['E82']  # Add alias
                sql_where.append(" e.class_code IN ({codes})".format(codes=str(codes)[1:-1]))
            elif name == 'place':
                sql_where.append("(e.class_code = 'E41' OR e.system_type = 'place')")
            elif name == 'feature':
                sql_where.append("e.system_type = 'feature'")
            elif name == 'stratigraphic unit':
                sql_where.append("e.system_type = 'stratigraphic unit'")
            elif name == 'find':
                sql_where.append("e.class_code = 'E22'")
            elif name == 'reference':
                sql_where.append(" e.class_code IN ({codes}) AND e.system_type != 'file'".format(
                    codes=str(app.config['CLASS_CODES']['reference'])[1:-1]))
            elif name == 'file':
                sql_where.append(" e.system_type = 'file'")
        sql += ' OR '.join(sql_where) + ") GROUP BY e.id ORDER BY e.name;"
        g.cursor.execute(sql, {'term': '%' + form.term.data + '%', 'user_id': current_user.id})
        debug_model['div sql'] += 1

        # Prepare date filter
        from_date = DateMapper.form_to_datetime64(form.begin_year.data, form.begin_month.data,
                                                  form.begin_day.data)
        to_date = DateMapper.form_to_datetime64(form.end_year.data, form.end_month.data,
                                                form.end_day.data, True)

        # Refill form in case dates were completed
        if from_date:
            string = str(from_date)
            if string.startswith('-') or string.startswith('0000'):
                string = string[1:]
            parts = string.split('-')
            form.begin_month.raw_data = None
            form.begin_day.raw_data = None
            form.begin_month.data = int(parts[1])
            form.begin_day.data = int(parts[2])

        if to_date:
            string = str(to_date)
            if string.startswith('-') or string.startswith('0000'):
                string = string[1:]  # pragma: no cover
            parts = string.split('-')
            form.end_month.raw_data = None
            form.end_day.raw_data = None
            form.end_month.data = int(parts[1])
            form.end_day.data = int(parts[2])

        entities = []
        for row in g.cursor.fetchall():
            entity = None
            if row.class_code == 'E82':  # If found in actor alias
                entity = LinkMapper.get_linked_entity(row.id, 'P131', True)
            elif row.class_code == 'E41':  # If found in place alias
                entity = LinkMapper.get_linked_entity(row.id, 'P1', True)
            elif row.class_code == 'E18':
                if row.system_type in form.classes.data:
                    entity = Entity(row)
            else:
                entity = Entity(row)

            if not entity:  # pragma: no cover
                continue

            if not from_date and not to_date:
                entities.append(entity)
                continue

            # Date criteria present but entity has no dates
            if not entity.begin_from and not entity.begin_to and not entity.end_from \
                    and not entity.end_to:
                if form.include_dateless.data:  # Include dateless entities
                    entities.append(entity)
                continue

            # Check date criteria
            dates = [entity.begin_from, entity.begin_to, entity.end_from, entity.end_to]
            begin_check_ok = False
            if not from_date:
                begin_check_ok = True  # pragma: no cover
            else:
                for date in dates:
                    if date and date >= from_date:
                        begin_check_ok = True

            end_check_ok = False
            if not to_date:
                end_check_ok = True  # pragma: no cover
            else:
                for date in dates:
                    if date and date <= to_date:
                        end_check_ok = True

            if begin_check_ok and end_check_ok:
                entities.append(entity)

        return {d.id: d for d in entities}.values()  # Remove duplicates before returning