djaodjin / djaodjin-saas

Django application for software-as-service and subscription businesses
Other
564 stars 124 forks source link

Organization matching query does not exist #213

Closed MAkcanca closed 5 years ago

MAkcanca commented 5 years ago

Hey!

I'm trying to install the djaodjin-saas but had no luck so far. I used pip install to install the package and added the url. But whenever I visit, for example xxx:/pricing it gives me this error: Organization matching query does not exist I loaded the test data fixture with loaddata. Database: PostgreSQL I'm using django-cookiecutter as a boilerplate.

Traceback

Environment:

Request Method: GET
Request URL: http://0.0.0.0:8000/pricing/

Django Version: 2.2.4
Python Version: 3.6.8
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'crispy_forms',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'rest_framework',
 'django_celery_beat',
 'testproject_frontend.users.apps.UsersConfig',
 'saas',
 'debug_toolbar',
 'django_extensions']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware']

Traceback:

File "/home/mpj/testproject_frontend/venv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/mpj/testproject_frontend/venv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/home/mpj/testproject_frontend/venv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/lib/python3.6/contextlib.py" in inner
  52.                 return func(*args, **kwds)

File "/home/mpj/testproject_frontend/venv/lib/python3.6/site-packages/django/views/generic/base.py" in view
  71.             return self.dispatch(request, *args, **kwargs)

File "/home/mpj/testproject_frontend/venv/lib/python3.6/site-packages/django/views/generic/base.py" in dispatch
  97.         return handler(request, *args, **kwargs)

File "/home/mpj/testproject_frontend/venv/lib/python3.6/site-packages/django/views/generic/list.py" in get
  142.         self.object_list = self.get_queryset()

File "/home/mpj/testproject_frontend/venv/lib/python3.6/site-packages/saas/views/plans.py" in get_queryset
  108.         queryset = Plan.objects.filter(organization=self.provider,

File "/home/mpj/testproject_frontend/venv/lib/python3.6/site-packages/saas/mixins.py" in provider
  586.             self._provider = self.get_organization()

File "/home/mpj/testproject_frontend/venv/lib/python3.6/site-packages/saas/mixins.py" in get_organization
  577.         return get_broker()

File "/home/mpj/testproject_frontend/venv/lib/python3.6/site-packages/saas/models.py" in get_broker
  4000.     return Organization.objects.get(slug=settings.BROKER_CALLABLE)

File "/home/mpj/testproject_frontend/venv/lib/python3.6/site-packages/django/db/models/manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/home/mpj/testproject_frontend/venv/lib/python3.6/site-packages/django/db/models/query.py" in get
  408.                 self.model._meta.object_name

Exception Type: DoesNotExist at /pricing/
Exception Value: Organization matching query does not exist.

Thanks

jpoechill commented 5 years ago

Hey MAk –

Were you able to get any up and showing at all? I previously wrote a short blog post on how I was able to get most of it set up myself.

Link: https://medium.com/@jpoechill/getting-setup-the-djaodjin-dev-environment-3ce20f982014

––

To get the pricing page to show with correct detail, it's noted that you have to turn on the debug mode, and load the sample data also.

$ diff -u <em>installTop</em>/etc/djaoapp/site.conf
    -DEBUG = False
    +DEBUG = True

    # Create the tests databases and load test datasets.
    $ make initdb

    # To generate some sample data, disable emailing of receipts and run:
    $ python manage.py load_test_transactions
MAkcanca commented 5 years ago

I have debug mode turned on, that's how I got the traceback. Also like I said, I loaded the test data.

But I will try it again and update this post.

Update: Debug is turned on, I used the loaddata stated in README.md, still the same.

I'm using PostgreSQL and Django-allauth for user management. Can you try to reproduce it with vanilla django-cookiecutter ? Thanks

smirolo commented 5 years ago

This error shows because the broker Organization is not present. You will need to populate the database with some bootstrap records. For example, to get quickly started you can load the testsite fixtures. Assuming you have a auth.User record with pk=1, the minimum fixtures required look something like:

{
    "fields": {
      "slug": "stripe",
      "full_name": "Stripe",
      "created_at": "2012-08-14T14:16:55-09:00",
      "email": "stripe@localhost.localdomain",
      "phone": "555-555-5555",
      "street_address": "185 Berry St #550",
      "locality":  "San Francisco",
      "region": "CA",
      "postal_code": "94107",
      "country": "US",
      "processor": 1,
      "is_provider": 1,
      "is_active": 1
    },
    "model": "saas.Organization", "pk": 1
},
{
    "fields": {
      "slug": "terms-of-use",
      "title": "Terms Of Use",
      "modified": "2012-08-14T00:00:00-09:00"
    },
    "model": "saas.agreement", "pk": 1
},
{
    "fields": {
      "slug": "djaoapp",
      "full_name": "DjaoApp",
      "created_at": "2015-06-01T00:00:00+00:00",
      "email": "djaoapp@localhost.localdomain",
      "phone": "555-555-5555",
      "street_address": "",
      "locality":  "",
      "region": "CA",
      "postal_code": "94133",
      "country": "US",
      "processor": 1,
      "is_provider": 1,
      "is_active": 1
    },
    "model": "saas.Organization", "pk": 2
},
{
    "fields": {
      "created_at": "2015-06-01T00:00:00+00:00",
      "slug": "manager",
      "title": "Profile Manager"
    },
    "model": "saas.RoleDescription", "pk": 1
},
{
    "fields": {
      "created_at": "2012-10-01T00:00:00-09:00",
      "role_description": 1,
      "organization": 2,
      "user": 1
    },
    "model": "saas.Role", "pk": 1
}]

Note that if you are looking to build a full-fledge SaaS product and are starting from django-cookiecutter, you might also want to start from djaoapp.

MAkcanca commented 5 years ago

I will be checking djaoapp but the problem persists. I already used the fixture and loaded test data, I think you misread my question since I stated that many times.

I will reset the database and update this post.

Update: I used the exact test data you provided with a clean database. Problem seems to persist.

DoesNotExist at /pricing/
Organization matching query does not exist.
smirolo commented 5 years ago

Apologies. I tried to replicate what you did, installing djaodjin-saas into django-cookiecutter (line-by-line commands following).

The issue is that the broker Organization installed by the djaodjin-saas/testsite fixtures is called "cowork", while the default value in saas/settings.py is derived from BASE_DIR (which does not exist in django-cookiecutter settings.py) or the default value "broker".

$ cat /lib/python3.6/site-packages/settings.py:
        'GET_INSTANCE': os.path.basename(
            getattr(settings, 'BASE_DIR', "broker")),

The djaodjin-saas/testsite/settings.py explicitly set the broker slug to the value in the fixtures.

SAAS = {
    'BROKER': {
       'GET_INSTANCE': 'cowork',
    }
}

When you are looking to go to production, you will want the slug of the broker Organization defined in the database to match SAAS['BROKER']['GET_INSTANCE'] defined in config/settings/base.py.

Please let us know if this works for you or if you are stumbling on other issues. Thank you.

Installing saas into django-cookiecutter

$ virtualenv-3.6 cookiecutter
$ source cookiecutter/bin/activate
$ pip install "cookiecutter>=1.4.0"
$ cookiecutter https://github.com/pydanny/cookiecutter-django
$ cd mytestsite
$ pip install -r requirements/local.txt
$ pip install djaodjin-saas
$ diff -u prev config/urls.py
from django.views import defaults as default_views
+ from saas.views import UserRedirectView

urlpatterns = [
+    path("api/", include('saas.urls.api')),
+    path("", include('saas.urls.noauth')),
+    path("", include('saas.urls.request')),
+    path("", include('saas.urls.redirects')),
+    path("", include('saas.urls.broker')),
+    path("", include('saas.urls.provider')),
+    path("", include('saas.urls.subscriber')),
+    path("", include('saas.urls.users')),
+    path("", UserRedirectView.as_view(), name='accounts_profile'),

$ diff -u prev config/settings/base.py
DATABASES = {
-    "default": env.db("DATABASE_URL", default="postgres:///mytestsite")
+   # Didn't want to complicate things by installing postgres.
+    "default": env.db("DATABASE_URL", default="sqlite:///db.sqlite")
}

THIRD_PARTY_APPS = [
    "crispy_forms",
    "allauth",
    "allauth.account",
    "allauth.socialaccount",
    "rest_framework",
+   "saas",
]

TEMPLATES = [
    {
        # https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATES-BACKEND
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        # https://docs.djangoproject.com/en/dev/ref/settings/#template-dirs
        "DIRS": [str(APPS_DIR.path("templates"))],
        "OPTIONS": {
+           "builtins": [
+               "saas.templatetags.saas_tags",
+           ],

Here we will download the sample test data to get started quickly. Unfortunately django-cookiecutter is not using the auth.User model but a users.User model instead, so we first need to do a string replace in the fixtures file.

$ cd ..
$ git clone https://github.com/djaodjin/djaodjin-saas
$ diff -u prev djaodjin-saas/testsite/fixtures/test_data.json
...
-    "model": "auth.User", "pk": 2
+    "model": "users.User", "pk": 2
...
$ cd mytestsite

We also configure the project settings.py to use the correct Organization as a broker.

$ diff -u prev config/settings/base.py
+SAAS = {
+  'BROKER': {
+      'GET_INSTANCE': 'cowork',
+  }
+}
$ python manage.py loaddata ../djaodjin-saas/testsite/fixtures/test_data.json
Installed 61 object(s) from 1 fixture(s)
$ python manage.py migratedb
$ python manage.py runserver

Browsing the pricing page loads correctly at this point.

MAkcanca commented 5 years ago

This ^ Kudos for your attention. Thank you so much. Didn't realize I had to set broker.