def denormalize(attachment): flat, created = get_attachment_flat_model().objects.update_or_create( attachment=attachment, defaults={"object_link": get_object_link(attachment)} ) return flat
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
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]
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()
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)
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)
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))
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())
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())
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())
class Meta: model = get_attachment_flat_model() fields = "__all__"
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()
def test_get_attachment_flat_model_override(settings): settings.ATTACHMENT_FLAT_MODEL = "demo.sample.models.AttachmentFlatOverride" assert utils.get_attachment_flat_model() == AttachmentFlatOverride
def test_get_attachment_flat_model_default(): assert utils.get_attachment_flat_model() == AttachmentFlat
def patch(self, *args, **kwargs): super().patch(*args, **kwargs) return Response( AttachmentFlatSerializer( get_attachment_flat_model().objects.filter( attachment=self.instance).first()).data)