Exemplo n.º 1
0
    def post(self):
        self.resource_fields['data'] = fields.Nested(self.album_fields)
        args = self.parser.parse_args()
        name = args.get('name', '')
        artist = args.get('artist', '')
        is_hot = args.get('is_hot', 0)
        classification = args.get('classification', [])

        cl_obj_list = Classification.select().where(
            Classification.id << classification)
        album = Album(name=name, artist=artist, is_hot=is_hot)
        album.save()

        cl_json_list = []
        if cl_obj_list.exists():
            for item in cl_obj_list:
                album.classification.add(item)
                cl_json_list.append(model_to_dict(item))
            album.save()
        album = model_to_dict(album)
        album['classification'] = cl_json_list
        resp = {'error_code': 0, 'data': album}

        return make_response(json.dumps(marshal(resp, self.resource_fields)),
                             200)
Exemplo n.º 2
0
    def put(self, album_id):
        album = Album.select().where(Album.id == album_id).first()
        if album:
            status_code = 200
            args = self.parser.parse_args()
            classification = args.get('classification', [])
            cl_json_list = []
            """ Return album data without update if error classification data"""
            if type(classification) == type([]) and len(classification) != 0:
                cl_obj_list = Classification.select().where(
                    Classification.id << classification)
                for item in cl_obj_list:
                    if item in album.classification:
                        continue
                    album.classification.add(item)
                    album.save()

            for item in album.classification:
                cl_json_list.append(model_to_dict(item))
            album = model_to_dict(album)
            album['classification'] = cl_json_list
            resp = {'error_code': 0, 'data': album}

        else:
            status_code = 400
            self.resource_fields['message'] = fields.String(
                attribute='message')
            resp = {'error_code': 40021, 'message': 'Album dose not exist.'}
        return make_response(json.dumps(marshal(resp, self.resource_fields)),
                             status_code)
Exemplo n.º 3
0
 def get(self):
     resource_fields = {
         'error_code': fields.Integer(attribute='error_code'),
         'data': fields.List(fields.Nested(self.classification_fields))
     }
     cl_list = Classification.select()
     data = [model_to_dict(cl) for cl in cl_list]
     resp = {'error_code': 0, 'data': data}
     return make_response(json.dumps(marshal(resp, resource_fields)), 200)
Exemplo n.º 4
0
    def post(self):
        resource_fields = {
            'error_code': fields.Integer(attribute='error_code'),
            'message': fields.String(attribute='message'),
            'data': fields.Nested(self.classification_fields)
        }
        args = self.parser.parse_args()
        name = args.get('name')
        parent_id = args.get('parent', None)
        if not (parent_id and Classification.select().where(
                Classification.id == parent_id).exists()):
            self.classification_fields.pop('parent')
        try:
            cl = Classification.create(name=name, parent=parent_id)
            status_code = 201
            resp = {'error_code': 0, 'data': model_to_dict(cl)}
            resource_fields.pop('message')
        except IntegrityError:
            resp = {'error_code': 40020, 'message': 'Duplicate name.'}
            resource_fields.pop('data')
            status_code = 400

        return make_response(json.dumps(marshal(resp, resource_fields)),
                             status_code)