Exemplo n.º 1
0
def file_handler_helper(post, uploadedfile):
	f = Attachment()
	# Save first to generate pk for random file system
	f.save()
	f.uploader = User.objects.get(pk=40) 	# Hardcoded for now
	f.attachment = uploadedfile
	if post.get('description'):
		f.description = post['description']
	f.save()
Exemplo n.º 2
0
def file_handler_helper(uploadedfile, description):
	f = Attachment()
	# Save first to generate pk for random file system
	f.save()
	f.uploader = User.objects.get(pk=40) 	# Hardcoded for now
	f.attachment = uploadedfile
	if description:
		f.description = description
	f.save()
	return f
Exemplo n.º 3
0
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})
Exemplo n.º 4
0
    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()
Exemplo n.º 5
0
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, "")
Exemplo n.º 6
0
    def get_pieces_movements(self, rettype):
        
        users = self.__get_ddmal_users()
        
        query = PIECE_MOVEMENT_QUERY.format(rettype)
        self.__connect()
        self.curs.execute(query)

        objects = self.curs.fetchall()

        print "Deleting {0}".format(rettype)
        if rettype == "piece":
            Piece.objects.all().delete()
        elif rettype == "movement":
            Movement.objects.all().delete()

        print "Adding {0}".format(rettype)
        for item in objects:
            # LM: composer may be none
            try:
                composer_obj = Composer.objects.get(old_id=item['composer_id'])
            except Composer.DoesNotExist:
                composer_obj = None

            for user in users:
                if item.get('uploader') == user.get('uid'):
                    user_obj = User.objects.get(username=user.get('name'))
                    break

            if rettype == "piece":
                collection_objs = Collection.objects.filter(old_id=item['book_id'])
                if not collection_objs.exists():
                    collection_objs = None
                else:
                    collection_objs = collection_objs
            elif rettype == "movement":
                parent_obj = self.__resolve_movement_parent(item['old_id'])
                collection_objs = Collection.objects.filter(old_id=item['book_id'])
                if not collection_objs.exists():
                    collection_objs = None
                else:
                    collection_objs = collection_objs

            p = {
                'uploader': user_obj,
                'composer': composer_obj,
                'old_id': item.get('old_id', None),
                'title': my_decoder(item.get('title', None)),
                'date_of_composition': pytz.utc.localize(item.get('date_of_composition', None)),
                'date_of_composition2': pytz.utc.localize(item.get('date_of_composition2', None)),  
                'number_of_voices': item.get('number_of_voices', None),
                'comment': my_decoder(item.get('comment', None)),
                'created': datetime.datetime.fromtimestamp(item.get('created')),
                'updated': datetime.datetime.fromtimestamp(item.get('updated'))
            }

            #if not item.get('composition_start_date', None) == item.get('composition_end_date', None):

            if rettype == "piece":
                x = Piece(**p)

            elif rettype == "movement":
                p.update({'piece': parent_obj})
                x = Movement(**p)
                
            
            x.save()
            if not (collection_objs is None or collection_objs is []):
                for collection_obj in collection_objs:
                    x.collections.add(collection_obj)
            x.save()

        self.__disconnect()
        
        print "Tagging {0}".format(rettype)
        # filters out composers (tid 3)
        ITEM_TAG_QUERY = """SELECT ti.nid, ti.tid FROM taxonomy_index ti
                             LEFT JOIN taxonomy_term_data td ON td.tid = ti.tid
                             WHERE ti.nid = %s"""

        if rettype == "piece":
            objects = Piece.objects.all()
        elif rettype == "movement":
            objects = Movement.objects.all()

        for item in objects:
            self.__connect()
            self.curs.execute(ITEM_TAG_QUERY, [item.old_id])
            tags = self.curs.fetchall()
            for tag in tags:
                tag_obj = Tag.objects.filter(old_id=tag.get('tid'))
                if not tag_obj.exists():
                    continue
                item.tags.add(tag_obj[0])
                item.save()

            self.__disconnect()
            
            
        ITEM_ATTACHMENT_QUERY = """SELECT ff.field_files_description AS description, fm.timestamp AS created,
                                    fm.uid AS uploader, fm.filename AS filename, fm.uri AS uri FROM field_data_field_files ff
                                    LEFT JOIN file_managed fm ON ff.field_files_fid = fm.fid
                                    WHERE ff.entity_id = %s"""

        print "Attaching files to {0}".format(rettype)
        if rettype == "piece":
            objects = Piece.objects.all()

            # LM: Deletion of previous attachments should happen here, otherwise attaching movements will delete attachments to pieces
            print "Deleting attachments"
            #Attachment.objects.all().delete()
            attachment_set = Attachment.objects.all()
            for item in attachment_set:
                item.delete()

        elif rettype == "movement":
            objects = Movement.objects.all()

        

        print("Attaching for real - LM")

        self.__connect()
        failures = 0
        for item in objects:
            self.curs.execute(ITEM_ATTACHMENT_QUERY, [item.old_id])
            attachments = self.curs.fetchall()
            for attachment in attachments:
                for user in users:
                    if attachment.get('uploader') == user.get('uid'):
                        user_obj = User.objects.get(username=user.get('name'))
                        break

                a = Attachment()
                a.save()  # ensure we've got a PK before we try and attach a file.

                # LM filename from old attachment objects
                filename = attachment.get('filename')  
                #filename = "test_file.mei"  # for testing.
                
                # LM Catch IO Error when trying to find file
                try:
                    filepath = os.path.join(DRUPAL_FILE_PATH, filename)
                    f = open(filepath, 'rb')
                except IOError:
                    # print('Failed to open: ' + filename)
                    # Some files have _0 appended to name before extension.... try that
                    try:
                        filename_temp, extension_temp = os.path.splitext(filename)
                        filename = filename_temp + '_0' + extension_temp
                        filepath = os.path.join(DRUPAL_FILE_PATH, filename)
                        f = open(filepath)
                    # If that doesn't work then pass
                    except IOError:
                        print('Failure: ' + filename)
                        failures = failures + 1
                        continue     


                attached_file = os.path.join(a.attachment_path, filename)

                # LM Try to localise timezone
                #try:
                date_created = pytz.utc.localize(datetime.datetime.fromtimestamp(attachment.get('created')))
                #except AttributeError:
                #    date_created = datetime.datetime.fromtimestamp(attachment.get('created'))


                s = {
                    'uploader': user_obj,
                    'description': attachment.get('description', None),
                    'created': date_created,
                    'old_id': attachment.get('old_id', None)
                }
                a.__dict__.update(**s)
                a.save()

                a.attachment.save(filename, File(f))
                a.save()
                f.close()
                
                #print('Attaching ' + a.attachment.attachment_path + ' to ' + item.title)
                item.attachments.add(a)
                

        self.__disconnect()
        print('failed attachments', failures)
Exemplo n.º 7
0
    def get_pieces_movements(self, rettype):
        users = self.__get_ddmal_users()
        query = PIECE_MOVEMENT_QUERY.format(rettype)
        self.__connect()
        self.curs.execute(query)

        objects = self.curs.fetchall()

        print "Deleting {0}".format(rettype)
        if rettype == "piece":
            Piece.objects.all().delete()
        elif rettype == "movement":
            Movement.objects.all().delete()

        print "Adding {0}".format(rettype)
        for item in objects:
            composer_obj = Composer.objects.get(old_id=item['composer_id'])
            for user in users:
                if item.get('uploader') == user.get('uid'):
                    user_obj = User.objects.get(username=user.get('name'))
                    break

            if rettype == "piece":
                parent_obj = Corpus.objects.filter(old_id=item['book_id'])
                if not parent_obj.exists():
                    parent_obj = None
                else:
                    parent_obj = parent_obj[0]
            elif rettype == "movement":
                parent_obj = self.__resolve_movement_parent(item['old_id'])
                corpus_obj = Corpus.objects.filter(old_id=item['book_id'])
                if not corpus_obj.exists():
                    corpus_obj = None
                else:
                    corpus_obj = corpus_obj[0]

            p = {
                'uploader': user_obj,
                'composer': composer_obj,
                'old_id': item.get('old_id', None),
                'title': item.get('title', None),
                'date_of_composition': item.get('date_of_composition', None),
                'number_of_voices': item.get('number_of_voices', None),
                'comment': item.get('comment', None),
                'created':
                datetime.datetime.fromtimestamp(item.get('created')),
                'updated': datetime.datetime.fromtimestamp(item.get('updated'))
            }

            if rettype == "piece":
                p.update({'corpus': parent_obj})
                x = Piece(**p)
            elif rettype == "movement":
                p.update({'piece': parent_obj, 'corpus': corpus_obj})
                x = Movement(**p)
            x.save()
        self.__disconnect()

        print "Tagging {0}".format(rettype)
        # filters out composers (tid 3)
        ITEM_TAG_QUERY = """SELECT ti.nid, ti.tid FROM taxonomy_index ti
                             LEFT JOIN taxonomy_term_data td ON td.tid = ti.tid
                             WHERE ti.nid = %s"""

        if rettype == "piece":
            objects = Piece.objects.all()
        elif rettype == "movement":
            objects = Movement.objects.all()

        for item in objects:
            self.__connect()
            self.curs.execute(ITEM_TAG_QUERY, item.old_id)
            tags = self.curs.fetchall()
            for tag in tags:
                tag_obj = Tag.objects.filter(old_id=tag.get('tid'))
                if not tag_obj.exists():
                    continue
                item.tags.add(tag_obj[0])
                item.save()

            self.__disconnect()

        ITEM_ATTACHMENT_QUERY = """SELECT ff.field_files_description AS description, fm.timestamp AS created,
                                    fm.uid AS uploader, fm.filename AS filename, fm.uri AS uri FROM field_data_field_files ff
                                    LEFT JOIN file_managed fm ON ff.field_files_fid = fm.fid
                                    WHERE ff.entity_id = %s"""

        print "Attaching files to {0}".format(rettype)
        if rettype == "piece":
            objects = Piece.objects.all()
        elif rettype == "movement":
            objects = Movement.objects.all()

        print "Deleting attachments"
        Attachment.objects.all().delete()

        self.__connect()
        for item in objects:
            self.curs.execute(ITEM_ATTACHMENT_QUERY, item.old_id)
            attachments = self.curs.fetchall()
            for attachment in attachments:
                for user in users:
                    if attachment.get('uploader') == user.get('uid'):
                        user_obj = User.objects.get(username=user.get('name'))
                        break

                a = Attachment()
                a.save(
                )  # ensure we've got a PK before we try and attach a file.

                # filename = attachment.get('filename')[9:]  # lop the 'public://' off.
                filename = "test_file.mei"  # for testing.

                filepath = os.path.join(DRUPAL_FILE_PATH, filename)
                f = open(filepath, 'rb')

                # attached_file = os.path.join(a.attachment_path, filename)
                s = {
                    'uploader':
                    user_obj,
                    'description':
                    attachment.get('description', None),
                    'created':
                    datetime.datetime.fromtimestamp(attachment.get('created')),
                    'old_id':
                    attachment.get('old_id', None)
                }
                a.__dict__.update(**s)
                a.save()

                a.attachment.save(filename, File(f))
                a.save()
                f.close()
                item.attachments.add(a)

        self.__disconnect()
Exemplo n.º 8
0
    def get_pieces_movements(self, rettype):
        users = self.__get_ddmal_users()
        query = PIECE_MOVEMENT_QUERY.format(rettype)
        self.__connect()
        self.curs.execute(query)

        objects = self.curs.fetchall()

        print "Deleting {0}".format(rettype)
        if rettype == "piece":
            Piece.objects.all().delete()
        elif rettype == "movement":
            Movement.objects.all().delete()

        print "Adding {0}".format(rettype)
        for item in objects:
            composer_obj = Composer.objects.get(old_id=item['composer_id'])
            for user in users:
                if item.get('uploader') == user.get('uid'):
                    user_obj = User.objects.get(username=user.get('name'))
                    break

            if rettype == "piece":
                parent_obj = Corpus.objects.filter(old_id=item['book_id'])
                if not parent_obj.exists():
                    parent_obj = None
                else:
                    parent_obj = parent_obj[0]
            elif rettype == "movement":
                parent_obj = self.__resolve_movement_parent(item['old_id'])
                corpus_obj = Corpus.objects.filter(old_id=item['book_id'])
                if not corpus_obj.exists():
                    corpus_obj = None
                else:
                    corpus_obj = corpus_obj[0]

            p = {
                'uploader': user_obj,
                'composer': composer_obj,
                'old_id': item.get('old_id', None),
                'title': item.get('title', None),
                'date_of_composition': item.get('date_of_composition', None),
                'number_of_voices': item.get('number_of_voices', None),
                'comment': item.get('comment', None),
                'created': datetime.datetime.fromtimestamp(item.get('created')),
                'updated': datetime.datetime.fromtimestamp(item.get('updated'))
            }

            if rettype == "piece":
                p.update({'corpus': parent_obj})
                x = Piece(**p)
            elif rettype == "movement":
                p.update({'piece': parent_obj, 'corpus': corpus_obj})
                x = Movement(**p)
            x.save()
        self.__disconnect()

        print "Tagging {0}".format(rettype)
        # filters out composers (tid 3)
        ITEM_TAG_QUERY = """SELECT ti.nid, ti.tid FROM taxonomy_index ti
                             LEFT JOIN taxonomy_term_data td ON td.tid = ti.tid
                             WHERE ti.nid = %s"""

        if rettype == "piece":
            objects = Piece.objects.all()
        elif rettype == "movement":
            objects = Movement.objects.all()

        for item in objects:
            self.__connect()
            self.curs.execute(ITEM_TAG_QUERY, item.old_id)
            tags = self.curs.fetchall()
            for tag in tags:
                tag_obj = Tag.objects.filter(old_id=tag.get('tid'))
                if not tag_obj.exists():
                    continue
                item.tags.add(tag_obj[0])
                item.save()

            self.__disconnect()

        ITEM_ATTACHMENT_QUERY = """SELECT ff.field_files_description AS description, fm.timestamp AS created,
                                    fm.uid AS uploader, fm.filename AS filename, fm.uri AS uri FROM field_data_field_files ff
                                    LEFT JOIN file_managed fm ON ff.field_files_fid = fm.fid
                                    WHERE ff.entity_id = %s"""

        print "Attaching files to {0}".format(rettype)
        if rettype == "piece":
            objects = Piece.objects.all()
        elif rettype == "movement":
            objects = Movement.objects.all()

        print "Deleting attachments"
        Attachment.objects.all().delete()

        self.__connect()
        for item in objects:
            self.curs.execute(ITEM_ATTACHMENT_QUERY, item.old_id)
            attachments = self.curs.fetchall()
            for attachment in attachments:
                for user in users:
                    if attachment.get('uploader') == user.get('uid'):
                        user_obj = User.objects.get(username=user.get('name'))
                        break

                a = Attachment()
                a.save()  # ensure we've got a PK before we try and attach a file.

                # filename = attachment.get('filename')[9:]  # lop the 'public://' off.
                filename = "test_file.mei"  # for testing.

                filepath = os.path.join(DRUPAL_FILE_PATH, filename)
                f = open(filepath, 'rb')

                # attached_file = os.path.join(a.attachment_path, filename)
                s = {
                    'uploader': user_obj,
                    'description': attachment.get('description', None),
                    'created': datetime.datetime.fromtimestamp(attachment.get('created')),
                    'old_id': attachment.get('old_id', None)
                }
                a.__dict__.update(**s)
                a.save()

                a.attachment.save(filename, File(f))
                a.save()
                f.close()
                item.attachments.add(a)

        self.__disconnect()