Пример #1
0
    def _migrate_abstract(self, old_abstract):
        submitter = self.user_from_legacy(old_abstract._submitter._user,
                                          system_user=True)
        submitted_dt = old_abstract._submissionDate
        modified_dt = (old_abstract._modificationDate if
                       (submitted_dt - old_abstract._modificationDate) >
                       timedelta(seconds=10) else None)
        description = getattr(old_abstract, '_fields', {}).get('content', '')
        description = convert_to_unicode(
            getattr(description, 'value',
                    description))  # str or AbstractFieldContent

        type_ = old_abstract._contribTypes[0]
        type_id = None
        try:
            type_id = self.event_ns.legacy_contribution_type_map[
                type_].id if type_ else None
        except KeyError:
            self.print_warning(
                'Abstract {} - invalid contrib type {}, setting to None'.
                format(old_abstract._id,
                       convert_to_unicode(getattr(type_, '_name',
                                                  str(type_)))))

        abstract = Abstract(friendly_id=int(old_abstract._id),
                            title=convert_to_unicode(old_abstract._title),
                            description=description,
                            submitter=submitter,
                            submitted_dt=submitted_dt,
                            submitted_contrib_type_id=type_id,
                            submission_comment=convert_to_unicode(
                                old_abstract._comments),
                            modified_dt=modified_dt)
        self.print_info('%[white!]Abstract %[cyan]{}%[reset]: {}'.format(
            abstract.friendly_id, abstract.title))
        self.event.abstracts.append(abstract)
        self.event_ns.abstract_map[old_abstract] = abstract

        accepted_type_id = None
        accepted_track_id = None

        old_contribution = getattr(old_abstract, '_contribution', None)
        if old_contribution:
            assert old_contribution.__class__.__name__ == 'AcceptedContribution'
            if old_abstract._currentStatus.__class__.__name__ == 'AbstractStatusAccepted':
                old_contrib_type = old_abstract._currentStatus._contribType
                try:
                    accepted_type_id = (
                        self.event_ns.
                        legacy_contribution_type_map[old_contrib_type].id
                        if old_contrib_type else None)
                except KeyError:
                    self.print_warning(
                        '%[yellow!]Contribution {} - invalid contrib type {}, setting to None'
                        .format(old_contribution.id,
                                convert_to_unicode(old_contrib_type._name)))

                old_accepted_track = old_abstract._currentStatus._track
                accepted_track_id = int(
                    old_accepted_track.id) if old_accepted_track else None

        if old_contribution and old_contribution.id is not None:
            self.event_ns.legacy_contribution_abstracts[
                old_contribution] = abstract

        try:
            accepted_track = (
                self.event_ns.track_map_by_id.get(accepted_track_id)
                if accepted_track_id is not None else None)
        except KeyError:
            self.print_error(
                '%[yellow!]Abstract #{} accepted in invalid track #{}'.format(
                    abstract.friendly_id, accepted_track_id))
            accepted_track = None

        # state
        old_state = old_abstract._currentStatus
        old_state_name = old_state.__class__.__name__
        self.event_ns.old_abstract_state_map[abstract] = old_state
        abstract.state = self.STATE_MAP[old_state_name]

        if abstract.state == AbstractState.accepted:
            abstract.accepted_contrib_type_id = accepted_type_id
            abstract.accepted_track = accepted_track

        if abstract.state in self.JUDGED_STATES:
            abstract.judge = self.user_from_legacy(old_state._responsible,
                                                   system_user=True)
            abstract.judgment_dt = as_utc(old_state._date)

        # files
        for old_attachment in getattr(old_abstract, '_attachments',
                                      {}).itervalues():
            storage_backend, storage_path, size, md5 = self._get_local_file_info(
                old_attachment)
            if storage_path is None:
                self.print_error(
                    '%[red!]File not found on disk; skipping it [{}]'.format(
                        convert_to_unicode(old_attachment.fileName)))
                continue
            content_type = mimetypes.guess_type(
                old_attachment.fileName)[0] or 'application/octet-stream'
            filename = secure_filename(
                convert_to_unicode(old_attachment.fileName), 'attachment')
            attachment = AbstractFile(filename=filename,
                                      content_type=content_type,
                                      size=size,
                                      md5=md5,
                                      storage_backend=storage_backend,
                                      storage_file_id=storage_path)
            abstract.files.append(attachment)

        # internal comments
        for old_comment in old_abstract._intComments:
            comment = AbstractComment(
                user=self.user_from_legacy(old_comment._responsible,
                                           system_user=True),
                text=convert_to_unicode(old_comment._content),
                created_dt=old_comment._creationDate,
                modified_dt=old_comment._modificationDate)
            abstract.comments.append(comment)

        # tracks
        reallocated = set(r._track for r in getattr(
            old_abstract, '_trackReallocations', {}).itervalues())
        for old_track in old_abstract._tracks.values():
            abstract.reviewed_for_tracks.add(
                self.event_ns.track_map.get(old_track))
            if old_track not in reallocated:
                abstract.submitted_for_tracks.add(
                    self.event_ns.track_map.get(old_track))

        # reviews/judgments
        self._migrate_abstract_reviews(abstract, old_abstract)
        # persons
        self._migrate_abstract_persons(abstract, old_abstract)
        # email log
        self._migrate_abstract_email_log(abstract, old_abstract)

        # contribution/abstract fields
        abstract.field_values = list(
            self._migrate_abstract_field_values(old_abstract))
        return abstract