Example #1
0
 def validate_date(self, value):
     try:
         parse(value)
     except ValueError:
         raise serializers.ValidationError("Unable to parse value.")
     else:
         return value
Example #2
0
 def test_years_end_start(self):
     self.assertEqual(
         parse("2020<>2008"),
         (
             datetime.combine(date(2008, 1, 1), time.min),
             datetime.combine(date(2020, 12, 31), time.max),
         ),
     )
Example #3
0
    def create(self, validated_data):
        user = self.context["user"]
        source = validated_data["source"]
        collection = self.target_node.get_collection()
        start, end = parse(validated_data["date"])
        dt_range = DateTimeTZRange(lower=start, upper=end)
        # actually create the media object
        media = Media.objects.create(
            creation_date=dt_range,
            license=validated_data.get("media_license"),
            title=validated_data.get("media_title", ""),
            description=validated_data.get("media_description", ""),
            set=self.target_node,
            collection=collection,
            created_by=user,
            original_media_type=validated_data["media_type"],
            original_media_identifier=validated_data.get(
                "media_identifier", ""),
            embargo_end_date=validated_data.get("embargo_end_date"),
            is_private=validated_data.get("is_private", False),
            coords_lat=validated_data.get("media_lat"),
            coords_lon=validated_data.get("media_lon"),
        )

        # save m2m
        for media2creator in validated_data["creators"]:
            MediaToCreator.objects.create(media=media, **media2creator)

        # set tags
        #
        tags_data = validated_data.get("media_tags", [])
        tags = []
        for td in tags_data:
            tag_serializer = SimpleTagSerializer(
                data=td, context={"collection": self.collection})
            assert tag_serializer.is_valid()
            tag = tag_serializer.save()
            tags.append(tag)

        media.tags.set(tags)

        archive_file = ArchiveFile.objects.create(source_id=source,
                                                  created_by=user,
                                                  media=media)

        media.files.set([archive_file])

        attachment_files = []
        for attachment in validated_data["attachments"]:
            attachment_file = AttachmentFile.objects.create(
                source_id=attachment["source"], created_by=user)
            for creator in attachment["creators"]:
                FileCreator.objects.create(file=attachment_file, **creator)

            attachment_files.append(attachment_file)

        media.attachments.set(attachment_files)

        # update the ingest queue (if available) by removing the source
        queue = self.context.get("queue")
        if queue:
            queue = IngestQueue.objects.select_for_update().get(pk=queue.pk)
            queue.link_to_media(media, source)
            queue.save()
        archive_ids = [a.pk for a in chain([archive_file], attachment_files)]

        logger.info(
            "Triggering archiving for %d file(s) (%s); media: %d; user: %d",
            len(archive_ids),
            ", ".join([str(pk) for pk in archive_ids]),
            media.pk,
            user.pk,
        )

        def ingestion_trigger():
            return archive_and_create_webassets(
                archive_ids,
                media.pk,
                # these args are for websockets via channels
                self.context["channel"],
            )

        # this instructs django to execute the function after any commit
        transaction.on_commit(ingestion_trigger)
        return media