def create_movement(request): if request.method == 'POST': form = MovementForm(request.POST, request.FILES) if form.is_valid(): clean_form = form.cleaned_data # Hardcoded user for now uploader = User.objects.get(pk=40) # Get and create attachment if possible try: attach = clean_form['attachment'] attachment = Attachment() attachment.save() attachment.attachment = attach attachment.uploader = uploader attachment.description = clean_form.get('description') attachment.save() except KeyError: attachment = None # Handle tags tags = tag_handler(clean_form['tags']) # Handle objects piece = object_handler(Piece, clean_form['piece']) corpus = object_handler(Corpus, clean_form.get('corpus')) composer = composer_handler(clean_form.get('composer')) # Create movement movement = Movement(title=clean_form['title'], composer=composer, corpus=corpus, piece=piece, date_of_composition=clean_form.get('date_of_composition'), number_of_voices=clean_form.get('number_of_voices'), comment=clean_form.get('comment'), attachment=attachment, tags=tags, uploader=uploader ) movement.save() else: form = MovementForm(initial={'title':'Movement title', 'composer': 'Composer', 'corpus': 'Corpus', 'piece': 'Piece', 'date_of_composition':'date of composition', 'comment': 'Add a comment...', 'tags': 'Comma-separated list of tags', 'description':'Description of movement file...'}) return render(request, 'forms/movement.html', {'form':form})
def create_movement_object(upfile, piece, composer, corpus, description, comments, voices, tags, dates, titles, voice, date, descrip, tag, comment, title): user = User.objects.get(pk=40) # Hardcoded for now final_tags = tags+tag final_description = descrip if descrip else description final_date = date_handler(date) if date else date_handler(dates) final_voice = voice if voice else voices final_comment = comment if comment else comments final_title = title if title else titles # Ugh...fix date stuff if final_date == '': final_date = datetime.today() # First create attachment attachment = file_handler_helper(upfile, final_description) # Handle tags, composer, corpus tag_objects = tag_handler(final_tags) composer_obj = composer_handler(composer) corpus_obj = object_handler(Corpus, corpus) # Next create the movement mov = Movement( title=final_title, composer=composer_obj, uploader=user, piece =piece, corpus=corpus_obj, number_of_voices=final_voice, comment=final_comment, date_of_composition=final_date ) mov.save() # Add attachment to movement mov.attachments.add(attachment) mov.save() # Add tags to movement for tag in tag_objects: mov.tags.add(tag) mov.save()
class CollectionTestCase(ElvisTestSetup, APITestCase): def setUp(self): self.setUp_user() self.c = Collection() self.c.title = "This is the collection title" self.c.save() self.p = Piece() self.p.creator = self.creator_user self.p.save() self.composer = Composer() self.composer.name = "Fake Composer" self.composer.save() self.m = Movement() self.m.creator = self.creator_user self.m.composer = self.composer self.m.save() def tearDown(self): self.c.delete() self.p.delete() self.m.delete() self.composer.delete() def test_unicode(self): self.assertEqual(str(self.c), "This is the collection title") def test_add_piece(self): self.assertTrue(self.p not in self.c) self.assertEqual(self.c.piece_count, 0) self.c.add(self.p) self.assertEqual(self.c.piece_count, 1) self.assertEqual(self.p, self.c.pieces.first()) self.assertTrue(self.p in self.c) def test_add_movement(self): self.assertTrue(self.m not in self.c) self.assertEqual(self.c.movement_count, 0) self.c.add(self.m) self.assertEqual(self.c.movement_count, 1) self.assertEqual(self.m, self.c.movements.first()) self.assertTrue(self.m in self.c) def test_remove_piece(self): self.assertTrue(self.p not in self.c) self.c.add(self.p) self.assertTrue(self.p in self.c) self.c.remove(self.p) self.assertTrue(self.p not in self.c) def test_remove_movement(self): self.assertTrue(self.m not in self.c) self.c.add(self.m) self.assertTrue(self.m in self.c) self.c.remove(self.m) self.assertTrue(self.m not in self.c) def test_get_free_movements(self): # First test with a free movement self.assertListEqual(list(self.c.free_movements), []) self.assertEqual(self.c.free_movements_count, 0) self.c.add(self.m) self.assertListEqual(list(self.c.free_movements), [self.m]) self.assertEqual(self.c.free_movements_count, 1) # Then, make self.m unfree and see that it responds self.m.piece = self.p self.p.movements.add(self.m) self.assertListEqual(list(self.c.free_movements), []) self.assertEqual(self.c.free_movements_count, 0) def test_container_contains_none(self): """ Test that checking "None in collection" does not crash the program. Instead, it returns false. :return: """ self.assertFalse(None in self.c) def test_invalid_contains_parameter(self): """ Test that calling the __contains__ interface on Collection with an unacceptable parameter throws an exception. :return: """ with self.assertRaises(Exception): 5 in self.c with self.assertRaises(Exception): "bad" in self.c def test_maintain_valid_collection(self): """ Test that a collection does not enter an invalid state when adding a movement or piece such that the movement is in the piece. An invalid state is when a collection contains both the movement and the piece even though the movement is contained within the piece. :return: """ # M is a movement in P self.m.piece = self.p self.p.movements.add(self.m) # Neither M or P is in C self.assertTrue(self.m not in self.c) self.assertTrue(self.p not in self.c) # Only M is in C self.c.add(self.m) self.assertTrue(self.m in self.c) self.assertTrue(self.p not in self.c) # Add P to C, thereby removing just M self.c.add(self.p) self.assertTrue(self.m not in self.c) self.assertTrue(self.p in self.c) # Try to add just M. M remains not in C and P remains in C self.c.add(self.m) self.assertTrue(self.m not in self.c) self.assertTrue(self.p in self.c)
class MovementTestCase(ElvisTestSetup, APITestCase): def setUp(self): self.setUp_user() self.p = Piece() self.p.creator = self.creator_user self.p.save() self.composer = Composer() self.composer.name = "Fake Composer" self.composer.save() self.m = Movement() self.m.creator = self.creator_user self.m.composer = self.composer self.m.title = "This is the movement title" self.m.save() self.source1 = Source() self.source1.title = "Source 1" self.source1.save() self.source2 = Source() self.source2.title = "Source 2" self.source2.save() self.genre1 = Genre() self.genre1.title = "Genre 1" self.genre1.save() self.genre2 = Genre() self.genre2.title = "Genre 2" self.genre2.save() self.attachment1 = Attachment() self.attachment1.description = "Attachment 1" self.attachment1.save() self.attachment2 = Attachment() self.attachment2.description = "Attachment 2" self.attachment2.save() def tearDown(self): self.p.delete() self.m.delete() self.composer.delete() self.source1.delete() self.source2.delete() self.genre1.delete() self.genre2.delete() self.attachment1.delete() def test_unicode(self): self.assertEqual(str(self.m), "This is the movement title") def test_get_parent_cart_id(self): # No piece self.assertEqual(self.m.get_parent_cart_id, "") # Add the piece self.m.piece = self.p # Assert that it matches the regex self.assertRegexpMatches(self.m.get_parent_cart_id, self.uuid_regexp) # Remove it self.m.piece = None self.assertEqual(self.m.get_parent_cart_id, "")