예제 #1
0
    def test_triple(self):
        normal = Normal.objects.language('en').get(pk=self.normal_id[1])
        standard = Standard.objects.get(pk=self.standard_id[1])
        simple = SimpleRelated.objects.language('en').create(normal=normal)

        obj = Normal.objects.language('en').get(standards__pk=standard.pk)
        self.assertEqual(obj.pk, normal.pk)

        obj = Normal.objects.language('en').get(simplerel__pk=simple.pk)
        self.assertEqual(obj.pk, normal.pk)

        # We created an english Normal object, so we want to make sure that we use 'en'
        with LanguageOverride('en'):
            obj = get_translation_aware_manager(Standard).get(
                normal__simplerel__pk=simple.pk)
            self.assertEqual(obj.pk, standard.pk)

        # If we don't use language 'en', it should give DoesNotExist, when using the
        # translation aware manager
        with LanguageOverride('ja'):
            manager = get_translation_aware_manager(Standard)
            self.assertRaises(Standard.DoesNotExist,
                              manager.get,
                              normal__simplerel__pk=simple.pk)

        # However, if we don't use the translation aware manager, we can query any
        # the shared fields in any language, and it should return the object,
        # even though there is no translated Normal objects
        with LanguageOverride('ja'):
            obj = Standard.objects.get(normal__simplerel__pk=simple.pk)
            self.assertEqual(obj.pk, standard.pk)
예제 #2
0
    def test_triple(self):
        normal = Normal.objects.language('en').create(shared_field='SHARED', translated_field='English')
        other = Other.objects.create(normal=normal)
        standard = Standard.objects.create(normal=normal, normal_field='NORMAL FIELD')

        obj = Normal.objects.language('en').get(standards__pk=standard.pk)
        self.assertEqual(obj.pk, normal.pk)

        obj = Normal.objects.language('en').get(others__pk=other.pk)
        self.assertEqual(obj.pk, normal.pk)

        # We created an english Normal object, so we want to make sure that we use 'en'
        with LanguageOverride('en'):
            obj = get_translation_aware_manager(Standard).get(normal__others__pk=other.pk)
            self.assertEqual(obj.pk, standard.pk)

        # If we don't use language 'en', it should give DoesNotExist, when using the
        # translation aware manager
        with LanguageOverride('ja'):
            manager = get_translation_aware_manager(Standard)
            self.assertRaises(Standard.DoesNotExist, manager.get, normal__others__pk=other.pk)

        # However, if we don't use the translation aware manager, we can query any
        # the shared fields in any language, and it should return the object,
        # even though there is no translated Normal objects
        with LanguageOverride('ja'):
            obj = Standard.objects.get(normal__others__pk=other.pk)
            self.assertEqual(obj.pk, standard.pk)
예제 #3
0
    def test_triple(self):
        normal = Normal.objects.language('en').get(pk=self.normal_id[1])
        standard = Standard.objects.get(pk=self.standard_id[1])
        simple = SimpleRelated.objects.language('en').create(normal=normal)

        obj = Normal.objects.language('en').get(standards__pk=standard.pk)
        self.assertEqual(obj.pk, normal.pk)

        obj = Normal.objects.language('en').get(simplerel__pk=simple.pk)
        self.assertEqual(obj.pk, normal.pk)

        # We created an english Normal object, so we want to make sure that we use 'en'
        with translation.override('en'):
            obj = get_translation_aware_manager(Standard).get(normal__simplerel__pk=simple.pk)
            self.assertEqual(obj.pk, standard.pk)

        # If we don't use language 'en', it should give DoesNotExist, when using the
        # translation aware manager
        with translation.override('ja'):
            manager = get_translation_aware_manager(Standard)
            self.assertRaises(Standard.DoesNotExist, manager.get, normal__simplerel__pk=simple.pk)

        # However, if we don't use the translation aware manager, we can query any
        # the shared fields in any language, and it should return the object,
        # even though there is no translated Normal objects
        with translation.override('ja'):
            obj = Standard.objects.get(normal__simplerel__pk=simple.pk)
            self.assertEqual(obj.pk, standard.pk)
예제 #4
0
    def test_proxy_simple_relation(self):
        self.assertEqual(NormalProxy.objects.count(), 0)
        self.assertEqual(NormalProxy.objects.language('en').count(), 0)

        NormalProxy.objects.language('en').create(shared_field='SHARED', translated_field='English')
        normal = NormalProxy.objects.get(shared_field='SHARED')
        SimpleRelatedProxy.objects.language('en').create(normal=normal, translated_field='RelatedEnglish')

        from hvad.utils import get_translation_aware_manager
        srp_manager = get_translation_aware_manager(SimpleRelatedProxy)
        qs = srp_manager.language('en').filter(normal__translated_field='English')
        self.assertEqual(qs.count(), 1)
        np_manager = get_translation_aware_manager(NormalProxy)
        qs = np_manager.language('en').filter(simplerel__translated_field='RelatedEnglish')
        self.assertEqual(qs.count(), 1)
예제 #5
0
class SiteEdit(LoginRequiredMixin, FormMixin, DetailView, SiteProcessingView):
    manager = get_translation_aware_manager(Site)
    queryset = manager.language()
    form_class = EditSiteForm
    model = Site
    template_name = 'archapp/edit.html'
    login_url = '/accounts/login/'
    redirect_field_name = 'redirect_to'

    def get_success_url(self):
        return reverse('archapp:allsites')

    def get_context_data(self, **kwargs):
        context = super(SiteEdit, self).get_context_data(**kwargs)
        context['form'] = self.get_form()
        context['title'] = "Edit Site"
        return context

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        form = self.get_form()

        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(form)

    def form_valid(self, form):
        # obtain current site, root can edit all of them
        params = {'pk': form.cleaned_data['site_id']}

        if not self.request.user.is_superuser:
            params['user'] = self.request.user

        try:
            site = Site.objects.get(**params)

            # update its name
            site.name = form.cleaned_data['name']

            # update all filters and images
            self.process_filters_and_pics(site=site, form=form, editing=True)

            # get 'bibliography'
            lit = form.cleaned_data['literature']

            # create new dict or update existing
            if type(site.data) is dict:
                site.data['Bibliography'] = lit
            else:
                site.data = {'Bibliography': lit}

            # save site
            site.save()

        finally:
            # no need to handle other case here. if user is
            # not root he or she shouldn't be able to edit
            # other's precious data.
            return super(SiteEdit, self).form_valid(form)
예제 #6
0
    def test_aware_qobject_filter(self):
        from hvad.utils import get_translation_aware_manager
        manager = get_translation_aware_manager(Standard)

        normal_one = Q(normal_field=STANDARD[1].normal_field)
        normal_two = Q(normal_field=STANDARD[2].normal_field)
        shared_one = Q(normal__shared_field=NORMAL[STANDARD[1].normal].shared_field)
        translated_one_en = Q(normal__translated_field=NORMAL[STANDARD[1].normal].translated_field['en'])
        translated_two_en = Q(normal__translated_field=NORMAL[STANDARD[2].normal].translated_field['en'])

        # control group test
        with translation.override('en'):
            qs = manager.filter(shared_one)
            self.assertEqual(qs.count(), 1)
            obj = qs[0]
            self.assertEqual(obj.normal_field, STANDARD[1].normal_field)

            # basic Q object test
            qs = manager.filter(translated_one_en)
            self.assertEqual(qs.count(), 1)
            obj = qs[0]
            self.assertEqual(obj.normal_field, STANDARD[1].normal_field)

            # test various intersection combinations
            # use a spurious Q to test the logic of recursion along the way
            qs = manager.filter(Q(normal_one & shared_one & translated_one_en))
            self.assertEqual(qs.count(), 1)
            obj = qs[0]
            self.assertEqual(obj.normal_field, STANDARD[1].normal_field)

            qs = manager.filter(Q(normal_one & translated_two_en))
            self.assertEqual(qs.count(), 0)
            qs = manager.filter(Q(shared_one & translated_two_en))
            self.assertEqual(qs.count(), 0)
            qs = manager.filter(Q(translated_one_en & translated_two_en))
            self.assertEqual(qs.count(), 0)

            # test various union combinations
            qs = manager.filter(Q(normal_one | translated_one_en))
            self.assertEqual(qs.count(), 1)
            qs = manager.filter(Q(shared_one | translated_one_en))
            self.assertEqual(qs.count(), 1)

            qs = manager.filter(Q(normal_one | translated_two_en))
            self.assertEqual(qs.count(), 2)
            qs = manager.filter(Q(shared_one | translated_two_en))
            self.assertEqual(qs.count(), 2)

            qs = manager.filter(Q(translated_one_en | translated_two_en))
            self.assertEqual(qs.count(), 2)

            # misc more complex combinations
            qs = manager.filter(Q(normal_one & (translated_one_en | translated_two_en)))
            self.assertEqual(qs.count(), 1)
            qs = manager.filter(Q(normal_two & (translated_one_en | translated_two_en)))
            self.assertEqual(qs.count(), 1)
            qs = manager.filter(shared_one & ~translated_one_en)
            self.assertEqual(qs.count(), 0)
            qs = manager.filter(shared_one & ~translated_two_en)
            self.assertEqual(qs.count(), 1)
예제 #7
0
    def test_aware_qobject_filter(self):
        from hvad.utils import get_translation_aware_manager
        manager = get_translation_aware_manager(Standard)

        normal_one = Q(normal_field=STANDARD[1].normal_field)
        normal_two = Q(normal_field=STANDARD[2].normal_field)
        shared_one = Q(normal__shared_field=NORMAL[STANDARD[1].normal].shared_field)
        translated_one_en = Q(normal__translated_field=NORMAL[STANDARD[1].normal].translated_field['en'])
        translated_two_en = Q(normal__translated_field=NORMAL[STANDARD[2].normal].translated_field['en'])

        # control group test
        with translation.override('en'):
            qs = manager.filter(shared_one)
            self.assertEqual(qs.count(), 1)
            obj = qs[0]
            self.assertEqual(obj.normal_field, STANDARD[1].normal_field)

            # basic Q object test
            qs = manager.filter(translated_one_en)
            self.assertEqual(qs.count(), 1)
            obj = qs[0]
            self.assertEqual(obj.normal_field, STANDARD[1].normal_field)

            # test various intersection combinations
            # use a spurious Q to test the logic of recursion along the way
            qs = manager.filter(Q(normal_one & shared_one & translated_one_en))
            self.assertEqual(qs.count(), 1)
            obj = qs[0]
            self.assertEqual(obj.normal_field, STANDARD[1].normal_field)

            qs = manager.filter(Q(normal_one & translated_two_en))
            self.assertEqual(qs.count(), 0)
            qs = manager.filter(Q(shared_one & translated_two_en))
            self.assertEqual(qs.count(), 0)
            qs = manager.filter(Q(translated_one_en & translated_two_en))
            self.assertEqual(qs.count(), 0)

            # test various union combinations
            qs = manager.filter(Q(normal_one | translated_one_en))
            self.assertEqual(qs.count(), 1)
            qs = manager.filter(Q(shared_one | translated_one_en))
            self.assertEqual(qs.count(), 1)

            qs = manager.filter(Q(normal_one | translated_two_en))
            self.assertEqual(qs.count(), 2)
            qs = manager.filter(Q(shared_one | translated_two_en))
            self.assertEqual(qs.count(), 2)

            qs = manager.filter(Q(translated_one_en | translated_two_en))
            self.assertEqual(qs.count(), 2)

            # misc more complex combinations
            qs = manager.filter(Q(normal_one & (translated_one_en | translated_two_en)))
            self.assertEqual(qs.count(), 1)
            qs = manager.filter(Q(normal_two & (translated_one_en | translated_two_en)))
            self.assertEqual(qs.count(), 1)
            qs = manager.filter(shared_one & ~translated_one_en)
            self.assertEqual(qs.count(), 0)
            qs = manager.filter(shared_one & ~translated_two_en)
            self.assertEqual(qs.count(), 1)
예제 #8
0
 def test_lookup_by_translated_field(self):
     en = Normal.objects.language('en').get(pk=1)
     translation_aware_manager = get_translation_aware_manager(Standard)
     with LanguageOverride('en'):
         by_translated_field = translation_aware_manager.get(normal__translated_field=en.translated_field)
         self.assertEqual(by_translated_field.normal.pk, en.pk)
         self.assertEqual(by_translated_field.normal.shared_field, en.shared_field)
         self.assertEqual(by_translated_field.normal.translated_field, en.translated_field)
         self.assertTrue(by_translated_field in en.standards.all())
예제 #9
0
    def test_in_bulk(self):
        manager = get_translation_aware_manager(Standard)
        with translation.override("en"):
            qs = manager.filter(normal__translated_field=NORMAL[STANDARD[1].normal].translated_field["en"])
            standard_dict = qs.in_bulk(self.standard_id.values())
            self.assertCountEqual(standard_dict, [self.standard_id[1]])

            with self.assertNumQueries(0):
                self.assertEqual(qs.in_bulk([]), {})
예제 #10
0
 def test_lookup_by_translated_field_using_q_objects(self):
     en = Normal.objects.language('en').get(pk=self.normal_id[1])
     translation_aware_manager = get_translation_aware_manager(Standard)
     with translation.override('en'):
         q = Q(normal__translated_field=en.translated_field)
         by_translated_field = translation_aware_manager.get(q)
         self.assertEqual(by_translated_field.normal.pk, en.pk)
         self.assertEqual(by_translated_field.normal.shared_field, en.shared_field)
         self.assertRaises(AttributeError, getattr, by_translated_field.normal, 'translated_field')
         self.assertTrue(by_translated_field in en.standards.all())
예제 #11
0
 def test_lookup_by_translated_field_using_q_objects(self):
     en = Normal.objects.language('en').get(pk=self.normal_id[1])
     translation_aware_manager = get_translation_aware_manager(Standard)
     with translation.override('en'):
         q = Q(normal__translated_field=en.translated_field)
         by_translated_field = translation_aware_manager.get(q)
         self.assertEqual(by_translated_field.normal.pk, en.pk)
         self.assertEqual(by_translated_field.normal.shared_field, en.shared_field)
         self.assertEqual(by_translated_field.normal.translated_field, en.translated_field)
         self.assertTrue(by_translated_field in en.standards.all())
예제 #12
0
    def test_in_bulk(self):
        manager = get_translation_aware_manager(Standard)
        with translation.override('en'):
            qs = manager.filter(normal__translated_field=NORMAL[
                STANDARD[1].normal].translated_field['en'])
            standard_dict = qs.in_bulk(self.standard_id.values())
            self.assertCountEqual(standard_dict, [self.standard_id[1]])

            with self.assertNumQueries(0):
                self.assertEqual(qs.in_bulk([]), {})
예제 #13
0
파일: proxy.py 프로젝트: mu-Corp/SmellGame
    def test_proxy_simple_relation(self):
        self.assertEqual(NormalProxy.objects.count(), 0)
        self.assertEqual(NormalProxy.objects.language('en').count(), 0)

        NormalProxy.objects.language('en').create(shared_field='SHARED',
                                                  translated_field='English')
        normal = NormalProxy.objects.get(shared_field='SHARED')
        SimpleRelatedProxy.objects.language('en').create(
            normal=normal, translated_field='RelatedEnglish')

        from hvad.utils import get_translation_aware_manager
        srp_manager = get_translation_aware_manager(SimpleRelatedProxy)
        qs = srp_manager.language('en').filter(
            normal__translated_field='English')
        self.assertEqual(qs.count(), 1)
        np_manager = get_translation_aware_manager(NormalProxy)
        qs = np_manager.language('en').filter(
            simplerel__translated_field='RelatedEnglish')
        self.assertEqual(qs.count(), 1)
예제 #14
0
class SitePage(LoginRequiredMixin, DetailView):
    manager = get_translation_aware_manager(Site)
    queryset = manager.language()
    template_name = 'archapp/site.html'

    def get_context_data(self, **kwargs):
        context = super(SitePage, self).get_context_data(**kwargs)
        context['sview'] = True
        context['title'] = "Site Page"
        return context
예제 #15
0
 def test_lookup_by_translated_field(self):
     en = Normal.objects.language('en').get(pk=self.normal_id[1])
     translation_aware_manager = get_translation_aware_manager(Standard)
     with translation.override('en'):
         by_translated_field = translation_aware_manager.get(
             normal__translated_field=en.translated_field
         )
         self.assertEqual(by_translated_field.normal.pk, en.pk)
         self.assertEqual(by_translated_field.normal.shared_field, en.shared_field)
         self.assertEqual(by_translated_field.normal.translated_field, en.translated_field)
         self.assertTrue(by_translated_field in en.standards.all())
예제 #16
0
 def test_values_list(self):
     manager = get_translation_aware_manager(Standard)
     with translation.override('en'):
         qs = manager.filter(
             normal__translated_field=NORMAL[STANDARD[1].normal].translated_field['en']
         ).values_list('normal_field', 'normal__shared_field', 'normal__translated_field')
         self.assertCountEqual(qs, [
             (STANDARD[1].normal_field,
              NORMAL[STANDARD[1].normal].shared_field,
              NORMAL[STANDARD[1].normal].translated_field['en'])
         ])
예제 #17
0
 def test_values_list(self):
     manager = get_translation_aware_manager(Standard)
     with translation.override('en'):
         qs = manager.filter(normal__translated_field=NORMAL[
             STANDARD[1].normal].translated_field['en']).values_list(
                 'normal_field', 'normal__shared_field',
                 'normal__translated_field')
         self.assertCountEqual(
             qs, [(STANDARD[1].normal_field,
                   NORMAL[STANDARD[1].normal].shared_field,
                   NORMAL[STANDARD[1].normal].translated_field['en'])])
예제 #18
0
 def test_lookup_by_translated_field_using_q_objects(self):
     en = Normal.objects.language('en').get(pk=1)
     translation_aware_manager = get_translation_aware_manager(Standard)
     with LanguageOverride('en'):
         q = Q(normal__translated_field=en.translated_field)
         by_translated_field = translation_aware_manager.get(q)
         self.assertEqual(by_translated_field.normal.pk, en.pk)
         self.assertEqual(by_translated_field.normal.shared_field,
                          en.shared_field)
         self.assertEqual(by_translated_field.normal.translated_field,
                          en.translated_field)
         self.assertTrue(by_translated_field in en.standards.all())
예제 #19
0
    def test_exclude_by_shared_field(self):
        manager = get_translation_aware_manager(Standard)
        with translation.override("en"):
            qs = manager.exclude(normal__shared_field=NORMAL[STANDARD[1].normal].shared_field)
            self.assertCountEqual([obj.pk for obj in qs], [self.standard_id[2]])
            self.assertCountEqual([obj.normal.pk for obj in qs], [self.normal_id[STANDARD[2].normal]])
            self.assertCountEqual([obj.normal.shared_field for obj in qs], [NORMAL[STANDARD[2].normal].shared_field])

            # Same result, other codepath
            qs = manager.exclude(normal=self.normal_id[1])
            self.assertCountEqual([obj.pk for obj in qs], [self.standard_id[2]])
            self.assertCountEqual([obj.normal.pk for obj in qs], [self.normal_id[STANDARD[2].normal]])
            self.assertCountEqual([obj.normal.shared_field for obj in qs], [NORMAL[STANDARD[2].normal].shared_field])
예제 #20
0
 def test_values(self):
     manager = get_translation_aware_manager(Standard)
     with translation.override('en'):
         qs = manager.filter(
             normal__translated_field=NORMAL[STANDARD[1].normal].translated_field['en']
         ).values('normal_field', 'normal__shared_field', 'normal__translated_field')
         self.assertCountEqual([obj['normal_field'] for obj in qs],
                               [STANDARD[1].normal_field])
         self.assertCountEqual([obj['normal__shared_field'] for obj in qs],
                               [NORMAL[STANDARD[1].normal].shared_field])
         # Following is a known caveat: translations table leaks into name
         self.assertCountEqual([obj['normal__translations__translated_field'] for obj in qs],
                               [NORMAL[STANDARD[1].normal].translated_field['en']])
예제 #21
0
 def test_values(self):
     manager = get_translation_aware_manager(Standard)
     with translation.override('en'):
         qs = manager.filter(
             normal__translated_field=NORMAL[STANDARD[1].normal].translated_field['en']
         ).values('normal_field', 'normal__shared_field', 'normal__translated_field')
         self.assertCountEqual([obj['normal_field'] for obj in qs],
                               [STANDARD[1].normal_field])
         self.assertCountEqual([obj['normal__shared_field'] for obj in qs],
                               [NORMAL[STANDARD[1].normal].shared_field])
         # Following is a known caveat: translations table leaks into name
         self.assertCountEqual([obj['normal__translations__translated_field'] for obj in qs],
                               [NORMAL[STANDARD[1].normal].translated_field['en']])
예제 #22
0
 def test_filter_by_translated_field_using_q_objects(self):
     en = Normal.objects.language('en').get(pk=self.normal_id[1])
     translation_aware_manager = get_translation_aware_manager(Standard)
     with translation.override('en'):
         q = Q(normal__translated_field=en.translated_field)
         by_translated_field = translation_aware_manager.filter(q)
         normals = [obj.normal.pk for obj in by_translated_field]
         expected = [en.pk]
         self.assertEqual(normals, expected)
         shared_fields = [obj.normal.shared_field for obj in by_translated_field]
         expected_fields = [en.shared_field]
         self.assertEqual(shared_fields, expected_fields)
         for obj in by_translated_field:
             self.assertTrue(obj in en.standards.all())
예제 #23
0
    def test_not_implemented_methods(self):
        Normal.objects.language('en').get(pk=self.normal_id[1])
        manager = get_translation_aware_manager(Standard)
        with translation.override('en'):
            self.assertRaises(NotImplementedError, manager.aggregate)
            self.assertRaises(NotImplementedError, manager.dates, 'dummy', 'dummy')
            self.assertRaises(NotImplementedError, manager.datetimes, 'dummy')
            self.assertRaises(NotImplementedError, manager.complex_filter, Q(normal_field=''))
            self.assertRaises(NotImplementedError, manager.annotate)
            self.assertRaises(NotImplementedError, manager.defer)
            self.assertRaises(NotImplementedError, manager.only)

            qs = manager.all()
            self.assertEqual(qs, qs.complex_filter({}))
예제 #24
0
    def test_not_implemented_methods(self):
        Normal.objects.language('en').get(pk=self.normal_id[1])
        manager = get_translation_aware_manager(Standard)
        with translation.override('en'):
            self.assertRaises(NotImplementedError, manager.aggregate)
            self.assertRaises(NotImplementedError, manager.dates, 'dummy', 'dummy')
            self.assertRaises(NotImplementedError, manager.datetimes, 'dummy')
            self.assertRaises(NotImplementedError, manager.complex_filter, Q(normal_field=''))
            self.assertRaises(NotImplementedError, manager.annotate)
            self.assertRaises(NotImplementedError, manager.reverse)
            self.assertRaises(NotImplementedError, manager.defer)
            self.assertRaises(NotImplementedError, manager.only)

            qs = manager.all()
            self.assertEqual(qs, qs.complex_filter({}))
예제 #25
0
    def test_latest(self):
        from datetime import datetime, timedelta

        now = datetime.now()
        for index, pk in self.standard_id.items():
            obj = Standard.objects.get(pk=pk)
            obj.date = Date.objects.language("en").create(
                shared_date=now + timedelta(days=index), translated_date=now - timedelta(days=index)
            )
            obj.save()

        manager = get_translation_aware_manager(Standard)
        with translation.override("en"):
            self.assertEqual(manager.latest("date__shared_date").pk, self.standard_id[self.standard_count])
            self.assertEqual(manager.latest("date__translated_date").pk, self.standard_id[1])
예제 #26
0
 def test_filter_by_translated_field(self):
     en = Normal.objects.language('en').get(pk=1)
     translation_aware_manager = get_translation_aware_manager(Standard)
     with LanguageOverride('en'):
         by_translated_field = translation_aware_manager.filter(normal__translated_field=en.translated_field)
         normals = [obj.normal.pk for obj in by_translated_field]
         expected = [en.pk]
         self.assertEqual(normals, expected)
         shared_fields = [obj.normal.shared_field for obj in by_translated_field]
         expected_fields = [en.shared_field]
         self.assertEqual(shared_fields, expected_fields)
         translated_fields = [obj.normal.translated_field for obj in by_translated_field]
         expected_fields = [en.translated_field]
         self.assertEqual(translated_fields, expected_fields)
         for obj in by_translated_field:
             self.assertTrue(obj in en.standards.all())
예제 #27
0
    def test_earliest(self):
        from datetime import datetime, timedelta
        now = datetime.now()
        for index, pk in self.standard_id.items():
            obj = Standard.objects.get(pk=pk)
            obj.date = Date.objects.language('en').create(
                shared_date=now + timedelta(days=index),
                translated_date=now - timedelta(days=index))
            obj.save()

        manager = get_translation_aware_manager(Standard)
        with translation.override('en'):
            self.assertEqual(
                manager.earliest('date__shared_date').pk, self.standard_id[1])
            self.assertEqual(
                manager.earliest('date__translated_date').pk,
                self.standard_id[self.standard_count])
예제 #28
0
 def test_filter_by_translated_field_using_q_objects(self):
     en = Normal.objects.language("en").get(pk=self.normal_id[1])
     translation_aware_manager = get_translation_aware_manager(Standard)
     with translation.override("en"):
         q = Q(normal__translated_field=en.translated_field)
         by_translated_field = translation_aware_manager.filter(q)
         normals = [obj.normal.pk for obj in by_translated_field]
         expected = [en.pk]
         self.assertEqual(normals, expected)
         shared_fields = [obj.normal.shared_field for obj in by_translated_field]
         expected_fields = [en.shared_field]
         self.assertEqual(shared_fields, expected_fields)
         translated_fields = [obj.normal.translated_field for obj in by_translated_field]
         expected_fields = [en.translated_field]
         self.assertEqual(translated_fields, expected_fields)
         for obj in by_translated_field:
             self.assertTrue(obj in en.standards.all())
예제 #29
0
    def test_exclude_by_shared_field(self):
        manager = get_translation_aware_manager(Standard)
        with translation.override('en'):
            qs = manager.exclude(
                normal__shared_field=NORMAL[STANDARD[1].normal].shared_field
            )
            self.assertCountEqual([obj.pk for obj in qs], [self.standard_id[2]])
            self.assertCountEqual([obj.normal.pk for obj in qs], [self.normal_id[STANDARD[2].normal]])
            self.assertCountEqual([obj.normal.shared_field for obj in qs],
                                  [NORMAL[STANDARD[2].normal].shared_field])

            # Same result, other codepath
            qs = manager.exclude(normal=self.normal_id[1])
            self.assertCountEqual([obj.pk for obj in qs], [self.standard_id[2]])
            self.assertCountEqual([obj.normal.pk for obj in qs], [self.normal_id[STANDARD[2].normal]])
            self.assertCountEqual([obj.normal.shared_field for obj in qs],
                                  [NORMAL[STANDARD[2].normal].shared_field])
예제 #30
0
    def test_not_implemented_methods(self):
        Normal.objects.language("en").get(pk=self.normal_id[1])
        manager = get_translation_aware_manager(Standard)
        with translation.override("en"):
            self.assertRaises(NotImplementedError, manager.aggregate)
            self.assertRaises(NotImplementedError, manager.dates, "dummy", "dummy")
            if django.VERSION < (1, 6):
                self.assertRaises(AttributeError, getattr, manager, "datetimes")
            else:
                self.assertRaises(NotImplementedError, manager.datetimes, "dummy")
            self.assertRaises(NotImplementedError, manager.complex_filter, Q(normal_field=""))
            self.assertRaises(NotImplementedError, manager.annotate)
            self.assertRaises(NotImplementedError, manager.reverse)
            self.assertRaises(NotImplementedError, manager.defer)
            self.assertRaises(NotImplementedError, manager.only)

            qs = manager.all()
            self.assertEqual(qs, qs.complex_filter({}))
예제 #31
0
 def test_filter_by_translated_field(self):
     en = Normal.objects.language('en').get(pk=1)
     translation_aware_manager = get_translation_aware_manager(Standard)
     with LanguageOverride('en'):
         by_translated_field = translation_aware_manager.filter(
             normal__translated_field=en.translated_field)
         normals = [obj.normal.pk for obj in by_translated_field]
         expected = [en.pk]
         self.assertEqual(normals, expected)
         shared_fields = [
             obj.normal.shared_field for obj in by_translated_field
         ]
         expected_fields = [en.shared_field]
         self.assertEqual(shared_fields, expected_fields)
         translated_fields = [
             obj.normal.translated_field for obj in by_translated_field
         ]
         expected_fields = [en.translated_field]
         self.assertEqual(translated_fields, expected_fields)
         for obj in by_translated_field:
             self.assertTrue(obj in en.standards.all())
예제 #32
0
    def test_not_implemented_methods(self):
        en = Normal.objects.language('en').get(pk=self.normal_id[1])
        manager = get_translation_aware_manager(Standard)
        with LanguageOverride('en'):
            self.assertRaises(NotImplementedError, manager.aggregate)
            self.assertRaises(NotImplementedError, manager.dates, 'dummy',
                              'dummy')
            if django.VERSION < (1, 6):
                self.assertRaises(AttributeError, getattr, manager,
                                  'datetimes')
            else:
                self.assertRaises(NotImplementedError, manager.datetimes,
                                  'dummy')
            self.assertRaises(NotImplementedError, manager.complex_filter,
                              Q(normal_field=''))
            self.assertRaises(NotImplementedError, manager.annotate)
            self.assertRaises(NotImplementedError, manager.reverse)
            self.assertRaises(NotImplementedError, manager.defer)
            self.assertRaises(NotImplementedError, manager.only)

            qs = manager.all()
            self.assertEqual(qs, qs.complex_filter({}))
예제 #33
0
class EventAdmin(TranslatableAdmin):
    list_display = ['code', 'place', 'starts_at']

    queryset = lambda self, request: get_translation_aware_manager(Event)
예제 #34
0
class PlaceAdmin(TranslatableAdmin):
    list_display = ['code', 'city']

    queryset = lambda self, request: get_translation_aware_manager(Place)
예제 #35
0
class CityAdmin(TranslatableAdmin):
    list_display = ['code', 'priority']
    order_by = ('-priority', )

    queryset = lambda self, request: get_translation_aware_manager(City)
예제 #36
0
 def queryset(self, request):
     return get_translation_aware_manager(NewsEntry)
예제 #37
0
 def queryset(self, request):
     return get_translation_aware_manager(NewsEntry)
예제 #38
0
파일: query.py 프로젝트: Madec/django-hvad
    def test_aware_qobject_filter(self):
        STANDARD={1: u'normal1', 2: u'normal2'}
        Standard.objects.create(
            normal_field=STANDARD[1],
            normal=Normal.objects.untranslated().get(
                shared_field=DOUBLE_NORMAL[1]['shared_field']
            )
        )
        Standard.objects.create(
            normal_field=STANDARD[2],
            normal=Normal.objects.untranslated().get(
                shared_field=DOUBLE_NORMAL[2]['shared_field']
            )
        )

        from hvad.utils import get_translation_aware_manager
        manager = get_translation_aware_manager(Standard)

        normal_one = Q(normal_field=STANDARD[1])
        normal_two = Q(normal_field=STANDARD[2])
        shared_one = Q(normal__shared_field=DOUBLE_NORMAL[1]['shared_field'])
        translated_one_en = Q(normal__translated_field=DOUBLE_NORMAL[1]['translated_field_en'])
        translated_two_en = Q(normal__translated_field=DOUBLE_NORMAL[2]['translated_field_en'])

        # control group test
        with LanguageOverride('en'):
            qs = manager.filter(shared_one)
            self.assertEqual(qs.count(), 1)
            obj = qs[0]
            self.assertEqual(obj.normal_field, STANDARD[1])

            # basic Q object test
            qs = manager.filter(translated_one_en)
            self.assertEqual(qs.count(), 1)
            obj = qs[0]
            self.assertEqual(obj.normal_field, STANDARD[1])

            # test various intersection combinations
            # use a spurious Q to test the logic of recursion along the way
            qs = manager.filter(Q(normal_one & shared_one & translated_one_en))
            self.assertEqual(qs.count(), 1)
            obj = qs[0]
            self.assertEqual(obj.normal_field, STANDARD[1])

            qs = manager.filter(Q(normal_one & translated_two_en))
            self.assertEqual(qs.count(), 0)
            qs = manager.filter(Q(shared_one & translated_two_en))
            self.assertEqual(qs.count(), 0)
            qs = manager.filter(Q(translated_one_en & translated_two_en))
            self.assertEqual(qs.count(), 0)

            # test various union combinations
            qs = manager.filter(Q(normal_one | translated_one_en))
            self.assertEqual(qs.count(), 2)
            qs = manager.filter(Q(shared_one | translated_one_en))
            self.assertEqual(qs.count(), 2)

            qs = manager.filter(Q(normal_one | translated_two_en))
            self.assertEqual(qs.count(), 3)
            qs = manager.filter(Q(shared_one | translated_two_en))
            self.assertEqual(qs.count(), 3)

            qs = manager.filter(Q(translated_one_en | translated_two_en))
            self.assertEqual(qs.count(), 2)

            # misc more complex combinations
            qs = manager.filter(Q(normal_one & (translated_one_en | translated_two_en)))
            self.assertEqual(qs.count(), 1)
            qs = manager.filter(Q(normal_two & (translated_one_en | translated_two_en)))
            self.assertEqual(qs.count(), 1)
            qs = manager.filter(shared_one & ~translated_one_en)
            self.assertEqual(qs.count(), 0)
            qs = manager.filter(shared_one & ~translated_two_en)
            self.assertEqual(qs.count(), 1)