def test_get_album_exists(self): a = Album.get_or_create_default_for_user(username='******') self.assertEqual(a.name, settings.DEFAULT_ALBUM_NAME) self.assertEqual(a.user, self.user) # Try to create again other_a = Album.get_or_create_default_for_user(username='******') self.assertEqual(other_a, a)
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)
import os parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) os.sys.path.insert(0,parentdir) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "frebapps.settings") from geosnapr.models import User, Album, Image import django if __name__ == '__main__': django.setup() # Get all users users = User.objects.all() for user in users: # Create the default album album = Album.get_or_create_default_for_user(username=user.username) # Associate all images with the default album images = Image.objects.filter(user=user) for image in images: if image not in album.images.all(): album.images.add(image) album.save() # Create the public album public = Album.get_or_create_public_for_user(username=user.username)
def test_get_no_album(self): a = Album.get_or_create_default_for_user(username='******') self.assertEqual(a.name, settings.DEFAULT_ALBUM_NAME) self.assertEqual(a.user, self.user)
def test_get_bad_user(self): a = Album.get_or_create_default_for_user(username='******') 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_user(self): a,err = Album.create(username='******', name='test') self.assertEqual(a, None)
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')