def upload_picture(request): #if request.user.is_authenticated: if request.method == 'POST' and request.FILES['pic_1']: pic_file = request.FILES['pic_1'] user_id = request.user name = request.POST['name'] tags = request.POST['tags'] category = request.POST['category'] #cat = AlbumCategory.objects.get(id=category) cat = get_object_or_404(AlbumCategory, pk=category) create_album = Album(uid=user_id, name=name, tags=tags, category=cat) create_album.save() # jumlah pic sum_pic = request.POST['sum_pic'] for i in range(int(sum_pic)): pic_num = i + 1 pic_file = 'pic_' + str(pic_num) pic_cap = 'pic_cap_' + str(pic_num) if request.FILES.get(pic_file, False): pic_file = request.FILES[pic_file] pic_cap = request.POST[pic_cap] create_pic = Photo(aid=create_album, caption=pic_cap, image=pic_file) create_pic.save() # Proses Tagging list_tags = tags.split(',') list_tags = [i.strip().lower() for i in list_tags] for tg in list_tags: if AlbumTag.objects.filter(tag=tg).exists(): current_tag = AlbumTag.objects.filter(tag=tg).first() current_tag.albums.add(create_album) else: create_tag = AlbumTag(uid=user_id, tag=tg) create_tag.save() create_tag.albums.add(create_album) ac_list = AlbumCategory.objects.all() context = {'ac_list': ac_list} #return render(request, 'upload-picture.html', context) else: ac_list = AlbumCategory.objects.all() context = {'ac_list': ac_list} #return render(request, 'upload-picture.html', context) return render(request, 'upload-picture.html', context)
def save(self, commit=True): user = super(TingUserCreateForm, self).save(commit=False) user.set_password(self.cleaned_data["password"]) user.set_registration_complete() if commit: user.save() album = Album(owner=user, name="MicroBlog", is_public=False,\ description="Used as default album for microblog images") album.save() return user
def add_album(user, album_name, can_edit, force): try: if not get_album(user, album_name): new_album = Album() new_album.set_album(user, album_name, can_edit) new_album.save(force_insert=force) return True return False except Exception: raise Exception("albums.add_album : Can't add album")
def new(request): if request.method == 'POST': form = NewAlbumForm(request.POST, request.FILES) if form.is_valid(): #Create a new album with the title and photo from the submitted form newAlbum = Album(title = request.POST['title'], cover_picture = request.FILES['cover']) newAlbum.save() #We also need to create a new entry in Gallery to associate the user with the new album userOb = request.user newGallery = Gallery(usr = userOb, albums = newAlbum) newGallery.save() else: print('form not valid') return HttpResponseRedirect(reverse('index', args=(), kwargs={}))
def create(self, request): name = request.data['name'] itunesUrl = "https://itunes.apple.com/search?term={artist}&media=music&entity=album".format(artist=urllib2.quote(name)) result = urllib2.urlopen(itunesUrl).read() albums = json.loads(result) if albums["resultCount"] == 0: return Response({'success': False}, status=status.HTTP_200_OK) else: artist = Artist(name=name) artist.save() for album in albums["results"]: album = Album(name=album["collectionName"], imageUrl=album["artworkUrl100"], releaseDate=album["releaseDate"], artist=artist) album.save() return Response({'success': True, 'artist': name, 'results': albums['resultCount']}, status=status.HTTP_201_CREATED)
def CreateDb(albums): from albums.models import Album, AlbumsSong from song.models import Song for data in albums: album = Album(name=data['name'], artist=data['artist'], image_url=data['image'], public=True) album.save() for dataSong in data['songs']: song = Song(title=dataSong, performer=data['artist'], year=data['published'].split(' ')[2][0:4], genre=data['genre'].capitalize()) song.save() albumSong = AlbumsSong(album=album, song=song) albumSong.save()
def migrate_albums(request): albums = AlbumMigration.objects.filter(migrated=False) new_albums = [] for album in albums: django_user = album.owner.django_user if django_user: new_album = Album( title = album.title, description = album.description, user = django_user ) try: new_album.full_clean() new_album.save() new_albums.append(new_album) album.migrated = True album.new_album = new_album album.save() except ValidationError: pass return render(request, 'migration/albums.html', {'new_albums': new_albums})
def create(self, validated_data): album = Album(name=validated_data.get('name'), public=validated_data.get('public'), artist=validated_data.get('artist'), image_url=validated_data.get('image_url')) album.save() for owner in validated_data.get('owners'): album.owners.add(owner.id) album.save() return album
def current_album(): url = 'https://1001albumsgenerator.com/mulloy' page = requests.get(url) soup = BeautifulSoup(page.content, 'html.parser') title = get_album_title(soup) if len(Album.objects.all()) is 0 or title != Album.objects.order_by( '-date_listened_to')[0].title: artist = get_artist_name(soup) link = get_spotify_link(soup) now = timezone.now().date() current_album = Album(title=title, artist_name=artist, spotify_link=link, date_listened_to=now) return current_album
import sys import os import django APPS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, APPS_DIR) sys.path.insert(0, os.path.join(APPS_DIR, 'quanbenxiaoshuo')) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production") django.setup() from albums.models import Category, Album from db_tools.data.album_data import row_data for lev1_cat in row_data: lev1_intance = Album() lev1_intance.name = lev1_cat["name"] lev1_intance.title = lev1_cat["title"] lev1_intance.keywords = lev1_cat["keywords"] lev1_intance.description = lev1_cat["description"] lev1_intance.info = lev1_cat["info"] lev1_intance.is_tab = lev1_cat["is_tab"] lev1_intance.category = Category.objects.filter( name=lev1_cat["category"])[0] lev1_intance.save()
def getTrackById(request, id): try: Track.objects.get(track_id=id) tracks = Track.objects.filter(track_id=id) data = { "results": list(tracks.values("name", "track_id", "album__album_id")) } return JsonResponse(data) except ObjectDoesNotExist: response = requests.get('https://api.deezer.com/track/%s' % id) tracks_data = response.json() if tracks_data.get('error'): return HttpResponse('No track according to URL param ID') else: album_artist = Artist.objects.filter( artist_id=tracks_data['artist']['id']) track_album = Album.objects.filter( album_id=tracks_data['album']['id']) if album_artist and track_album: track = Track(name=tracks_data['title'], track_id=tracks_data['data']['id'], album=track_album.get()) track.save() return redirect('getTrackByName', name=tracks_data['data']['title']) elif album_artist and not track_album: album_response = requests.get( 'https://api.deezer.com/album/%s' % tracks_data['data'][0]['album']['id']) albums_data = album_response.json() album = Album(name=albums_data['title'], album_id=albums_data['id'], artist=album_artist.get()) album.save() track = Track(name=tracks_data['title'], track_id=tracks_data['id'], album=album) track.save() return redirect('getTrackByName', name=tracks_data['title']) else: artist_response = requests.get( 'https://api.deezer.com/artist/%s' % tracks_data['artist']['id']) artists_data = artist_response.json() artist = Artist(name=artists_data['name'], artist_id=artists_data['id'], link=artists_data['link'], tracklist=artists_data['tracklist']) artist.save() album_response = requests.get( 'https://api.deezer.com/album/%s' % tracks_data['album']['id']) albums_data = album_response.json() album = Album(name=albums_data['title'], album_id=albums_data['id'], artist=artist) album.save() track = Track(name=tracks_data['title'], track_id=tracks_data['id'], album=album) track.save() return redirect('getTrackByName', name=tracks_data['title'])
def addTrack(request): N = 27 pi = ''.join( random.choice(string.ascii_uppercase + string.digits) for _ in range(N)) if request.GET.get('name') and request.GET.get('album_id'): try: album = Album.objects.get(album_id=request.GET.get('album_id')) if album: tracks = album.track_set.all() track_list = [] x = 0 while x < (len(tracks)): single_track = tracks[x].name track_list.append(single_track) x += 1 if request.GET.get('name') in track_list: return redirect('getTrackByName', name=request.GET.get('name')) else: track = Track(name=request.GET.get('name'), track_id=pi, album=album) track.save() return redirect('getTrackByName', name=request.GET.get('name')) except ObjectDoesNotExist: album_response = requests.get('https://api.deezer.com/album/%s' % request.GET.get('album_id')) albums_data = album_response.json() if not albums_data.get('error'): try: Artist.objects.get( artist_id=albums_data['contributors'][0]['id']) artist = Artist.objects.get( artist_id=albums_data['contributors'][0]['id']) if artist: album = Album(name=albums_data['title'], album_id=albums_data['id'], artist=artist) album.save() track = Track(name=request.GET.get( 'name', track_id=pi, album=album)) track.save() return redirect('getTrackByName', name=request.GET.get('name')) except ObjectDoesNotExist: artist_response = requests.get( 'https://api.deezer.com/artist/%s' % albums_data['contributors'][0]['id']) artists_data = artist_response.json() artist = Artist(name=artists_data['name'], artist_id=artists_data['id'], link=artists_data['link'], tracklist=artists_data['tracklist']) artist.save() album = Album(name=albums_data['title'], album_id=albums_data['id'], artist=artist) album.save() track = Track( name=request.GET.get('name', track_id=pi, album=album)) track.save() return redirect('getTrackByName', name=request.GET.get('name')) else: return HttpResponse( 'Album is not registered neither in API or Database.') return HttpResponse( 'The {{name}} param and the {{album_id}} param is required to associate track to album' )