示例#1
0
def denormalize(attachment):
    flat, created = get_attachment_flat_model().objects.update_or_create(
        attachment=attachment,
        defaults={"object_link": get_object_link(attachment)}
    )

    return flat
示例#2
0
 def run(self):
     attachment_qs = get_attachment_flat_model().objects.filter(source="")
     for flat in attachment_qs:
         try:
             flat.source = get_source(flat.attachment)
             flat.save()
         except Attachment.DoesNotExist:
             pass
示例#3
0
class AttachmentListView(ListAPIView):
    queryset = get_attachment_flat_model().objects.exclude(
        Q(attachment__file__isnull=True) | Q(attachment__file__exact=""),
        Q(attachment__hyperlink__isnull=True)
        | Q(attachment__hyperlink__exact=""))
    permission_classes = (get_attachment_permissions(), )
    serializer_class = AttachmentFlatSerializer
    filter_backends = (QueryStringFilterBackend, )
    filter_fields = [f for f in AttachmentFlatSerializer().fields]
示例#4
0
 def run(self):
     for flat in get_attachment_flat_model().objects.all():
         try:
             flat.object_link = flat.attachment.content_object.get_object_url(
             )
         except AttributeError:
             continue
         else:
             flat.save()
示例#5
0
    def run(self):
        attachment_qs = Attachment.objects

        if not self.all_flag:
            attachment_qs = attachment_qs.exclude(
                pk__in=get_attachment_flat_model().objects.values_list(
                    "attachment_id", flat=True))

        for attachment in attachment_qs.all():
            get_denormalize_func()(attachment)
示例#6
0
    def run(self):
        attachment_qs = Attachment.objects

        if not self.all_flag:
            attachment_qs = attachment_qs.exclude(
                pk__in=get_attachment_flat_model().objects.values_list(
                    "attachment_id",
                    flat=True
                )
            )

        for attachment in attachment_qs.all():
            get_denormalize_func()(attachment)
示例#7
0
 def test_str(self):
     attachment = AttachmentFactory(
         file=SimpleUploadedFile(
             'simple_file.txt', 'R\xe4dda Barnen'.encode('utf-8')
         ),
         content_object=self.simple_object
     )
     flat_qs = get_attachment_flat_model().objects.filter(
         attachment=attachment
     )
     self.assertTrue(flat_qs.exists())
     flat = flat_qs.first()
     self.assertEqual(str(flat), str(attachment.file))
示例#8
0
    def test_trips_excluded(self):
        travel_attachment = TravelAttachmentFactory(
            travel=TravelFactory(),
            type="Travel Attachment PDF",
        )
        flat_qs = get_attachment_flat_model().objects
        flat_count = flat_qs.count()
        attachment = AttachmentFactory(
            file="test_file.pdf",
            file_type=AttachmentFileTypeFactory(code="t2f_travel_attachment"),
            code="t2f_travel_attachment",
            content_object=travel_attachment,
        )

        self.assertEqual(flat_qs.count(), flat_count)
        self.assertTrue(attachment not in flat_qs.all())
示例#9
0
    def test_run(self):
        AttachmentFlat = get_attachment_flat_model()
        user = UserFactory()
        code = "test_code"
        file_type = AttachmentFileTypeFactory(code=code)
        attachment = AttachmentFactory(file_type=file_type,
                                       code=code,
                                       file="sample1.pdf",
                                       content_object=file_type,
                                       uploaded_by=user)
        flat_qs = AttachmentFlat.objects.filter(attachment=attachment)
        self.assertTrue(flat_qs.exists())
        AttachmentFlat.objects.get(attachment=attachment).delete()
        self.assertFalse(flat_qs.exists())

        call_command("denormalize_attachments")

        self.assertTrue(flat_qs.exists())
示例#10
0
    def test_run(self):
        AttachmentFlat = get_attachment_flat_model()
        user = UserFactory()
        code = "test_code"
        file_type = AttachmentFileTypeFactory(code=code)
        attachment = AttachmentFactory(
            file_type=file_type,
            code=code,
            file="sample1.pdf",
            content_object=file_type,
            uploaded_by=user
        )
        flat_qs = AttachmentFlat.objects.filter(attachment=attachment)
        self.assertTrue(flat_qs.exists())
        AttachmentFlat.objects.get(attachment=attachment).delete()
        self.assertFalse(flat_qs.exists())

        call_command("denormalize_attachments")

        self.assertTrue(flat_qs.exists())
示例#11
0
 class Meta:
     model = get_attachment_flat_model()
     fields = "__all__"
示例#12
0
def test_get_attachment_flat_model_invalid(settings):
    settings.ATTACHMENT_FLAT_MODEL = "demo.sample.wrong.AttachmentFlatOverride"
    with pytest.raises(ImproperlyConfigured):
        utils.get_attachment_flat_model()
示例#13
0
def test_get_attachment_flat_model_override(settings):
    settings.ATTACHMENT_FLAT_MODEL = "demo.sample.models.AttachmentFlatOverride"
    assert utils.get_attachment_flat_model() == AttachmentFlatOverride
示例#14
0
def test_get_attachment_flat_model_default():
    assert utils.get_attachment_flat_model() == AttachmentFlat
示例#15
0
 def patch(self, *args, **kwargs):
     super().patch(*args, **kwargs)
     return Response(
         AttachmentFlatSerializer(
             get_attachment_flat_model().objects.filter(
                 attachment=self.instance).first()).data)