示例#1
0
    def set_dates(self, form: FlaskForm) -> None:
        if not hasattr(form, 'begin_year_from'):
            return
        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:
            self.begin_comment = form.begin_comment.data
            self.begin_from = form_to_datetime64(
                form.begin_year_from.data,
                form.begin_month_from.data,
                form.begin_day_from.data)
            self.begin_to = form_to_datetime64(
                form.begin_year_to.data,
                form.begin_month_to.data,
                form.begin_day_to.data,
                to_date=True)

        if form.end_year_from.data:
            self.end_comment = form.end_comment.data
            self.end_from = form_to_datetime64(
                form.end_year_from.data,
                form.end_month_from.data,
                form.end_day_from.data)
            self.end_to = form_to_datetime64(
                form.end_year_to.data,
                form.end_month_to.data,
                form.end_day_to.data,
                to_date=True)
示例#2
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:
         self.begin_from = form_to_datetime64(
             form.begin_year_from.data,
             form.begin_month_from.data,
             form.begin_day_from.data)
         self.begin_to = 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:
         self.end_from = form_to_datetime64(
             form.end_year_from.data,
             form.end_month_from.data,
             form.end_day_from.data)
         self.end_to = 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 validate(self) -> bool:
     valid = FlaskForm.validate(self)
     from_date = form_to_datetime64(
         self.begin_year.data,
         self.begin_month.data,
         self.begin_day.data)
     to_date = 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
示例#4
0
def validate(self: FlaskForm) -> bool:
    valid = FlaskForm.validate(self)

    # Dates
    if hasattr(self, 'begin_year_from'):

        # Check date format, put in list "dates" for further validation
        dates = {}
        for prefix in ['begin_', 'end_']:
            if getattr(self, prefix + 'year_to').data \
                    and not getattr(self, prefix + 'year_from').data:
                getattr(self, prefix + 'year_from').errors.append(
                    _("Required for time span"))
                valid = False
            for postfix in ['_from', '_to']:
                if getattr(self, prefix + 'year' + postfix).data:
                    date_ = 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 '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

    # File
    if request.files:
        files = request.files.getlist('file')
        ext = session['settings']['file_upload_allowed_extension']
        for file_ in files:
            if not file_:  # pragma: no cover
                self.file.errors.append(_('no file to upload'))
                valid = False
            elif not ('.' in file_.filename
                      and file_.filename.rsplit('.', 1)[1].lower() in ext):
                self.file.errors.append(_('file type not allowed'))
                valid = False

    # Super event
    if hasattr(self, 'event') and hasattr(self, 'event_id'):
        if self.event.data:
            if str(self.event.data) == str(self.event_id.data):
                self.event.errors.append(_('error node self as super'))
                valid = False

    # External reference systems
    for field_id, field in self.__dict__.items():
        if field_id.startswith('reference_system_id_') and field.data:
            if not getattr(self, field_id.replace('id_', 'precision_')).data:
                valid = False
                field.errors.append(uc_first(_('precision required')))
            if field.label.text == 'Wikidata':
                if field.data[0].upper() != 'Q' or not field.data[1:].isdigit():
                    field.errors.append(uc_first(_('wrong id format') + '.'))
                    valid = False
                else:
                    field.data = uc_first(field.data)
            if field.label.text == 'GeoNames' and not field.data.isnumeric():
                field.errors.append(uc_first(_('wrong id format') + '.'))
                valid = False

    # Membership
    if hasattr(self, 'member_origin_id'):
        member = getattr(self, 'actor') \
            if hasattr(self, 'actor') else getattr(self, 'group')
        if self.member_origin_id.data in ast.literal_eval(member.data):
            member.errors.append(_("Can't link to itself."))
            valid = False

    # Actor actor relation
    if hasattr(self, 'relation_origin_id'):
        if self.relation_origin_id.data in ast.literal_eval(self.actor.data):
            self.actor.errors.append(_("Can't link to itself."))
            valid = False
    return valid
示例#5
0
def search(form: FlaskForm) -> ValuesView[Entity]:
    if not form.term.data:
        return {}.values()
    classes = form.classes.data
    if 'person' in classes:
        classes.append('actor_appellation')
    if 'place' in classes:
        classes.append('appellation')

    # Repopulate date fields with autocompleted values
    from_date = form_to_datetime64(form.begin_year.data, form.begin_month.data,
                                   form.begin_day.data)
    to_date = form_to_datetime64(form.end_year.data,
                                 form.end_month.data,
                                 form.end_day.data,
                                 to_date=True)
    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])

    # Get search results
    entities = []
    for row in Db.search(form.term.data, form.classes.data, form.desc.data,
                         form.own.data, current_user.id):
        if row['system_class'] == 'actor_appellation':  # Found in actor alias
            entity = Link.get_linked_entity(row['id'], 'P131', True)
        elif row['system_class'] == 'appellation':  # Found in place alias
            entity = Link.get_linked_entity(row['id'], 'P1', True)
        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