示例#1
0
    def patch(self):
        dataclient = current_user()
        schema = DataClientSchema(partial=True)
        data, errors = schema.load(request.json)
        for err in errors:
            if err in data:
                del data[err]

        if not data:
            return {
                'message': 'DataClient not updated. Some errors occurred',
                'errors': errors
            }, 400

        for k, v in data.items():
            if k == 'roles':
                if not current_has_minimum_role(Role.Admin):
                    errors[k] = [
                        'You are note allowed to change user\'s '
                        'roles'
                    ]
                    continue
                dataclient.update_roles(data['roles'])
            setattr(dataclient, k, v)
        db.session.commit()

        response = {
            'message': 'DataClient updated.',
            'data_client': schema.dump(dataclient).data
        }
        if errors:
            response['message'] += ' Some errors occurred.'
            response['errors'] = errors
        return response
示例#2
0
    def post(self):
        schema = DataClientSchema()
        data, errors = schema.load(request.json)
        for err in errors:
            if err in data:
                del data[err]
        if not data:
            return {
                'message': 'DataClient not created. Some errors occurred.',
                'errors': errors
            }, 400

        try:
            dataclient = auth.dataclient.register(data['email'],
                                                  data['password'])
        except UserExistsException:
            return {
                'message': 'DataClient not created. Some errors occurred.',
                'errors': {
                    'email':
                    ['An user with that email address already exists.']
                }
            }, 409

        del data['password']
        for k, v in data.items():
            setattr(dataclient, k, v)

        db.session.add(dataclient)
        db.session.commit()
        return schema.dump(dataclient).data, 201
示例#3
0
    def get(self, dataclient_id=None):
        schema = DataClientSchema()

        if dataclient_id != current_user().id:
            if not current_has_minimum_role(Role.Admin):
                abort(403,
                      message='You are not allowed to access other user\'s'
                      ' data.')
        dataclient = DataClient.query.get_or_404(dataclient_id)
        return schema.dump(dataclient).data
示例#4
0
 def delete(self):
     dataclient_data = DataClientSchema().dump(current_user()).data
     db.session.delete(current_user())
     db.session.commit()
     auth.session.logout()
     return {
         'message': 'DataClient removed',
         'data_client': dataclient_data
     }
示例#5
0
 def delete(self, dataclient_id=None):
     if dataclient_id != current_user().id:
         if not current_has_minimum_role(Role.Admin):
             abort(403,
                   message='You are not allowed to remove other user\'s'
                   ' accounts.')
     dataclient = DataClient.query.get_or_404(dataclient_id)
     dataclient_data = DataClientSchema().dump(dataclient).data
     db.session.delete(dataclient)
     db.session.commit()
     return {
         'message': 'DataClient removed',
         'data_client': dataclient_data
     }
示例#6
0
class QuestionnaireSchema(SurveyBaseSchema):
    name = fields.String(required=True)
    description = fields.String(required=True)
    dimensions = fields.Nested(DimensionSchema, many=True, dump_only=True)

    shadow = fields.Method('get_is_shadow', dump_only=True)
    owners = fields.Nested(DataClientSchema(only=("id", "href")),
                           many=True,
                           dump_only=True)

    published = fields.Boolean()
    accepts_submissions = fields.Boolean()
    scheduled = fields.Boolean()
    begins = fields.DateTime()
    ends = fields.DateTime()

    password_enabled = fields.Boolean()
    email_whitelist_enabled = fields.Boolean()
    email_blacklist_enabled = fields.Boolean()
    password = fields.String()
    email_whitelist = fields.List(fields.Email())
    email_blacklist = fields.List(fields.Email())

    allow_embedded = fields.Boolean()
    allow_standalone = fields.Boolean()
    lti_consumer_key = fields.String(dump_only=True)

    xapi_target = fields.String(allow_none=True)

    __private__ = [
        *SurveyBaseSchema.get_private(), *[
            'published', 'email_whitelist', 'email_blacklist', 'password',
            'email_whitelist_enabled', 'email_blacklist_enabled',
            'xapi_target', 'allow_embedded', 'lti_consumer_key'
        ]
    ]

    def get_is_shadow(self, obj):
        return isinstance(obj, ShadowQuestionnaire)
示例#7
0
class SurveyBaseSchema(RESTFulSchema):
    original_language = enum_field(BabelLanguage, dump_only=True)

    reference_id = fields.String()
    template = fields.Boolean()
    current_language = fields.Method('get_current_language', dump_only=True)
    available_languages = fields.List(enum_field(BabelLanguage),
                                      dump_only=True)
    shadow = fields.Boolean(dump_only=True)
    reference_to = fields.Method('build_reference_to', dump_only=True)
    owners = fields.Nested(DataClientSchema(only=("id", "href")),
                           many=True,
                           dump_only=True)
    owned_incoming_references = fields.Method(
        'build_owned_incoming_references', dump_only=True)
    incoming_reference_count = fields.Method('build_incoming_reference_count',
                                             dump_only=True)

    __private__ = [
        'shadow', 'incoming_reference_count', 'owned_incoming_references',
        'reference_to', 'owners'
    ]

    def get_current_language(self, obj):
        current_language = g._language
        if current_language not in obj.available_languages:
            current_language = obj.original_language
        return {
            'item_id': current_language.name,
            'value': current_language.value
        }

    def build_reference_to(self, obj: SurveyBase):
        if not obj.shadow:
            return missing

        return {
            "href": super(SurveyBaseSchema, self).build_href(obj.concrete),
            "id": obj.concrete.id
        }

    def build_owned_incoming_references(self, obj: SurveyBase):
        return list(
            map(
                lambda q: {
                    "href": super(SurveyBaseSchema, self).build_href(q),
                    "id": q.id
                }, filter(lambda q: q.accessible_by(current_user()),
                          obj.copies)))

    def build_incoming_reference_count(self, obj: SurveyBase):
        return len(obj.copies)

    @classmethod
    def get_private(cls) -> List[str]:
        if hasattr(cls, '__private__'):
            return cls.__private__
        return []

    @post_dump(pass_original=True, pass_many=False)
    def strip_private_fields(self, data, original_data: SurveyBase = None):
        def _strip(d):
            for k in self.get_private():
                if k in d:
                    del d[k]
            return d

        if isinstance(original_data, list):
            for od in original_data:
                if od.id == data['id']:
                    original_data = od
                    break

        if not original_data.modifiable_by(current_user()):
            return _strip(data)
示例#8
0
 def get(self):
     return DataClientSchema().dump(current_user()).data