Esempio n. 1
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()