Пример #1
0
    def test_switching_search_path(self):
        tenant1 = Tenant(domain_urls=['something.test.com'],
                         schema_name='tenant1')
        tenant1.save()

        connection.set_schema_to_public()
        tenant2 = Tenant(domain_urls=['example.com'], schema_name='tenant2')
        tenant2.save()

        # go to tenant1's path
        connection.set_tenant(tenant1)

        # add some data, 2 DummyModels for tenant1
        DummyModel(name="Schemas are").save()
        DummyModel(name="awesome!").save()

        # switch temporarily to tenant2's path
        with tenant_context(tenant2):
            # add some data, 3 DummyModels for tenant2
            DummyModel(name="Man,").save()
            DummyModel(name="testing").save()
            DummyModel(name="is great!").save()

        # we should be back to tenant1's path, test what we have
        self.assertEqual(2, DummyModel.objects.count())

        # switch back to tenant2's path
        with tenant_context(tenant2):
            self.assertEqual(3, DummyModel.objects.count())
Пример #2
0
    def test_switching_search_path(self):
        tenant1 = Tenant(domain_urls=['something.test.com'],
                         schema_name='tenant1')
        tenant1.save()

        connection.set_schema_to_public()
        tenant2 = Tenant(domain_urls=['example.com'], schema_name='tenant2')
        tenant2.save()

        # go to tenant1's path
        connection.set_tenant(tenant1)

        # add some data, 2 DummyModels for tenant1
        DummyModel(name="Schemas are").save()
        DummyModel(name="awesome!").save()

        # switch temporarily to tenant2's path
        with tenant_context(tenant2):
            # add some data, 3 DummyModels for tenant2
            DummyModel(name="Man,").save()
            DummyModel(name="testing").save()
            DummyModel(name="is great!").save()

        # we should be back to tenant1's path, test what we have
        self.assertEqual(2, DummyModel.objects.count())

        # switch back to tenant2's path
        with tenant_context(tenant2):
            self.assertEqual(3, DummyModel.objects.count())
Пример #3
0
    def test_files_are_saved_under_subdirectories_per_tenant(self):
        storage = TenantFileSystemStorage()

        connection.set_schema_to_public()
        tenant2 = utils.get_tenant_model()(schema_name='tenant2',
                                           owner=UserFactory())
        tenant2.save()

        domain2 = utils.get_tenant_domain_model()(tenant=tenant2,
                                                  domain='example.com')
        domain2.save()

        # this file should be saved on the public schema
        public_file_name = storage.save('hello_world.txt',
                                        ContentFile('Hello World'))
        public_os_path = storage.path(public_file_name)
        public_url = storage.url(public_file_name)

        # switch to tenant1
        with utils.tenant_context(self.tenant):
            t1_file_name = storage.save('hello_from_1.txt',
                                        ContentFile('Hello T1'))
            t1_os_path = storage.path(t1_file_name)
            t1_url = storage.url(t1_file_name)

        # switch to tenant2
        with utils.tenant_context(tenant2):
            t2_file_name = storage.save('hello_from_2.txt',
                                        ContentFile('Hello T2'))
            t2_os_path = storage.path(t2_file_name)
            t2_url = storage.url(t2_file_name)

        # assert the paths are correct
        self.assertTrue(
            public_os_path.endswith('apps_dir/media/public/%s' %
                                    public_file_name))
        self.assertTrue(
            t1_os_path.endswith('apps_dir/media/test/%s' % t1_file_name))
        self.assertTrue(
            t2_os_path.endswith('apps_dir/media/tenant2/%s' % t2_file_name))

        # assert urls are correct
        self.assertEqual(public_url, '/media/public/%s' % public_file_name)
        self.assertEqual(t1_url, '/media/test/%s' % t1_file_name)
        self.assertEqual(t2_url, '/media/tenant2/%s' % t2_file_name)

        # assert contents are correct
        with open(public_os_path, 'r') as fobj:
            self.assertEqual(fobj.read(), 'Hello World')

        with open(t1_os_path, 'r') as fobj:
            self.assertEqual(fobj.read(), 'Hello T1')

        with open(t2_os_path, 'r') as fobj:
            self.assertEqual(fobj.read(), 'Hello T2')
    def test_files_are_saved_under_subdirectories_per_tenant(self):
        storage = TenantFileSystemStorage()

        connection.set_schema_to_public()
        tenant1 = utils.get_tenant_model()(schema_name='tenant1')
        tenant1.save()

        domain1 = utils.get_tenant_domain_model()(tenant=tenant1, domain='something.test.com')
        domain1.save()

        connection.set_schema_to_public()
        tenant2 = utils.get_tenant_model()(schema_name='tenant2')
        tenant2.save()

        domain2 = utils.get_tenant_domain_model()(tenant=tenant2, domain='example.com')
        domain2.save()

        # this file should be saved on the public schema
        public_file_name = storage.save('hello_world.txt', ContentFile('Hello World'))
        public_os_path = storage.path(public_file_name)
        public_url = storage.url(public_file_name)

        # switch to tenant1
        with utils.tenant_context(tenant1):
            t1_file_name = storage.save('hello_from_1.txt', ContentFile('Hello T1'))
            t1_os_path = storage.path(t1_file_name)
            t1_url = storage.url(t1_file_name)

        # switch to tenant2
        with utils.tenant_context(tenant2):
            t2_file_name = storage.save('hello_from_2.txt', ContentFile('Hello T2'))
            t2_os_path = storage.path(t2_file_name)
            t2_url = storage.url(t2_file_name)

        # assert the paths are correct
        self.assertTrue(public_os_path.endswith('apps_dir/media/public/%s' % public_file_name))
        self.assertTrue(t1_os_path.endswith('apps_dir/media/tenant1/%s' % t1_file_name))
        self.assertTrue(t2_os_path.endswith('apps_dir/media/tenant2/%s' % t2_file_name))

        # assert urls are correct
        self.assertEqual(public_url, '/media/public/%s' % public_file_name)
        self.assertEqual(t1_url, '/media/tenant1/%s' % t1_file_name)
        self.assertEqual(t2_url, '/media/tenant2/%s' % t2_file_name)

        # assert contents are correct
        with open(public_os_path, 'r') as f:
            self.assertEqual(f.read(), 'Hello World')

        with open(t1_os_path, 'r') as f:
            self.assertEqual(f.read(), 'Hello T1')

        with open(t2_os_path, 'r') as f:
            self.assertEqual(f.read(), 'Hello T2')
Пример #5
0
    def setUpClass(cls):
        super(SharedAuthTest, cls).setUpClass()
        settings.SHARED_APPS = (
            'django_tenants',
            'django.contrib.auth',
            'django.contrib.contenttypes',
        )
        settings.TENANT_APPS = ('dts_test_app', )
        settings.INSTALLED_APPS = settings.SHARED_APPS + settings.TENANT_APPS
        cls.sync_shared()
        Tenant(domain_urls=['test.com'],
               schema_name=get_public_schema_name()).save()

        # Create a tenant
        cls.tenant = Tenant(domain_urls=['tenant.test.com'],
                            schema_name='tenant')
        cls.tenant.save()

        # Create some users
        with schema_context(get_public_schema_name(
        )):  # this could actually also be executed inside a tenant
            cls.user1 = User(username='******', email="*****@*****.**")
            cls.user1.save()
            cls.user2 = User(username='******', email="*****@*****.**")
            cls.user2.save()

        # Create instances on the tenant that point to the users on public
        with tenant_context(cls.tenant):
            cls.d1 = ModelWithFkToPublicUser(user=cls.user1)
            cls.d1.save()
            cls.d2 = ModelWithFkToPublicUser(user=cls.user2)
            cls.d2.save()
    def handle(self, *args, **options):
        r = requests.get(
            "https://api.eve-echoes-market.com/market-stats/stats.csv")
        content = r.content
        decoded_content = content.decode("UTF-8")
        csv_lines = csv.reader(decoded_content.splitlines(), delimiter=",")

        lookback_days = options["lookback_days"] or 7
        cutoff = timezone.now() - timezone.timedelta(days=lookback_days)
        request_sleep = options["request_sleep"] or 1
        print(
            f"Looking back {lookback_days} days to {cutoff} with per request sleep "
            f"of {request_sleep} seconds.")

        for tenant in Client.objects.all():
            with tenant_context(tenant):
                if tenant.name != "public":
                    print(f"Syncing tenant: {tenant.name}")
                    if options["truncate"]:
                        self.truncate_if_sure()
                    for line in list(csv_lines)[1:]:
                        try:
                            market_id = line[0]
                            print("------------------------------------------")
                            print(f"Syncing item id :{market_id}")
                            url = f"https://api.eve-echoes-market.com/market-stats/{market_id}"

                            time.sleep(request_sleep)
                            item_data = requests.get(url).json()
                            self.sync_item(cutoff, item_data, market_id)
                        except Exception as e:  # pylint: disable=broad-except
                            print(f"WARNING EXCEPTION = {e}")
                    # After the first tenant we have cached all market data, turn off
                    # the sleep!
                    request_sleep = 0
Пример #7
0
    def run(self):
        while not self.stop_event.is_set():
            clients = Client.objects.exclude(schema_name="public")
            for client in clients:
                with tenant_context(client):
                    try:
                        item = VideoQueue.objects.filter(
                            status=VideoQueue.QUEUED).first()
                    except OperationalError:
                        # Reset the connection to the db
                        connection.close()
                        item = None

                    if not item:
                        self.stop_event.wait(TIMEOUT)
                        continue

                    LOGGER.info("Working on %s", item.video.guid)

                    try:
                        self.convert_video(item)
                    except Exception as err:
                        LOGGER.error(str(err))
                        if self._emailuser:
                            emailUser(item.video, err)
                        mail_admins("Video Processing Failure", str(err))
                        item.status = VideoQueue.ERROR
                        item.save()
Пример #8
0
    def setUp(self):
        super().setUp()

        settings.SHARED_APPS = ('django_tenants',
                                'customers',
                                'django.contrib.auth',
                                'django.contrib.contenttypes', )
        settings.TENANT_APPS = ('dts_test_app', )
        settings.INSTALLED_APPS = settings.SHARED_APPS + settings.TENANT_APPS
        self.sync_shared()
        self.public_tenant = get_tenant_model()(schema_name=get_public_schema_name())
        self.public_tenant.save()
        self.public_domain = get_tenant_domain_model()(tenant=self.public_tenant, domain='test.com')
        self.public_domain.save()

        # Create a tenant
        self.tenant = get_tenant_model()(schema_name='tenant')
        self.tenant.save()
        self.domain = get_tenant_domain_model()(tenant=self.tenant, domain='tenant.test.com')
        self.domain.save()

        # Create some users
        with schema_context(get_public_schema_name()):  # this could actually also be executed inside a tenant
            self.user1 = User(username='******', email="*****@*****.**")
            self.user1.save()
            self.user2 = User(username='******', email="*****@*****.**")
            self.user2.save()

        # Create instances on the tenant that point to the users on public
        with tenant_context(self.tenant):
            self.d1 = ModelWithFkToPublicUser(user=self.user1)
            self.d1.save()
            self.d2 = ModelWithFkToPublicUser(user=self.user2)
            self.d2.save()
Пример #9
0
    def setUp(self):
        super(SharedAuthTest, self).setUp()

        settings.SHARED_APPS = ('django_tenants',
                                'customers',
                                'django.contrib.auth',
                                'django.contrib.contenttypes', )
        settings.TENANT_APPS = ('dts_test_app', )
        settings.INSTALLED_APPS = settings.SHARED_APPS + settings.TENANT_APPS
        self.sync_shared()
        self.public_tenant = get_tenant_model()(schema_name=get_public_schema_name())
        self.public_tenant.save()
        self.public_domain = get_tenant_domain_model()(tenant=self.public_tenant, domain='test.com')
        self.public_domain.save()

        # Create a tenant
        self.tenant = get_tenant_model()(schema_name='tenant')
        self.tenant.save()
        self.domain = get_tenant_domain_model()(tenant=self.tenant, domain='tenant.test.com')
        self.domain.save()

        # Create some users
        with schema_context(get_public_schema_name()):  # this could actually also be executed inside a tenant
            self.user1 = User(username='******', email="*****@*****.**")
            self.user1.save()
            self.user2 = User(username='******', email="*****@*****.**")
            self.user2.save()

        # Create instances on the tenant that point to the users on public
        with tenant_context(self.tenant):
            self.d1 = ModelWithFkToPublicUser(user=self.user1)
            self.d1.save()
            self.d2 = ModelWithFkToPublicUser(user=self.user2)
            self.d2.save()
Пример #10
0
    def setUpClass(cls):
        super(SharedAuthTest, cls).setUpClass()
        settings.SHARED_APPS = ('django_tenants',
                                'django.contrib.auth',
                                'django.contrib.contenttypes', )
        settings.TENANT_APPS = ('dts_test_app', )
        settings.INSTALLED_APPS = settings.SHARED_APPS + settings.TENANT_APPS
        cls.sync_shared()
        Tenant(domain_urls=['test.com'], schema_name=get_public_schema_name()).save()

        # Create a tenant
        cls.tenant = Tenant(domain_urls=['tenant.test.com'], schema_name='tenant')
        cls.tenant.save()

        # Create some users
        with schema_context(get_public_schema_name()):  # this could actually also be executed inside a tenant
            cls.user1 = User(username='******', email="*****@*****.**")
            cls.user1.save()
            cls.user2 = User(username='******', email="*****@*****.**")
            cls.user2.save()

        # Create instances on the tenant that point to the users on public
        with tenant_context(cls.tenant):
            cls.d1 = ModelWithFkToPublicUser(user=cls.user1)
            cls.d1.save()
            cls.d2 = ModelWithFkToPublicUser(user=cls.user2)
            cls.d2.save()
    def test_type2_with_type2(self):
        """
        Writing to type2 model should be ok
        """

        with tenant_context(self.tenant2):
            TypeTwoOnly(name='hello')
Пример #12
0
def refresh_from_discord_all():
    output = ""
    for tenant in Client.objects.all():
        if tenant.name != "public":
            with tenant_context(tenant):
                output = output + f"\n====== CLIENT {tenant.name} =========\n"
                output = output + refresh_from_discord()
    return output
Пример #13
0
 def do(self):
     cron_header_line(self.code)
     now = timezone.now()
     for tenant in Client.objects.all():
         with tenant_context(tenant):
             if tenant.name != "public":
                 print(f"Repeating for tenant {tenant.name}")
                 _run_for_tenant(now)
Пример #14
0
 def test_direct_relation_to_public(self):
     """
     Tests that a forward relationship through a foreign key to public from a model inside TENANT_APPS works.
     """
     with tenant_context(self.tenant):
         self.assertEqual(User.objects.get(pk=self.user1.id),
                          ModelWithFkToPublicUser.objects.get(pk=self.d1.id).user)
         self.assertEqual(User.objects.get(pk=self.user2.id),
                          ModelWithFkToPublicUser.objects.get(pk=self.d2.id).user)
Пример #15
0
 def test_direct_relation_to_public(self):
     """
     Tests that a forward relationship through a foreign key to public from a model inside TENANT_APPS works.
     """
     with tenant_context(self.tenant):
         self.assertEqual(User.objects.get(pk=self.user1.id),
                          ModelWithFkToPublicUser.objects.get(pk=self.d1.id).user)
         self.assertEqual(User.objects.get(pk=self.user2.id),
                          ModelWithFkToPublicUser.objects.get(pk=self.d2.id).user)
Пример #16
0
    def form_valid(self, form):
        instance = form.instance
        request = get_object_or_404(PizzeriaRequest, pk=self.kwargs['pk'])
        instance.request = request
        instance.schema_name = instance.name.lower().replace(" ", '_')
        instance.paid_until = datetime.datetime.now() + datetime.timedelta(
            days=30)
        instance.date_expired_paid = datetime.date.today(
        ) + datetime.timedelta(days=30)
        instance.phones = request.phone
        instance.email = request.email
        instance.address = request.address
        instance.plan = request.plan
        instance = form.save()
        domain = Domain()
        domain.domain = form.cleaned_data['domain'].lower().replace(
            " ", "_") + "." + self.request.tenant.domains.first().domain
        domain.is_primary = True
        domain.tenant = instance
        domain.save()
        with tenant_context(instance):
            password = "".join(
                [random.choice(string.ascii_lowercase[:26]) for i in range(8)])
            # password = '******'
            user = UserProfile.objects.create(
                first_name='admin',
                last_name="admin",
                email=request.email,
            )
            groups = {
                'admin': [],
                'vendedor': [],
                'client': [],
            }
            for key, value in groups.items():
                created, group = Group.objects.get_or_create(name=key)
            group = Group.objects.get(name="admin")
            user.is_staff = True
            user.set_password(password)
            user.is_superuser = True
            user.save()
            user.groups.add(group)

        send_mail(
            subject="Bienvenido a SuperPizzas",
            message=
            "Su solicitud de franquicia ha sido creado con exito. utilice el correo "
            + request.email + " y"
            " la contraseña " + password + " para loguearse. url: " +
            domain.domain,
            from_email="*****@*****.**",
            recipient_list=[request.email])

        request.is_active = False
        request.save()
        return super(PizzeriaCreateView, self).form_valid(form)
Пример #17
0
 def test_reverse_relation_to_public(self):
     """
     Tests that a reverse relationship through a foreign keys to public from a model inside TENANT_APPS works.
     """
     with tenant_context(self.tenant):
         users = User.objects.all().select_related().order_by('id')
         self.assertEqual(ModelWithFkToPublicUser.objects.get(pk=self.d1.id),
                          users[0].modelwithfktopublicuser_set.all()[:1].get())
         self.assertEqual(ModelWithFkToPublicUser.objects.get(pk=self.d2.id),
                          users[1].modelwithfktopublicuser_set.all()[:1].get())
Пример #18
0
 def test_reverse_relation_to_public(self):
     """
     Tests that a reverse relationship through a foreign keys to public from a model inside TENANT_APPS works.
     """
     with tenant_context(self.tenant):
         users = User.objects.all().select_related().order_by('id')
         self.assertEqual(ModelWithFkToPublicUser.objects.get(pk=self.d1.id),
                          users[0].modelwithfktopublicuser_set.all()[:1].get())
         self.assertEqual(ModelWithFkToPublicUser.objects.get(pk=self.d2.id),
                          users[1].modelwithfktopublicuser_set.all()[:1].get())
Пример #19
0
    def test_switching_search_path(self):
        tenant1 = get_tenant_model()(schema_name='tenant1')
        tenant1.save()

        domain1 = get_tenant_domain_model()(tenant=tenant1,
                                            domain='something.test.com')
        domain1.save()

        connection.set_schema_to_public()

        tenant2 = get_tenant_model()(schema_name='tenant2')
        tenant2.save()

        domain2 = get_tenant_domain_model()(tenant=tenant2,
                                            domain='example.com')
        domain2.save()

        # go to tenant1's path
        connection.set_tenant(tenant1)

        # add some data, 2 DummyModels for tenant1
        DummyModel(name="Schemas are").save()
        DummyModel(name="awesome!").save()

        # switch temporarily to tenant2's path
        with self.assertNumQueries(6):
            with tenant_context(tenant2):
                # add some data, 3 DummyModels for tenant2
                DummyModel(name="Man,").save()
                DummyModel(name="testing").save()
                DummyModel(name="is great!").save()

        # we should be back to tenant1's path, test what we have
        with self.assertNumQueries(2):
            self.assertEqual(2, DummyModel.objects.count())

        # switch back to tenant2's path
        with self.assertNumQueries(2):
            with tenant_context(tenant2):
                self.assertEqual(3, DummyModel.objects.count())

        self.created = [domain2, domain1, tenant2, tenant1]
Пример #20
0
 def do(self):
     cron_header_line(self.code)
     now = timezone.now()
     now_minus_98_hours = now - timezone.timedelta(hours=98)
     for tenant in Client.objects.all():
         if tenant.name != "public":
             with tenant_context(tenant):
                 stats = ShipOrder.objects.filter(
                     contract_made=False,
                     created_at__lt=now_minus_98_hours).delete()
                 print(f"Deleted {stats} old contracts")
Пример #21
0
    def test_switching_tenant_without_previous_tenant(self):
        tenant = Tenant(domain_urls=['something.test.com'], schema_name='test')
        tenant.save()

        connection.tenant = None
        with tenant_context(tenant):
            DummyModel(name="No exception please").save()

        connection.tenant = None
        with schema_context(tenant.schema_name):
            DummyModel(name="Survived it!").save()
Пример #22
0
    def test_switching_tenant_without_previous_tenant(self):
        tenant = Tenant(domain_urls=['something.test.com'], schema_name='test')
        tenant.save()

        connection.tenant = None
        with tenant_context(tenant):
            DummyModel(name="No exception please").save()

        connection.tenant = None
        with schema_context(tenant.schema_name):
            DummyModel(name="Survived it!").save()
Пример #23
0
    def tearDown(self):
        with schema_context('public'):
            # reset tenant access after each test
            self.private_tenant.authorized_users.clear()
            self.tenant.authorized_users.clear()

        # delete products b/c we use the same payload for multiple tests
        # and sometimes there can be duplicates from a previous test
        for tenant in [self.public_tenant, self.private_tenant, self.tenant]:
            with tenant_context(tenant):
                Product.objects.all().delete()
                BugSystem.objects.all().delete()
Пример #24
0
def create_product_from_repository(data):
    tenant, installation = find_tenant(data)

    # can't handle requests from unconfigured installation
    if not tenant:
        return

    with tenant_context(tenant):
        rpc = github_rpc_from_inst(installation)
        repo_object = rpc.get_repo(data.payload['repository']['full_name'])

        _product_from_repo(repo_object)
        _bugtracker_from_repo(repo_object)
    def handle(self, *args, **options):
        franchises = SharedFranchise.objects.filter(~Q(schema_name="public")
                                                    & ~Q(schema_name="test"))

        # Iterate through all the franchise schemas and perform our operations
        # limited to the specific operation.
        for franchise in franchises.all():
            with tenant_context(franchise):
                self.run_update_expired_associates_for_franchise(franchise)

        self.stdout.write(
            self.style.SUCCESS(
                _('Successfully updated all ongoing job orders.')))
Пример #26
0
def post(request):
    data = json.loads(request.body)["body"]
    name = data["domain"].replace(".", "_").replace("-", "_")
    quota = data["quota"]
    created = False

    client = Client.objects.filter(schema_name=name)
    if client:
        client = client[0]
    else:
        client = Client(schema_name=name, name=name, space_quota=quota)
        client.save()
        created = True

    if not client.domains.count():
        domain = Domain()
        domain.domain = data["domain"]
        domain.tenant = client
        domain.is_primary = True
        domain.save()

    with tenant_context(client):
        if created:
            # Create superuser
            call_command("createsuperuser",
                         username="******",
                         email="admin@{}".format(data["domain"]),
                         interactive=False)

            # Run Fixtures
            call_command("loaddata", "initial_data.json", app="frog")

        item = client.toJson()
        item['image'] = None
        item['image_count'] = Image.objects.all().count()
        item['video_count'] = Video.objects.all().count()
        item['site_config'] = None
        item['managers'] = []
        item['user_count'] = User.objects.all().count()
        item['history'] = []
        item['space_used'] = math.ceil(
            getFolderSize(getRoot()) / float(1 << 27))
        item['tenant_created'] = created

        staticroot = pathlib.Path(getRoot())
        if not staticroot.exists():
            staticroot.mkdir(parents=True)

        res = item

    return JsonResponse(res)
Пример #27
0
def create_admin_tenant(tenant, user_id, password, url):
    """
    Function that creates a superuser in the tenant schema.
    Receive the data of a registered user in the public schema
    and that belongs to a registry of the tenant.
    """
    user = get_user_model().objects.get(pk=user_id)
    tenant = Tenant(schema_name=tenant)

    # Send email of welcome
    send_mailgun("Bienvenido a SCR", user.email, url)

    with tenant_context(tenant):
        get_user_model().objects.create_superuser(email=user.email, password=password, first_name=user.first_name, last_name=user.last_name)
Пример #28
0
    def test_switching_tenant_without_previous_tenant(self):
        tenant = get_tenant_model()(schema_name='test')
        tenant.save()

        domain = get_tenant_domain_model()(tenant=tenant, domain='something.test.com')
        domain.save()

        connection.tenant = None
        with tenant_context(tenant):
            DummyModel(name="No exception please").save()

        connection.tenant = None
        with schema_context(tenant.schema_name):
            DummyModel(name="Survived it!").save()
    def handle(self, *args, **options):
        # Get the user inputs.
        email = options['email'][0]

        # Defensive Code: Prevent continuing if the email already exists.
        if not SharedUser.objects.filter(email=email).exists():
            raise CommandError(
                _('Email does not exists, please pick another email.'))

        # Create the user.
        user = SharedUser.objects.get(email=email)
        self.stdout.write(self.style.SUCCESS(_('Retrieved "User" object.')))
        self.stdout.write(
            self.style.SUCCESS("-------------------------------"))
        self.stdout.write(self.style.SUCCESS("Name: %s" %
                                             user.get_full_name()))
        self.stdout.write(self.style.SUCCESS("ID: %s" % user.id))
        self.stdout.write(
            self.style.SUCCESS("-------------------------------"))

        for franchise in SharedFranchise.objects.filter(~Q(
                schema_name="public")).iterator():
            with tenant_context(franchise):
                associate = Associate.objects.filter(
                    Q(owner=user) | Q(email=email)).first()
                staff = Staff.objects.filter(Q(owner=user)
                                             | Q(email=email)).first()
                customer = Customer.objects.filter(
                    Q(owner=user) | Q(email=email)).first()
                if associate or staff or customer:
                    self.stdout.write(
                        self.style.SUCCESS("Found in Tenant: %s" %
                                           franchise.schema_name))
                    if associate:
                        self.stdout.write(
                            self.style.SUCCESS("\tAssociate ID: %s" %
                                               associate.id))
                    if staff:
                        self.stdout.write(
                            self.style.SUCCESS("\tStaff ID: %s" % staff.id))
                    if customer:
                        self.stdout.write(
                            self.style.SUCCESS("\tCustomer ID: %s" %
                                               customer.id))
        self.stdout.write(
            self.style.SUCCESS("-------------------------------"))

        # For debugging purposes.
        self.stdout.write(
            self.style.SUCCESS(_('Successfully retrieved account.')))
Пример #30
0
def create_tenant(form_data, request):
    owner = request.user
    name = form_data['name']
    schema_name = form_data['schema_name']
    on_trial = form_data['on_trial']
    paid_until = form_data['paid_until']

    with schema_context('public'):
        tenant = Tenant.objects.create(
            name=name,
            schema_name=schema_name,
            paid_until=paid_until,
            on_trial=on_trial,
            owner=owner,
            organization=form_data['organization'],
        )
        domain = Domain.objects.create(
            domain=tenant_domain(schema_name),
            is_primary=True,
            tenant=tenant,
        )

        # work-around: for some reason the ContentType for tenant-user
        # relationship model doesn't get automatically created and if
        # the owner tries to add other authorized users via admin the
        # action will fail b/c ModelAdmin.log_addition tries to add a
        # a logging record linking to this content type and fails !
        ContentType.objects.get_for_model(tenant.authorized_users.through)

        # make the owner the first authorized user
        # otherwise they can't login
        tenant.authorized_users.add(owner)

    with tenant_context(tenant):
        # this is used to build full URLs for things like emails
        site = Site.objects.get(pk=settings.SITE_ID)
        site.domain = domain.domain
        site.name = domain.domain
        site.save()

    mailto(
        template_name='tcms_tenants/email/new.txt',
        recipients=[owner.email],
        subject=str(_('New Kiwi TCMS tenant created')),
        context={
            'tenant_url': tenant_url(request, tenant.schema_name),
        }
    )
    return tenant
Пример #31
0
    def test_switching_tenant_without_previous_tenant(self):
        tenant = get_tenant_model()(schema_name='test')
        tenant.save()

        domain = get_tenant_domain_model()(tenant=tenant,
                                           domain='something.test.com')
        domain.save()

        connection.tenant = None
        with tenant_context(tenant):
            DummyModel(name="No exception please").save()

        connection.tenant = None
        with schema_context(tenant.schema_name):
            DummyModel(name="Survived it!").save()
Пример #32
0
    def test_switching_search_path_limited_calls(self):
        tenant1 = get_tenant_model()(schema_name='tenant1')
        tenant1.save()

        domain1 = get_tenant_domain_model()(tenant=tenant1,
                                            domain='something.test.com')
        domain1.save()

        connection.set_schema_to_public()

        tenant2 = get_tenant_model()(schema_name='tenant2')
        tenant2.save()

        domain2 = get_tenant_domain_model()(tenant=tenant2,
                                            domain='example.com')
        domain2.save()

        # set path is not executed when setting tenant so 0 queries expected
        with self.assertNumQueries(0):
            connection.set_tenant(tenant1)

        # switch temporarily to tenant2's path
        # 1 query to set search path + 3 to save data
        # with self.assertNumQueries(4):
        # This was always returning 6, do not why
        with self.assertNumQueries(6):
            with tenant_context(tenant2):
                DummyModel(name="Man,").save()
                DummyModel(name="testing").save()
                DummyModel(name="is great!").save()

        # 0 queries as search path not set here
        with self.assertNumQueries(0):
            connection.set_tenant(tenant1)

        # 1 set search path + 1 count
        with self.assertNumQueries(2):
            self.assertEqual(0, DummyModel.objects.count())

        # 0 queries as search path not set here
        with self.assertNumQueries(0):
            connection.set_tenant(tenant2)

        # 1 set search path + 1 count
        with self.assertNumQueries(2):
            self.assertEqual(3, DummyModel.objects.count())

        self.created = [domain2, domain1, tenant2, tenant1]
Пример #33
0
    def setUpClass(cls):
        super().setUpClass()

        # authorize tenant owner
        cls.tenant.authorized_users.add(cls.tenant.owner)

        cls.tester = UserFactory()
        cls.tester.set_password('password')
        cls.tester.save()

        # authorize this user
        cls.tenant.authorized_users.add(cls.tester)

        # initial data
        with tenant_context(cls.tenant):
            cls.test_plan_by_owner = TestPlanFactory(author=cls.tenant.owner)
Пример #34
0
def find_tenants_for_user(request, excluding=None):
    owners_orgs = []
    member_orgs = []
    if excluding:
        qs = Client.objects.exclude(id=excluding.id).all()
    else:
        qs = Client.objects.all()
    for tenant in qs:
        if tenant.name != "public" and request.user.is_authenticated:
            with tenant_context(tenant):
                if tenant.owner == request.user:
                    owners_orgs.append(tenant)
                elif GooseUser.objects.filter(
                        site_user=request.user).count() > 0:
                    member_orgs.append(tenant)
    return owners_orgs, member_orgs
Пример #35
0
 def do(self):
     cron_header_line(self.code)
     r = requests.get(
         "https://api.eve-echoes-market.com/market-stats/stats.csv")
     content = r.content
     decoded_content = content.decode("UTF-8")
     csv_lines = csv.reader(decoded_content.splitlines(), delimiter=",")
     lines = list(csv_lines)[1:]
     print(
         f"Found {len(lines)} lines of market data from https://api.eve-echoes-market.com/market-stats/stats.csv"
     )
     for tenant in Client.objects.all():
         with tenant_context(tenant):
             if tenant.name != "public":
                 print(f"Inserting latest market data for {tenant.name}")
                 for line in lines:
                     market_id = line[0]
                     datetime_str = line[2]
                     time = parse_datetime(datetime_str)
                     if time is None:
                         raise Exception(
                             f"Invalid datetime recieved from stats.csv: {time}"
                         )
                     try:
                         item = Item.objects.get(
                             eve_echoes_market_id=market_id)
                         lowest_sell = decimal_or_none(line[5])
                         for ee_pl in DataSet.objects.filter(
                                 api_type="eve_echoes_market"):
                             ItemMarketDataEvent.objects.update_or_create(
                                 price_list=ee_pl,
                                 item=item,
                                 time=time,
                                 defaults={
                                     "sell": decimal_or_none(line[3]),
                                     "buy": decimal_or_none(line[4]),
                                     "lowest_sell": lowest_sell,
                                     "highest_buy":
                                     decimal_or_none(line[6]),
                                 },
                             )
                         item.cached_lowest_sell = lowest_sell
                         item.save()
                     except Item.DoesNotExist:
                         print(
                             f"WARNING: Market Data Found for Item not in {settings.SITE_NAME}- id:{market_id}"
                         )
Пример #36
0
def create_product_from_installation_repositories(data):
    """
        Warning: not handling `repositories_removed` for now b/c
        there could be linked data which we don't want to destroy!
    """
    tenant, installation = find_tenant(data)

    # can't handle requests from unconfigured installation
    if not tenant:
        return

    with tenant_context(tenant):
        rpc = github_rpc_from_inst(installation)
        for repo in data.payload['repositories_added']:
            repo_object = rpc.get_repo(repo['full_name'])
            _product_from_repo(repo_object)
            _bugtracker_from_repo(repo_object)
 def do(self):
     cron_header_line(self.code)
     if (settings.SHIP_PRICE_GOOGLE_SHEET_ID
             and settings.SHIP_PRICE_GOOGLE_SHEET_CELL_RANGE):
         for tenant in Client.objects.all():
             if tenant.name != "public":
                 with tenant_context(tenant):
                     import_ship_prices()
                     for price_list in DataSet.objects.filter(
                             api_type="google_sheet").all():
                         try:
                             import_price_list(price_list)
                         except Exception as e:  # pylint: disable=broad-except
                             print(
                                 f"ERROR IMPORTING PRICES FOR {price_list} = {e}"
                             )
     else:
         print("Not looking up ship prices as no spreadsheet and range is "
               "configured.")
Пример #38
0
def get(request):
    res = {"items": []}
    for client in Client.objects.exclude(schema_name="public"):
        with tenant_context(client):
            image = Image.objects.first()
            siteconfig = SiteConfig.objects.first()

            numdays = 7
            today = datetime.datetime.today()
            last_30_days = today - datetime.timedelta(numdays)
            week_history = Image.objects.filter(
                created__gte=last_30_days).extra(select={
                    'day': 'date(created)'
                }).values('day').annotate(dcount=Count('created'))
            history = [
                today - datetime.timedelta(days=_) for _ in range(numdays)
            ]
            hist = [0 for _ in range(numdays)]
            for i, _ in enumerate(history):
                for obj in week_history:
                    if obj['day'].day == _.day:
                        hist[i] = obj['dcount']

            item = client.toJson()
            item['image'] = image.json() if image else None
            item['image_count'] = Image.objects.all().count()
            item['video_count'] = Video.objects.all().count()
            item['site_config'] = siteconfig.json() if siteconfig else None
            try:
                item['managers'] = json.loads(
                    serializers.serialize(
                        "json",
                        Group.objects.get(name="managers").user_set.all()))
            except:
                item['managers'] = []
            item['user_count'] = User.objects.all().count()
            item['history'] = hist
            item['space_used'] = math.ceil(
                getFolderSize(getRoot()) / float(1 << 27))

            res["items"].append(item)

    return JsonResponse(res)