def create_album(request): if request.method != 'POST': return redirect(main_map) context = {} errs = [] context['errors'] = errs # Get new album name name = request.POST.get('album_name') # Try to create the album album, errors = Album.create(request.user.username, name) context['album'] = album if errors: print(errors) errs.extend(errors) else: context['message'] = "Album successfully created!" return render(request, 'json/create_album_response.json', context, content_type="application/json")
def post_album(request): # Retrieve API key from the GET URL api_key = request.GET.get('api_key') # Do we have a user with this api key? try: profile = Profile.objects.get(api_key=api_key) except Exception as e: return JsonResponse(bad_api_key_error, status=403) # Try to get the data try: data = json.loads(request.body.decode('utf-8')).get('data') except: return JsonResponse(bad_format_errors(["Could not parse JSON body"]), status=400) if not data: return JsonResponse(bad_format_errors(["Missing 'data' component of request"]), status=400) # Retrieve the name attributes = data.get('attributes') try: name = attributes.get('name') except: return JsonResponse(bad_format_errors(["Missing 'attributes' component of request data"]), status=400) # Try to create the album album,errs = Album.create(profile.user.username, name) if errs: return JsonResponse(bad_format_errors(errs), status=400) # Create response object context = { 'data': album.as_dict(True,True) } return JsonResponse(context, status=201)
def test_create_no_user(self): a,err = Album.create(username='******', name='test') self.assertEqual(a, None)
def test_create_with_name(self): a,err = Album.create(username='******', name='my album!') self.assertEqual(a.name, 'my album!')
def test_create_no_name(self): a,err = Album.create(username='******', name='') self.assertEqual(err,None) self.assertEqual(a.name, 'Untitled Album') self.assertEqual(a.user.username, 'jlbrooks')