Exemplo n.º 1
0
    def test_subquery_joins(self):
        # we want all the projects with the name of their first task
        import django
        if django.VERSION[0] == 1 and django.VERSION[1] < 11:
            # subqueries where only introduced in django 1.11
            return

        from django.db.models import OuterRef, Subquery
        from .models import Project, SubTask

        projects = self.projects
        account = self.account_fr
        subtasks = self.subtasks

        self.assertEqual(Project.objects.count(), 30)

        set_current_tenant(account)
        with self.assertNumQueries(1) as captured_queries:
            subtask_qs = SubTask.objects.filter(
                project=OuterRef("pk"), task__opened=True).order_by('-name')
            projects = Project.objects.all().annotate(
                first_subtask_name=Subquery(subtask_qs.values('name')[:1]))
            for p in projects:
                self.assertTrue(p.first_subtask_name is not None)

            # check that tenant in subquery
            for query in captured_queries.captured_queries:
                self.assertTrue('U0."account_id" = %d' %
                                account.id in query['sql'])

                pattern = '\(U0."task_id" = U\d."id" AND \(U0."account_id" = .?U\d."account_id"\)'
                self.assertTrue(bool(re.search(pattern, query['sql'])))
                self.assertTrue('WHERE "tests_project"."account_id" = %d' %
                                account.id in query['sql'])

        unset_current_tenant()
Exemplo n.º 2
0
    def test_delete_tenant_set(self):
        from .models import Project, Account

        unset_current_tenant()
        projects = self.projects
        accounts = Account.objects.all().order_by("id")[1:]
        self.assertEqual(Project.objects.count(), 30)

        set_current_tenant(accounts)
        with self.assertNumQueries(8) as captured_queries:
            Project.objects.all().delete()

            for query in captured_queries.captured_queries:
                if "tests_revenue" in query["sql"]:
                    self.assertTrue(
                        '"acc_id" IN (%s)' %
                        ", ".join([str(account.id) for account in accounts]))
                else:
                    self.assertTrue(
                        '"account_id" IN (%s)' %
                        ", ".join([str(account.id) for account in accounts]))

        unset_current_tenant()
        self.assertEqual(Project.objects.count(), 10)
Exemplo n.º 3
0
def gl(request):
    # url: https://mysite.ru/vkbot/

    if request.method == "POST":
        # take POST request from auto-generated variable <request.body>
        # in json format
        data = json.loads(request.body)

        if data['type'] == 'confirmation':
            # VK server request confirmation
            confirmation_token = Company.objects.get(
                vk_group_id=data['group_id']).vk_confirmation_token
            return HttpResponse(confirmation_token,
                                content_type="text/plain",
                                status=200)

        if data['type'] == 'message_new':
            # VK server send a message
            company = Company.objects.get(vk_group_id=data['group_id'])
            set_current_tenant(company)
            token = company.vk_access_token
            enqueue('create_answer', data['object'], token)

    return HttpResponse('ok', content_type="text/plain", status=200)
Exemplo n.º 4
0
    def clean_username(self):
        username = self.cleaned_data['username']

        # Don't check username uniqueness if it wasn't changed
        if 'username' not in self.changed_data:
            return username

        current_tenant = get_current_tenant()
        set_current_tenant(None)
        if get_user_model().objects.filter(username=username).exists():
            set_current_tenant(current_tenant)
            raise ValidationError(_('User with name %(username)s exists'),
                                  code='invalid',
                                  params={'username': username})

        set_current_tenant(current_tenant)
        return username
Exemplo n.º 5
0
    def set_tenant(self, create, extracted, **kwargs):
        if not create:
            return

        set_current_tenant(self)
Exemplo n.º 6
0
def receivables():

    for client in Client.objects.filter(balance__lt=0):
        set_current_tenant(client.company)
        ClientHaveNegativeBalance(client, personalized=False).send_message()
Exemplo n.º 7
0
    def set_tenant(self, serializer):
        company = self.get_company(serializer)
        if not company:
            raise ValueError('No company found for vk group')

        set_current_tenant(company)
Exemplo n.º 8
0
 def __call__(self, request):
     if request.user and not request.user.is_anonymous:
         set_current_tenant(request.user.employee.company)
     return self.get_response(request)
Exemplo n.º 9
0
 def test_uuid_create(self):
     from .models import Record
     organization = self.organization
     set_current_tenant(organization)
     record = Record.objects.create(name='record1')
     self.assertEqual(record.organization_id, organization.id)