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)
class EntryResourceTest(ResourceTestCaseMixin, TestCase): def setUp(self): super(EntryResourceTest, self).setUp() self.album1 = Album(title='test_title_1', order_num=1) self.album1.save() self.album2 = Album(title='test_title_2', order_num=1) self.album2.save() self.foto1 = Foto(album=self.album1, title='foto1', image='test_image_1.jpg') self.foto2 = Foto(album=self.album1, title='foto2', image='test_image_2.jpg') self.video1 = Video(album=self.album2, title='video1', video_link='test_video_link2') self.video2 = Video(album=self.album2, title='video2', video_link='test_video_link2') def test_get_albums(self): resp = self.api_client.get('/api/v1/album/', format='json') self.assertValidJSONResponse(resp) self.assertEqual(len(self.deserialize(resp)['objects']), 2) self.assertEqual( self.deserialize(resp)['objects'][0], { u'id': self.album1.pk, u'title': u'{0}'.format(self.album1.title), u'image': None, u'order_num': self.album1.order_num, u'resource_uri': u'/api/v1/album/{0}/'.format(self.album1.pk) }) def test_get_foto_from_album_1(self): resp = self.api_client.get('/api/v1/foto/?album_id__id=' + str(self.album1.id), format='json') self.assertValidJSONResponse(resp) self.assertEqual(len(self.deserialize(resp)), 2) #self.assertEqual(self.deserialize(resp)['objects'][0], { # u'id': self.album1.pk, # u'title': u'{0}'.format(self.album1.title), # u'image': None, # u'order_num': self.album1.order_num, # u'resource_uri': u'/api/v1/album/{0}/'.format(self.album1.pk) #}) def test_get_VIDEO_from_album_1(self): resp = self.api_client.get('/api/v1/video/?album_id__id=' + str(self.album1.id), format='json') self.assertValidJSONResponse(resp) self.assertEqual(len(self.deserialize(resp)), 2)
def updateDBInfo(response, track): tags = Information() # Changing tags in the database if 'TITLE' in response and response['TITLE'] != '': tags.trackTitle = strip_tags(response['TITLE']).lstrip().rstrip() track.title = tags.trackTitle if 'ARTISTS' in response and response['ARTISTS'] != '': tags.trackArtist = strip_tags( response['ARTISTS']).lstrip().rstrip().split(',') artists = [] for artist in tags.trackArtist: if Artist.objects.filter(name=artist).count() == 0: newArtist = Artist() newArtist.name = artist newArtist.save() artists.append(Artist.objects.get(name=artist)) track.artist.clear() for artist in artists: track.artist.add(artist) if 'PERFORMER' in response and response['PERFORMER'] != '': tags.trackPerformer = strip_tags( response['PERFORMER']).lstrip().rstrip() track.performer = tags.trackPerformer if 'COMPOSER' in response and response['COMPOSER'] != '': tags.trackComposer = strip_tags(response['COMPOSER']).lstrip().rstrip() track.composer = tags.trackComposer if 'YEAR' in response and response['YEAR'] != '': tags.trackYear = checkIntValueError(response['YEAR']) track.year = tags.trackYear if 'TRACK_NUMBER' in response and response['TRACK_NUMBER'] != '': tags.trackNumber = checkIntValueError(response['TRACK_NUMBER']) track.number = tags.trackNumber if 'BPM' in response and response['BPM'] != '': track.bpm = checkIntValueError(response['BPM']) if 'LYRICS' in response and response['LYRICS'] != '': tags.lyrics = strip_tags(response['LYRICS']).lstrip().rstrip() track.lyrics = tags.lyrics if 'COMMENT' in response and response['COMMENT'] != '': tags.comment = strip_tags(response['COMMENT']).lstrip().rstrip() track.comment = tags.comment if 'GENRE' in response and response['GENRE'] != '': tags.trackGenre = strip_tags(response['GENRE']).lstrip().rstrip() if Genre.objects.filter(name=tags.trackGenre).count() == 0: genre = Genre() genre.name = tags.trackGenre genre.save() genre = Genre.objects.get(name=tags.trackGenre) track.genre = genre if 'COVER' in response: md5Name = hashlib.md5() if str(response['COVER'].split(",")[0]) == "image/png": extension = "png" else: extension = "jpg" md5Name.update(base64.b64decode(str(response['COVER'].split(",")[1]))) # Check if the folder exists filePath = "/ManaZeak/static/img/covers/" if not os.path.isdir(filePath): os.mkdir(filePath) # Create the folder filePath += +md5Name.hexdigest() + extension # if the filePath is the same, then the md5 hash of the image is # the same, therefore the images are the same, therefore do nothing if not os.path.isfile(filePath): with open(filePath, 'wb+') as destination: # Split the header with MIME type tags.cover = base64.b64decode( str(response['COVER'].split(",")[1])) destination.write(tags.cover) track.coverLocation = md5Name.hexdigest() + extension if 'ALBUM_TITLE' in response and 'ALBUM_ARTISTS' in response and response['ALBUM_TITLE'] != '' \ and response['ALBUM_ARTISTS'] != '': tags.albumTitle = strip_tags(response['ALBUM_TITLE']).lstrip().rstrip() tags.albumArtist = strip_tags( response['ALBUM_ARTISTS']).lstrip().rstrip().split(',') if Album.objects.filter(title=tags.albumTitle).count() == 0: album = Album() album.title = tags.albumTitle album.save() album = Album.objects.get(title=tags.albumTitle) album.artist.clear() for artist in tags.albumArtist: if Artist.objects.filter(name=artist).count() == 0: newArtist = Artist() newArtist.name = artist newArtist.save() album.artist.add(Artist.objects.get(name=artist)) if 'ALBUM_TOTAL_DISC' in response and response[ 'ALBUM_TOTAL_DISC'] != '': tags.albumTotalDisc = checkIntValueError( response['ALBUM_TOTAL_DISC']) album.totalDisc = tags.albumTotalDisc if 'DISC_NUMBER' in response and response['DISC_NUMBER'] != '': tags.albumDiscNumber = checkIntValueError(response['DISC_NUMBER']) track.discNumber = tags.albumDiscNumber if 'ALBUM_TOTAL_TRACK' in response and response[ 'ALBUM_TOTAL_TRACK'] != '': tags.albumTotalTrack = checkIntValueError( response['ALBUM_TOTAL_TRACK']) album.totalTrack = tags.albumTotalTrack album.save() track.album = album track.save() return tags