Skip to content

Commit

Permalink
Merge pull request #19 from certego/develop
Browse files Browse the repository at this point in the history
0.2.0
  • Loading branch information
0ssigeno authored Jul 5, 2022
2 parents 49c3dea + 6e3a504 commit a900160
Show file tree
Hide file tree
Showing 32 changed files with 224 additions and 40 deletions.
17 changes: 17 additions & 0 deletions certego_saas/apps/notifications/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ class NotificationViewSet(ReadOnlyViewSet):
serializer_class = NotificationSerializer
filter_class = NotificationFilter

# i have absolutely no fucking idea on why the fuck the filtering class is not working, so i'm just doing it manually
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
if "read" in request.query_params:
value = request.query_params["read"]
if value == "true":
queryset = queryset.filter(read_by_users__in=[self.request.user])
elif value == "false":
queryset = queryset.exclude(read_by_users__in=[self.request.user])
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)

serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)

def get_queryset(self):
qs = super().get_queryset()
# if FILTER_NOTIFICATIONS_VIEW_FOR_CURRENTAPP is True then,
Expand Down
4 changes: 2 additions & 2 deletions certego_saas/apps/organization/serializers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from rest_flex_fields.serializers import FlexFieldsModelSerializer
from rest_framework import serializers as rfs

from certego_saas.user.models import User
from certego_saas.user.serializers import UserSerializer
from certego_saas.apps.user.models import User
from certego_saas.apps.user.serializers import UserSerializer

from .invitation import Invitation
from .membership import Membership
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class AbstractUserAdmin(DjangoUserAdmin, ExportCsvAdminMixin):
"""
An abstract admin class for the
:class:`certego_saas.user.models.User` model.
:class:`certego_saas.apps.user.models.User` model.
"""

add_form = UserCreateForm
Expand Down
6 changes: 6 additions & 0 deletions certego_saas/apps/user/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class CertegoUserConfig(AppConfig):
name = "certego_saas.apps.user"
label = "certego_saas_user"
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
class Migration(migrations.Migration):

initial = True

replaces = [
("certego_saas", "0001_initial"),
]
dependencies = [
("auth", "0012_alter_user_first_name_max_length"),
]
Expand Down
63 changes: 63 additions & 0 deletions certego_saas/apps/user/migrations/0002_migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from django.db import OperationalError, migrations
from django.db.migrations.operations.base import Operation


class AlterCertegoSaasUser(Operation):
reversible = True

def state_forwards(self, app_label, state):
pass

def database_forwards(self, app_label, schema_editor, from_state, to_state):
vendor = schema_editor.connection.vendor

for old, new in zip(
[
"certego_saas_user",
"certego_saas_user_groups",
"certego_saas_user_user_permissions",
],
[
"certego_saas_user_user",
"certego_saas_user_user_groups",
"certego_saas_user_user_user_permissions",
],
):
try:
schema_editor.execute(
f"ALTER TABLE {'IF EXISTS' if vendor == 'postgresql' else ''} {old} RENAME TO {new};"
)
except OperationalError:
pass

def database_backwards(self, app_label, schema_editor, from_state, to_state):
vendor = schema_editor.connection.vendor
for old, new in zip(
[
"certego_saas_user",
"certego_saas_user_groups",
"certego_saas_user_user_permissions",
],
[
"certego_saas_user_user",
"certego_saas_user_user_groups",
"certego_saas_user_user_user_permissions",
],
):
try:
schema_editor.execute(
f"ALTER TABLE {'IF EXISTS' if vendor == 'postgresql' else ''} {new} RENAME TO {old};"
)
except OperationalError:
pass

def describe(self):
return "Alter Certego_saas_user table if necessary"


class Migration(migrations.Migration):
dependencies = [
("certego_saas_user", "0001_initial"),
]

operations = [AlterCertegoSaasUser()]
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class AbstractUser(DjangoAbstractUser):

class Meta:
abstract = True
app_label = "certego_saas"

# utils

Expand Down Expand Up @@ -87,9 +86,6 @@ def is_email_verified(self) -> Optional[bool]:
from certego_saas.apps.payments.models import Customer, Subscription

class User(AbstractUser):
class Meta:
app_label = "certego_saas"

def has_customer(self) -> bool:
return hasattr(self, "customer") and self.customer is not None

Expand Down Expand Up @@ -155,5 +151,4 @@ def post_save_user_handler(sender, instance: User, created: bool, **kwargs):
else:

class User(AbstractUser): # type: ignore
class Meta:
app_label = "certego_saas"
pass
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

from certego_saas.ext.serializers import APIExceptionSerializer
from certego_saas.ext.views import APIView

from ..settings import certego_apps_settings
from certego_saas.settings import certego_apps_settings

UserAccessSerializer = certego_apps_settings.USER_ACCESS_SERIALIZER

Expand Down
18 changes: 16 additions & 2 deletions certego_saas/ext/models.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import enum

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db import models
from django.utils.functional import classproperty

try:
# Django 3.1 and above
from django.utils.functional import classproperty
except ImportError:
from django.utils.decorators import classproperty

from .managers import AppSpecificModelManager, ToggleableModelManager


class AppChoices(models.TextChoices):
class AppChoices(str, enum.Enum):
ACCOUNTS = "ACCOUNTS"
DRAGONFLY = "DRAGONFLY"
INTELOWL = "INTELOWL"
QUOKKA_PUBLIC = "QUOKKA_PUBLIC"

@classproperty
def CURRENTAPP(cls) -> str:
Expand All @@ -18,6 +26,12 @@ def CURRENTAPP(cls) -> str:
except KeyError:
raise ImproperlyConfigured(f"Incorrect HOST_NAME: {settings.HOST_NAME} set")

@classproperty
def choices(cls):
return [
(member.value, member.value.replace("_", " ").title()) for member in cls
]


class TimestampedModel(models.Model):
"""
Expand Down
File renamed without changes.
Empty file.
36 changes: 36 additions & 0 deletions certego_saas/management/commands/list_jobs_queues.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""This command allow us to list all tasks from the queue (selected automatically)"""

from django.conf import settings
from django.core.management.base import BaseCommand


class Command(BaseCommand):
help = "List available messages in the queue used by this instance"

def handle(self, *args, **options):
try:
import boto3
except ImportError:
self.stdout.write(
self.style.ERROR(
"boto3 is not installed. Please install boto3 to use this command."
)
)
return

client = boto3.client("sqs", settings.AWS_REGION)
queues = client.list_queues().get("QueueUrls", [])
for queue in queues:
if input(f"Are you sure you want to select {queue}? (y/n) ") == "y":
queue_data = client.get_queue_attributes(
QueueUrl=queue,
AttributeNames=["ApproximateNumberOfMessages"],
)
message_in_the_queue = queue_data["Attributes"][
"ApproximateNumberOfMessages"
]
self.stdout.write(
f"the number of the messages in the queue {queue} is {message_in_the_queue}"
)
else:
self.stdout.write(self.style.ERROR(f"Skipping {queue}"))
31 changes: 31 additions & 0 deletions certego_saas/management/commands/purge_queues.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django.conf import settings
from django.core.management.base import BaseCommand


class Command(BaseCommand):
help = "Purge selected queues"

def handle(self, *args, **options):
# we should import it here so that projects that don't use boto3 can still use this library
# since we are not adding this in the requirements.txt file
try:
import boto3
except ImportError:
self.stdout.write(
self.style.ERROR(
"boto3 is not installed. Please install boto3 to use this command."
)
)
return
client = boto3.client("sqs", settings.AWS_REGION)
queues = client.list_queues().get("QueueUrls", [])
for queue in queues:
if input(f"Are you sure you want to purge {queue}? (y/n) ") == "y":
client.purge_queue(QueueUrl=queue)
self.stdout.write(
self.style.SUCCESS(
f"Successfully purged queue {queue} with url {queue}"
)
)
else:
self.stdout.write(self.style.ERROR(f"Skipping {queue}"))
13 changes: 11 additions & 2 deletions certego_saas/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# flake8: noqa
from django.apps import apps

from .user.models import User
__all__ = []

__all__ = ["User"]
if apps.is_installed("certego_saas.apps.user"):
from .apps.user.models import User

__all__ += ["User"]


if apps.is_installed("certego_saas.apps.notifications"):
from .apps.notifications.models import Notification

__all__ += ["Notification"]

if apps.is_installed("certego_saas.apps.organization"):
# import these models only if organization app is installed,
Expand Down
11 changes: 7 additions & 4 deletions certego_saas/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@

TEST_RUNNER = "tests.timed_runner.TimedRunner"
# stripe-python
STRIPE_LIVE_MODE = (
settings.PUBLIC_DEPLOYMENT and not settings.STAGE_CI and not settings.DEBUG
)
try:
STRIPE_LIVE_MODE = (
settings.PUBLIC_DEPLOYMENT and not settings.STAGE_CI and not settings.DEBUG
)
except AttributeError:
STRIPE_LIVE_MODE = False
stripe.api_key = str(
get_secret("STRIPE_LIVE_SECRET_KEY", None)
if STRIPE_LIVE_MODE
Expand All @@ -28,7 +31,7 @@
"AUTH_COOKIE_SAMESITE": "Strict",
"AUTH_COOKIE_DOMAIN": None,
"FILTER_NOTIFICATIONS_VIEW_FOR_CURRENTAPP": True,
"USER_ACCESS_SERIALIZER": "certego_saas.user.serializers.UserAccessSerializer",
"USER_ACCESS_SERIALIZER": "certego_saas.apps.user.serializers.UserAccessSerializer",
"ORGANIZATION_MAX_MEMBERS": 3,
# app info
"HOST_URI": settings.HOST_URI,
Expand Down
12 changes: 8 additions & 4 deletions certego_saas/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Patterns
from django.apps import apps
from django.urls import include, path

# Patterns
urlpatterns = [
urlpatterns = []

if apps.is_installed("certego_saas.apps.user"):
# certego_saas: user sub-app
path("", include("certego_saas.user.urls")),
]
urlpatterns.append(
path("", include("certego_saas.apps.user.urls")),
)
2 changes: 1 addition & 1 deletion certego_saas/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = "0.1.3"
VERSION = "0.2.0"
1 change: 1 addition & 0 deletions docs/source/apps/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Apps (``certego_saas.apps``)
:maxdepth: 2

auth
user
feedback
notifications
organization
Expand Down
10 changes: 5 additions & 5 deletions docs/source/user.rst → docs/source/apps/user.rst
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
User (``certego_saas.user``)
User (``certego_saas.apps.user``)
====================================

``admin.py``
------------------
------------------

.. automodule:: certego_saas.user.admin
.. automodule:: certego_saas.apps.user.admin
:members:
:show-inheritance:

``models.py``
------------------
------------------

.. automodule:: certego_saas.user.models
.. automodule:: certego_saas.apps.user.models
:members:
:show-inheritance:
:exclude-members: DoesNotExist, MultipleObjectsReturned
Expand All @@ -22,15 +22,15 @@ User (``certego_saas.user``)
------------------
------------------

.. automodule:: certego_saas.user.serializers
.. automodule:: certego_saas.apps.user.serializers
:members:
:show-inheritance:

``views.py``
------------------
------------------

.. automodule:: certego_saas.user.views
.. automodule:: certego_saas.apps.user.views
:members:
:show-inheritance:
:exclude-members: get_object, get_permissions, get_queryset, get_serializer
2 changes: 1 addition & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Directory Structure
apps/ optional/extra apps
|- auth/ "certego_saas.apps.auth" app (optional)
| |- backend.py authentication backend
|- user/ User model, UserAccessSerializer serializer, etc.
|- feedback/ "certego_saas.apps.feedback" app (optional)
|- notifications/ "certego_saas.apps.notifications" app (optional)
|- organization/ "certego_saas.apps.organization" app (not exactly optional)
Expand All @@ -24,7 +25,6 @@ Directory Structure
templates/
|- certego_saas/ E-mail templates
|- context_processors.py custom context processor
user/ User model, UserAccessSerializer serializer, etc.
apps.py "certego_saas" app
models.py default models
settings.py default settings/configurations
Expand Down
Loading

0 comments on commit a900160

Please sign in to comment.