Ejemplo n.º 1
0
 def __init__(self, event, contrib, allow_claimed_files=False, **kwargs):
     self.event = event
     self.contrib = contrib
     keys_field = ModelField(EditingFileType, get_query=lambda m: m.query.with_parent(event))
     values_field = FilesField(required=True, allow_claimed=allow_claimed_files)
     validators = kwargs.pop('validate', []) + [self.validate_files]
     super(EditingFilesField, self).__init__(keys=keys_field, values=values_field, validate=validators, **kwargs)
Ejemplo n.º 2
0
class EventPersonSchema(mm.SQLAlchemyAutoSchema):
    class Meta:
        model = EventPerson
        public_fields = ('id', 'identifier', '_title', 'email', 'affiliation',
                         'affiliation_link', 'affiliation_id',
                         'affiliation_meta', 'name', 'first_name', 'last_name',
                         'user_identifier')
        fields = public_fields + ('phone', 'address')

    type = fields.Constant('EventPerson')
    _title = EnumField(UserTitle, data_key='title')
    name = fields.String(attribute='full_name')
    user_identifier = fields.String(attribute='user.identifier')
    last_name = fields.String(required=True)
    email = fields.String(load_default='')
    affiliation_link = ModelField(Affiliation,
                                  data_key='affiliation_id',
                                  load_default=None,
                                  load_only=True)
    affiliation_id = fields.Integer(load_default=None, dump_only=True)
    affiliation_meta = fields.Nested(AffiliationSchema,
                                     attribute='affiliation_link',
                                     dump_only=True)

    @pre_load
    def handle_affiliation_link(self, data, **kwargs):
        # in some cases we get data that's already been loaded by PersonLinkSchema and thus no longer
        # has an affiliation_id but only an affiliation_link...
        data = data.copy()
        if affiliation_link := data.pop('affiliation_link', None):
            data['affiliation_id'] = affiliation_link.id
        return data
Ejemplo n.º 3
0
class PersonLinkSchema(mm.Schema):
    type = fields.String(dump_default='person_link')
    person_id = fields.Int()
    user_id = fields.Int(attribute='person.user_id', dump_only=True)
    user_identifier = fields.String(attribute='person.user.identifier',
                                    dump_only=True)
    name = fields.String(attribute='display_full_name', dump_only=True)
    first_name = fields.String(load_default='')
    last_name = fields.String(required=True)
    _title = EnumField(UserTitle, data_key='title')
    affiliation = fields.String(load_default='')
    affiliation_link = ModelField(Affiliation,
                                  data_key='affiliation_id',
                                  load_default=None,
                                  load_only=True)
    affiliation_id = fields.Integer(load_default=None, dump_only=True)
    affiliation_meta = fields.Nested(AffiliationSchema,
                                     attribute='affiliation_link',
                                     dump_only=True)
    phone = fields.String(load_default='')
    address = fields.String(load_default='')
    email = fields.String(load_default='')
    display_order = fields.Int(load_default=0, dump_default=0)
    avatar_url = fields.Function(lambda o: o.person.user.avatar_url
                                 if o.person.user else None,
                                 dump_only=True)
    roles = fields.List(fields.String(), load_only=True)

    @pre_load
    def load_nones(self, data, **kwargs):
        if not data.get('title'):
            data['title'] = UserTitle.none.name
        if not data.get('affiliation'):
            data['affiliation'] = ''
        if data.get('affiliation_id') == -1:
            # external search results with a predefined affiliation
            del data['affiliation_id']
        return data

    @post_load
    def ensure_affiliation_text(self, data, **kwargs):
        if data['affiliation_link']:
            data['affiliation'] = data['affiliation_link'].name
        return data

    @post_dump
    def dump_type(self, data, **kwargs):
        if data['person_id'] is None:
            del data['type']
            del data['person_id']
        if data['title'] == UserTitle.none.name:
            data['title'] = None
        return data
Ejemplo n.º 4
0
class RHMoveEvents(RHManageCategorySelectedEventsBase):
    @use_kwargs({
        'target_category':
        ModelField(Category,
                   filter_deleted=True,
                   required=True,
                   data_key='target_category_id'),
        'comment':
        fields.String(load_default=''),
    })
    def _process_args(self, target_category, comment):
        RHManageCategorySelectedEventsBase._process_args(self)
        self.target_category = target_category
        self.comment = comment

    def _check_access(self):
        RHManageCategorySelectedEventsBase._check_access(self)
        if (not self.target_category.can_create_events(session.user)
                and not self.target_category.can_propose_events(session.user)):
            raise Forbidden(_('You may not move events to this category.'))

    def _process(self):
        if self.target_category.can_create_events(session.user):
            for event in self.events:
                event.move(self.target_category)
            flash(
                ngettext(
                    'You have moved {count} event to the category "{cat}"',
                    'You have moved {count} events to the category "{cat}"',
                    len(self.events)).format(count=len(self.events),
                                             cat=self.target_category.title),
                'success')
        else:
            for event in self.events:
                create_event_request(event, self.target_category, self.comment)
            notify_move_request_creation(self.events, self.target_category,
                                         self.comment)
            flash(
                ngettext(
                    'You have requested the move of {count} event to the category "{cat}"',
                    'You have requested the move of {count} events to the category "{cat}"',
                    len(self.events)).format(count=len(self.events),
                                             cat=self.target_category.title),
                'success')
        return jsonify_data(flash=False)
Ejemplo n.º 5
0
    def __init__(self,
                 event,
                 contrib,
                 editable_type,
                 allow_claimed_files=False,
                 **kwargs):
        self.event = event
        self.contrib = contrib
        self.editing_file_types_query = EditingFileType.query.with_parent(
            event).filter_by(type=editable_type)

        keys_field = ModelField(
            EditingFileType, get_query=lambda m: self.editing_file_types_query)
        values_field = FilesField(required=True,
                                  allow_claimed=allow_claimed_files)
        validators = kwargs.pop('validate', []) + [self.validate_files]
        super().__init__(keys=keys_field,
                         values=values_field,
                         validate=validators,
                         **kwargs)
Ejemplo n.º 6
0
class RHMoveEvent(RHManageEventBase):
    """Move event to a different category."""
    @use_kwargs({
        'target_category':
        ModelField(Category,
                   filter_deleted=True,
                   required=True,
                   data_key='target_category_id'),
        'comment':
        fields.String(load_default=''),
    })
    def _process_args(self, target_category, comment):
        RHManageEventBase._process_args(self)
        self.target_category = target_category
        self.comment = comment

    def _check_access(self):
        RHManageEventBase._check_access(self)
        if (not self.target_category.can_create_events(session.user)
                and not self.target_category.can_propose_events(session.user)):
            raise Forbidden(_('You may not move events to this category.'))

    def _process(self):
        if self.target_category.can_create_events(session.user):
            self.event.move(self.target_category)
            flash(
                _('Event "{event}" has been moved to category "{category}"').
                format(event=self.event.title,
                       category=self.target_category.title), 'success')
        else:
            create_event_request(self.event, self.target_category,
                                 self.comment)
            notify_move_request_creation([self.event], self.target_category,
                                         self.comment)
            flash(
                _('Moving the event "{event}" to "{category}" has been requested and is pending approval'
                  ).format(event=self.event.title,
                           category=self.target_category.title), 'success')
        return jsonify_data(flash=False)