Ejemplo n.º 1
0
class RegistrationApiAttrs(UserUpdateApiAttrs):
    email = fields.Email(required=True, default=None)
    password1 = fields.Str(required=True)
    password2 = fields.Str(required=True)

    class Meta:
        strict = True
        ordered = True
        object_type = 'user'

    @validates_schema
    def validate_data(self, data, **kwargs):
        if 'password1' in data:
            try:
                validate_password(data['password1'])
            except DjangoValidationError as e:
                raise ValidationError(e.error_list[0].message,
                                      field_name='password1',
                                      code=e.error_list[0].code,
                                      field_names=[
                                          'password1',
                                      ])
            if 'password2' in data and data['password1'] != data['password2']:
                raise ValidationError(_('Passwords not match'),
                                      field_name='password1',
                                      field_names=['password1', 'password2'])

    @post_load
    def prepare_data(self, data, **kwargs):
        data = super().prepare_data(data)
        data['password'] = data['password1']
        data.pop('password1')
        data.pop('password2')
        data.pop('subscriptions_report_opt_in', None)
        return data
Ejemplo n.º 2
0
class ResendActivationEmailApiAttrs(ObjectAttrs):
    email = fields.Email(required=True, default=None)

    class Meta:
        strict = True
        ordered = True
        object_type = 'user'
Ejemplo n.º 3
0
class CreateSubmissionAttrs(ObjectAttrs):
    applicant_email = core_fields.Email(required=False, default=None)
    author = core_fields.Str(required=False, default=None)
    title = core_fields.Str(required=True,
                            faker_type='application title',
                            example='Some App')
    url = core_fields.Url(required=True)
    notes = core_fields.Str(required=True)
    image = core_fields.Base64String(required=False,
                                     default=None,
                                     max_size=settings.IMAGE_UPLOAD_MAX_SIZE)
    datasets = core_fields.List(core_fields.Int(), required=False, default=[])
    external_datasets = core_fields.Nested(ExternalResourceSchema,
                                           required=False,
                                           default={},
                                           many=True)
    keywords = core_fields.List(core_fields.Str(), default='', required=False)
    comment = core_fields.String(required=False,
                                 description='Comment body',
                                 example='Looks unpretty',
                                 default='')

    class Meta:
        strict = True
        ordered = True
        object_type = 'application-submission'
class UserSchemaMixin(object):
    state = fields.Str(
        required=True,
        faker_type='userstate',
        example='active',
        description="Allowed values: 'active', 'inactive' or 'blocked'")
    email = fields.Email(required=True,
                         faker_type='email',
                         example='*****@*****.**')
    fullname = fields.Str(missing=None,
                          faker_type='name',
                          example='Jan Kowalski')
    about = fields.Str(missing=None,
                       faker_type='sentence',
                       example='I am a very talented programmer.')
    created = fields.Date()
    subscriptions_report_opt_in = fields.Boolean()
    rodo_privacy_policy_opt_in = fields.Boolean()
    count_datasets_created = fields.Int()
    count_datasets_modified = fields.Int()

    @post_dump
    def prepare_data(self, data, **kwargs):
        data['subscriptions_report_opt_in'] = True if data.get(
            'subscriptions_report_opt_in') is not None else False
        data['rodo_privacy_policy_opt_in'] = True if data.get(
            'rodo_privacy_policy_opt_in') is not None else False
        return data
Ejemplo n.º 5
0
class LoginApiAttrs(ObjectAttrs):
    email = fields.Email(required=True, default=None)
    password = fields.Str(required=True, default=None)

    class Meta:
        strict = True
        ordered = True
        object_type = 'user'
Ejemplo n.º 6
0
class SubmissionAttrs(ObjectAttrs):
    title = fields.Str()
    url = fields.URL()
    applicant_email = fields.Email()

    class Meta:
        object_type = 'application-submission'
        path = 'submissions'
        url_template = '{api_url}/applications/submissions/{ident}'
Ejemplo n.º 7
0
class ShowcaseProposalAttrs(ObjectAttrs):
    title = fields.Str()
    url = fields.URL()
    applicant_email = fields.Email()

    class Meta:
        object_type = 'showcaseproposal'
        path = 'showcases'
        url_template = '{api_url}/showcases/suggest'
class InstitutionCSVMetadataSerializer(ExtSchema):
    organization_url = fields.Url(attribute='frontend_absolute_url',
                                  data_key=_('Organization URL'))
    organization_type = fields.Function(
        lambda obj: obj.get_institution_type_display(),
        data_key=_('Institution type'))
    organization_title = TranslatedStr(attribute='title', data_key=_('Name'))
    organization_abbr_title = TranslatedStr(attribute='abbreviation',
                                            data_key=_('Abbreviation'),
                                            default='')
    organization_regon = fields.Str(data_key=_('REGON'), attribute='regon')
    organization_epuap = fields.Str(attribute='epuap',
                                    data_key=_('EPUAP'),
                                    default='')
    organization_website = fields.Url(attribute='website',
                                      data_key=_('Website'))
    organization_created = fields.DateTime(attribute='created',
                                           data_key=_('Organization created'),
                                           format='iso8601')
    organization_modified = fields.DateTime(
        attribute='modified',
        data_key=_('Organization modified'),
        format='iso8601')
    organization_datasets_count = fields.Method(
        'get_published_datasets_count', data_key=_('Number of datasets'))
    organization_resources_count = fields.Method(
        'get_published_resources_count',
        data_key=_('Number of organization resources'))
    organization_postal_code = fields.Str(attribute='postal_code',
                                          data_key=_('Postal code'))
    organization_city = fields.Str(attribute='city', data_key=_('City'))
    organization_street_type = fields.Str(attribute='street_type',
                                          data_key=_('Street type'))
    organization_street = fields.Str(attribute='street', data_key=_('Street'))
    organization_street_number = fields.Str(attribute='street_number',
                                            data_key=_('Street number'))
    organization_flat_number = fields.Str(attribute='flat_number',
                                          data_key=_('Flat number'))
    organization_email = fields.Email(attribute='email', data_key=_('Email'))
    organization_phone_number = fields.Str(attribute='tel',
                                           data_key=_('Phone'))

    class Meta:
        ordered = True

    def get_published_datasets_count(self, organization):
        return self.context['published_datasets_count']

    def get_published_resources_count(self, organization):
        return self.context['published_resources_count']
Ejemplo n.º 9
0
class SubscriptionAttrs(ObjectAttrs):
    email = core_fields.Email(required=True)
    newsletter_subscription_info = core_fields.String(
        description='Human readable subscription status',
        example='Your subscription is active',
        required=False)
    is_active = core_fields.Boolean(
        description='Is newsletter subscription active?',
        example=True,
        required=False)

    class Meta:
        object_type = 'subscription'
        strict = True
        ordered = True
class CommentApiAttrs(ObjectAttrs):
    text = fields.Str()
    created = fields.DateTime()
    author = fields.Email()
    modified = fields.DateTime()

    class Meta:
        relationships_schema = CommentApiRelationships
        object_type = 'comment'
        ordered = True
        model = 'schedules.Comment'

    @staticmethod
    def self_api_url(data):
        return None
Ejemplo n.º 11
0
class CreateSubmissionAttrs(ObjectAttrs):
    applicant_email = core_fields.Email(required=False, default=None)
    author = core_fields.Str(required=False, default=None)
    title = core_fields.Str(required=True,
                            faker_type='application title',
                            example='Some App')
    url = core_fields.Url(required=True)
    notes = core_fields.Str(required=True)
    image = core_fields.Base64String(required=False,
                                     default=None,
                                     max_size=settings.IMAGE_UPLOAD_MAX_SIZE)
    illustrative_graphics = core_fields.Base64String(
        required=False, default=None, max_size=settings.IMAGE_UPLOAD_MAX_SIZE)
    image_alt = core_fields.Str(required=False, default=None)
    datasets = core_fields.List(core_fields.Str(), required=False, default=[])
    external_datasets = core_fields.Nested(ExternalResourceSchema,
                                           required=False,
                                           default={},
                                           many=True)
    keywords = core_fields.List(core_fields.Str(), default='', required=False)
    comment = core_fields.String(required=False,
                                 description='Comment body',
                                 example='Looks unpretty',
                                 default='')
    is_personal_data_processing_accepted = core_fields.Boolean(required=True)
    is_terms_of_service_accepted = core_fields.Boolean(required=True)

    class Meta:
        strict = True
        ordered = True
        object_type = 'application-submission'

    @pre_load
    def prepare_data(self, data, **kwargs):
        data['datasets'] = [
            x.replace('dataset-', '') for x in data.get('datasets', []) if x
        ]
        return data

    @validates('is_personal_data_processing_accepted')
    def validate_is_personal_data_processing_accepted(self, value):
        if not value:
            raise ValidationError(_('This field is required'))

    @validates('is_terms_of_service_accepted')
    def validate_is_terms_of_service_accepted(self, value):
        if not value:
            raise ValidationError(_('This field is required'))
class UserScheduleApiAttrs(ObjectAttrs):
    email = fields.Email(attribute='user.email')
    institution = fields.Str()
    items_count = fields.Int()
    is_ready = fields.Bool()
    is_blocked = fields.Bool()
    recommended_items_count = fields.Int()
    implemented_items_count = fields.Int()
    state = fields.Str()

    class Meta:
        relationships_schema = UserScheduleApiRelationships
        object_type = 'user_schedule'
        url_template = '{api_url}/auth/user_schedules/{ident}'
        ordered = True
        model = 'schedules.UserSchedule'
class UserCSVSerializer(CSVSerializer):
    id = api_fields.Int(data_key=_('id'), required=True, example=77)
    email = api_fields.Email(data_key=_('Email'),
                             default='',
                             required=True,
                             example='*****@*****.**')
    fullname = api_fields.Str(data_key=_('Full name'),
                              default='',
                              example='Jan Kowalski')
    official_phone = api_fields.Method('get_phone',
                                       data_key=_('Official phone'),
                                       example='+481234567890')
    role = api_fields.Method('get_role',
                             data_key=_('Role'),
                             default='',
                             example='+481234567890')
    state = api_fields.Str(
        data_key=_('State'),
        required=True,
        example='active',
        description="Allowed values: 'active', 'inactive' or 'blocked'")
    institution = api_fields.Method('get_institutions',
                                    data_key=_('Institution'),
                                    example='Ministerstwo Cyfryzacji')

    def get_phone(self, obj):
        if obj.phone:
            phone = obj.phone
            if obj.phone_internal:
                phone += f'.{obj.phone_internal}'
            return phone
        return ''

    def get_role(self, obj):
        if obj.is_superuser:
            return _('Admin')
        elif obj.is_staff:
            return _('Editor')
        else:
            return _('User')

    def get_institutions(self, obj):
        return ','.join(str(org.id) for org in obj.organizations.all())

    class Meta:
        ordered = True
        model = 'users.User'
Ejemplo n.º 14
0
class ShowcaseProposalCSVSerializer(CSVSerializer):
    id = fields.Int(data_key='id', required=True, example=77)
    category_name = fields.Str(data_key=_('Category'), example='Aplikacja')
    title = fields.Str(data_key=_('Name'), example='Propozycja aplikacji')
    notes = fields.Str(data_key=_('Notes'), default='', example='opis...')
    url = fields.Str(data_key=_('App URL'),
                     default='',
                     example='http://example.com')
    author = fields.Str(data_key=_('Author'),
                        default='',
                        example='Jan Kowalski')
    applicant_email = fields.Email(data_key=_('applicant email'),
                                   default='',
                                   required=False,
                                   example='*****@*****.**')
    keywords = fields.Str(data_key=_('keywords'),
                          attribute='keywords_as_str',
                          default='',
                          example='tag1,tag2,tag3')
    report_date = fields.Date(data_key=_('report date'))
    decision_date = fields.Date(data_key=_('decision date'), default=None)
    comment = fields.Str(data_key=_('comment'),
                         example='komentarz...',
                         default='')
    datasets = fields.Method('get_datasets',
                             data_key=_('datasets'),
                             example='998,999',
                             default='')
    external_datasets = fields.Raw(data_key=_('external datasets'),
                                   example='[]')
    showcase = fields.Int(data_key=_('Showcase'),
                          attribute='showcase.id',
                          default=None)

    class Meta:
        ordered = True
        model = 'showcases.ShowcaseProposal'

    def get_datasets(self, obj):
        return ','.join(str(x.id) for x in obj.datasets.order_by('id'))
Ejemplo n.º 15
0
class SubscribeApiRequest(TopLevel):

    email = core_fields.Email(required=True,
                              error_messages={
                                  'invalid':
                                  _('E-mail address you entered is not valid')
                              })
    personal_data_processing = core_fields.Boolean(required=True)
    personal_data_use = core_fields.Boolean(required=True)

    @validates('personal_data_processing')
    def validate_personal_data_processing(self, value):
        if not value:
            raise ValidationError(_('This field is required'))

    @validates('personal_data_use')
    def validate_personal_data_use(self, value):
        if not value:
            raise ValidationError(_('This field is required'))

    class Meta:
        attrs_schema = SubscriptionAttrs
class UserScheduleItemApiAttrs(ObjectAttrs):
    email = fields.Email()
    institution = fields.Str(attribute='organization_name')
    institution_unit = fields.Str(attribute='organization_unit')
    dataset_title = fields.Str()
    created = fields.Date()
    format = fields.Str()
    is_new = fields.Bool()
    is_openness_score_increased = fields.Bool()
    is_quality_improved = fields.Bool()
    description = fields.Str()
    state = fields.Str()
    recommendation_state = fields.Str()
    recommendation_notes = fields.Str()
    is_recommendation_issued = fields.Bool()

    is_accepted = fields.Bool()
    is_completed = fields.Bool()
    is_resource_added = fields.Bool()
    is_resource_added_notes = fields.Str()
    resource_link = fields.Url()

    class Meta:
        relationships_schema = UserScheduleItemApiRelationships
        object_type = 'user_schedule_item'
        url_template = '{api_url}/auth/user_schedule_items/{ident}'
        ordered = True
        model = 'schedules.UserScheduleItem'

    @post_dump
    def prepare_data(self, data, **kwargs):
        request = self.context.get('request')
        user = getattr(request, 'user', None) if request else None
        if not user or not user.is_superuser:
            del data['recommendation_state']
            del data['recommendation_notes']
        return data
class UserCSVSerializer(CSVSerializer):
    id = fields.Int(data_key=_('id'), required=True, example=77)
    email = fields.Email(data_key=_('Email'),
                         default='',
                         required=True,
                         example='*****@*****.**')
    fullname = fields.Str(data_key=_('Full name'),
                          default='',
                          example='Jan Kowalski')
    official_phone = fields.Method('get_phone',
                                   data_key=_('Official phone'),
                                   example='+481234567890')
    role = fields.Method('get_role',
                         data_key=_('Role'),
                         default='',
                         example='+481234567890')
    state = fields.Str(
        data_key=_('State'),
        required=True,
        example='active',
        description="Allowed values: 'active', 'inactive' or 'blocked'")
    institution = fields.Str(attribute='institutions_ids_list_as_str',
                             data_key=_('Institution'),
                             example='1,2')
    institution1 = fields.Str(attribute='institutions_ids_list_as_str',
                              data_key=_('Institution1'),
                              example='1,2')
    institution2 = fields.Int(attribute='agent_organization_id',
                              data_key=_('Institution2'),
                              example=1)
    is_academy_admin = fields.Method('get_is_academy_admin',
                                     data_key=_('Admin AOD'),
                                     example='Nie')
    is_agent = fields.Method('get_is_agent',
                             data_key=_('Agent'),
                             example='Nie')
    extra_agent_of = fields.Method('get_extra_agent_of',
                                   data_key=_('Extra agent'),
                                   example=1)
    is_labs_admin = fields.Method('get_is_labs_admin',
                                  data_key=_('Admin LOD'),
                                  example='Nie')
    is_official = fields.Method('get_is_official',
                                data_key=_('Official'),
                                example='Nie')
    is_staff = fields.Method('get_is_staff',
                             data_key=_('Editor'),
                             example='Nie')
    is_superuser = fields.Method('get_is_superuser',
                                 data_key=_('Admin'),
                                 example='Nie')
    last_login = fields.DateTime(data_key=_('Last login date'),
                                 example='2021-01-01T00:00:00Z',
                                 default=None)

    @staticmethod
    def get_yes():
        return _('Yes')

    @staticmethod
    def get_no():
        return _('No')

    def get_is_academy_admin(self, obj):
        return self.get_yes() if obj.is_academy_admin else self.get_no()

    def get_is_agent(self, obj):
        return self.get_yes() if obj.is_agent else self.get_no()

    def get_extra_agent_of(self, obj):
        return obj.extra_agent_of.id if obj.extra_agent_of else None

    def get_is_labs_admin(self, obj):
        return self.get_yes() if obj.is_labs_admin else self.get_no()

    def get_is_superuser(self, obj):
        return self.get_yes() if obj.is_superuser else self.get_no()

    def get_is_official(self, obj):
        return self.get_yes() if obj.is_official else self.get_no()

    def get_is_staff(self, obj):
        return self.get_yes() if obj.is_staff else self.get_no()

    def get_phone(self, obj):
        if obj.phone:
            phone = obj.phone
            if obj.phone_internal:
                phone += f'.{obj.phone_internal}'
            return phone
        return ''

    def get_role(self, obj):
        if obj.is_superuser:
            return _('Admin')
        elif obj.is_staff:
            return _('Editor')
        else:
            return _('User')

    class Meta:
        ordered = True
        model = 'users.User'
        fields = ('id', 'email', 'fullname', 'official_phone', 'is_staff',
                  'is_official', 'is_superuser', 'is_academy_admin',
                  'is_labs_admin', 'is_agent', 'extra_agent_of', 'state',
                  'institution1', 'institution2', 'last_login')