From b638890a143f507a9b30c5078f83795698818634 Mon Sep 17 00:00:00 2001 From: Federico Fantini Date: Wed, 9 Aug 2023 16:44:21 +0200 Subject: [PATCH 1/2] added admin support source: https://github.com/intelowlproject/IntelOwl/issues/1470 updated pre-commit and fixed flake8 syntax error --- .flake8 | 11 ++++++----- .pre-commit-config.yaml | 12 ++++++------ certego_saas/apps/organization/admin.py | 2 ++ certego_saas/apps/organization/membership.py | 14 +++++++++++++- .../migrations/0002_membership_is_admin.py | 15 +++++++++++++++ 5 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 certego_saas/apps/organization/migrations/0002_membership_is_admin.py diff --git a/.flake8 b/.flake8 index eccdfba..32a28b4 100644 --- a/.flake8 +++ b/.flake8 @@ -7,9 +7,10 @@ exclude = build venv* certego_django_apps/migrations + example_project ignore = - F821 # undefined name 'Rule' - W503 # line break before binary operator - E402 # module level import not at top of file - W291 # trailing whitespace nightmare - E203 # whitespace before ':' \ No newline at end of file + F821, # undefined name 'Rule' + W503, # line break before binary operator + E402, # module level import not at top of file + W291, # trailing whitespace nightmare + E203, # whitespace before ':' \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d33db27..0d39091 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/myint/autoflake - rev: v1.4 + rev: v2.2.0 hooks: - id: autoflake args: @@ -11,15 +11,15 @@ repos: "-i", ] - repo: https://github.com/psf/black - rev: 22.3.0 + rev: 23.7.0 hooks: - id: black - - repo: https://gitlab.com/pycqa/flake8 - rev: 4.0.1 + - repo: https://github.com/pycqa/flake8 + rev: 6.1.0 hooks: - id: flake8 - repo: https://github.com/pycqa/isort - rev: 5.10.1 + rev: 5.12.0 hooks: - id: isort args: ["--profile", "black", "--filter-files", "--skip", "venv"] @@ -30,4 +30,4 @@ repos: # additional_dependencies: # - django-stubs # - djangorestframework-stubs -# args: [--no-strict-optional, --ignore-missing-imports] +# args: [--no-strict-optional, --ignore-missing-imports] \ No newline at end of file diff --git a/certego_saas/apps/organization/admin.py b/certego_saas/apps/organization/admin.py index 0f4359a..8f1193b 100644 --- a/certego_saas/apps/organization/admin.py +++ b/certego_saas/apps/organization/admin.py @@ -11,12 +11,14 @@ class MembershipAdmin(admin.ModelAdmin): "user", "organization", "is_owner", + "is_admin", "created_at", ) list_filter = ( "organization", "is_owner", + "is_admin", ) diff --git a/certego_saas/apps/organization/membership.py b/certego_saas/apps/organization/membership.py index 241dcb0..20eeff6 100644 --- a/certego_saas/apps/organization/membership.py +++ b/certego_saas/apps/organization/membership.py @@ -33,8 +33,17 @@ class Meta: on_delete=models.CASCADE, ) is_owner = models.BooleanField(default=False) + is_admin = models.BooleanField(default=False) # funcs + def clean_admin(self): + if self.is_owner and not self.is_admin: + # an owner must be an admin, permissions will be checked on this field + self.is_admin = True + + def clean(self): + super().clean() + self.clean_admin() def __str__(self): member_str = "owner" if self.is_owner else "member" @@ -48,4 +57,7 @@ class ExistingMembershipException(ValidationError): ) class OwnerCantLeaveException(ValidationError): - default_detail = "Owner cannot leave the organization but can choose to delete the organization." + default_detail = ( + "Owner cannot leave the organization" + "but can choose to delete the organization." + ) diff --git a/certego_saas/apps/organization/migrations/0002_membership_is_admin.py b/certego_saas/apps/organization/migrations/0002_membership_is_admin.py new file mode 100644 index 0000000..f500e22 --- /dev/null +++ b/certego_saas/apps/organization/migrations/0002_membership_is_admin.py @@ -0,0 +1,15 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("certego_saas_organization", "0001_initial"), + ] + + operations = [ + migrations.AddField( + model_name="membership", + name="is_admin", + field=models.BooleanField(default=False), + ), + ] From d17b44adbafb512ef8a2309a7af4da18414227d5 Mon Sep 17 00:00:00 2001 From: Federico Fantini Date: Wed, 9 Aug 2023 16:56:08 +0200 Subject: [PATCH 2/2] executed command pre-commit on all files --- .flake8 | 15 ++++++++++----- .../apps/feedback/migrations/0001_initial.py | 1 - .../apps/notifications/migrations/0001_initial.py | 1 - .../migrations/0002_alter_notification_appname.py | 1 - .../notifications/migrations/0003_for_user.py | 1 - .../apps/organization/migrations/0001_initial.py | 1 - .../apps/payments/migrations/0001_initial.py | 1 - certego_saas/apps/user/migrations/0001_initial.py | 1 - .../ext/test_utilities/no_logs_test_case.py | 1 - certego_saas/ext/upload/elastic.py | 1 - certego_saas/ext/upload/slack.py | 2 -- certego_saas/ext/upload/twitter.py | 2 -- 12 files changed, 10 insertions(+), 18 deletions(-) diff --git a/.flake8 b/.flake8 index 32a28b4..a5d6e04 100644 --- a/.flake8 +++ b/.flake8 @@ -9,8 +9,13 @@ exclude = certego_django_apps/migrations example_project ignore = - F821, # undefined name 'Rule' - W503, # line break before binary operator - E402, # module level import not at top of file - W291, # trailing whitespace nightmare - E203, # whitespace before ':' \ No newline at end of file + # undefined name 'Rule' + F821, + # line break before binary operator + W503, + # module level import not at top of file + E402, + # trailing whitespace nightmare + W291, + # whitespace before ':' + E203, \ No newline at end of file diff --git a/certego_saas/apps/feedback/migrations/0001_initial.py b/certego_saas/apps/feedback/migrations/0001_initial.py index 4510a21..8a40deb 100644 --- a/certego_saas/apps/feedback/migrations/0001_initial.py +++ b/certego_saas/apps/feedback/migrations/0001_initial.py @@ -6,7 +6,6 @@ class Migration(migrations.Migration): - initial = True dependencies = [ diff --git a/certego_saas/apps/notifications/migrations/0001_initial.py b/certego_saas/apps/notifications/migrations/0001_initial.py index 2e11f79..2c60618 100644 --- a/certego_saas/apps/notifications/migrations/0001_initial.py +++ b/certego_saas/apps/notifications/migrations/0001_initial.py @@ -5,7 +5,6 @@ class Migration(migrations.Migration): - initial = True dependencies = [ diff --git a/certego_saas/apps/notifications/migrations/0002_alter_notification_appname.py b/certego_saas/apps/notifications/migrations/0002_alter_notification_appname.py index f44db4a..0ac5ab1 100644 --- a/certego_saas/apps/notifications/migrations/0002_alter_notification_appname.py +++ b/certego_saas/apps/notifications/migrations/0002_alter_notification_appname.py @@ -4,7 +4,6 @@ class Migration(migrations.Migration): - dependencies = [ ("certego_saas_notifications", "0001_initial"), ] diff --git a/certego_saas/apps/notifications/migrations/0003_for_user.py b/certego_saas/apps/notifications/migrations/0003_for_user.py index 944b645..94843a0 100644 --- a/certego_saas/apps/notifications/migrations/0003_for_user.py +++ b/certego_saas/apps/notifications/migrations/0003_for_user.py @@ -3,7 +3,6 @@ class Migration(migrations.Migration): - dependencies = [ ("certego_saas_notifications", "0002_alter_notification_appname"), ] diff --git a/certego_saas/apps/organization/migrations/0001_initial.py b/certego_saas/apps/organization/migrations/0001_initial.py index 5118614..0f00b6a 100644 --- a/certego_saas/apps/organization/migrations/0001_initial.py +++ b/certego_saas/apps/organization/migrations/0001_initial.py @@ -6,7 +6,6 @@ class Migration(migrations.Migration): - initial = True dependencies = [ diff --git a/certego_saas/apps/payments/migrations/0001_initial.py b/certego_saas/apps/payments/migrations/0001_initial.py index 703089e..4beaae8 100644 --- a/certego_saas/apps/payments/migrations/0001_initial.py +++ b/certego_saas/apps/payments/migrations/0001_initial.py @@ -6,7 +6,6 @@ class Migration(migrations.Migration): - initial = True dependencies = [ diff --git a/certego_saas/apps/user/migrations/0001_initial.py b/certego_saas/apps/user/migrations/0001_initial.py index aa7d2d5..075511d 100644 --- a/certego_saas/apps/user/migrations/0001_initial.py +++ b/certego_saas/apps/user/migrations/0001_initial.py @@ -7,7 +7,6 @@ class Migration(migrations.Migration): - initial = True replaces = [ ("certego_saas", "0001_initial"), diff --git a/certego_saas/ext/test_utilities/no_logs_test_case.py b/certego_saas/ext/test_utilities/no_logs_test_case.py index 4a13c84..c8acb61 100644 --- a/certego_saas/ext/test_utilities/no_logs_test_case.py +++ b/certego_saas/ext/test_utilities/no_logs_test_case.py @@ -19,7 +19,6 @@ def assertNoLogs( with self.assertLogs(logger=logger, level=level) as new_log: yield except AssertionError as e: - if isinstance(e.args[0], str) and not e.args[0].startswith( "no logs of level" ): diff --git a/certego_saas/ext/upload/elastic.py b/certego_saas/ext/upload/elastic.py index 765a051..b9fa996 100644 --- a/certego_saas/ext/upload/elastic.py +++ b/certego_saas/ext/upload/elastic.py @@ -72,7 +72,6 @@ def __repr__(self): from django.db.models import fields as django_fields class BIDocument(__BIDocumentInterface, Model): - index = django_fields.CharField(max_length=100) creation_date = django_fields.DateTimeField(auto_now_add=True) category = django_fields.CharField(max_length=100) diff --git a/certego_saas/ext/upload/slack.py b/certego_saas/ext/upload/slack.py index 1402de8..84eb1cc 100644 --- a/certego_saas/ext/upload/slack.py +++ b/certego_saas/ext/upload/slack.py @@ -29,13 +29,11 @@ def send_message( if settings.DEBUG or certego_apps_settings.TESTING: - Slack = _Slack else: class Slack(_Slack): - token = settings.SLACK_TOKEN client = WebClient(token=token) diff --git a/certego_saas/ext/upload/twitter.py b/certego_saas/ext/upload/twitter.py index 6439ad8..e61c759 100644 --- a/certego_saas/ext/upload/twitter.py +++ b/certego_saas/ext/upload/twitter.py @@ -53,13 +53,11 @@ def post_tweet( if settings.DEBUG or certego_apps_settings.TESTING: - Twitter = _Twitter else: class Twitter(_Twitter): - CHARACTER_LIMIT = twitter_lib.api.CHARACTER_LIMIT client = twitter_lib.Api( consumer_key=certego_apps_settings.TWITTER_CONSUMER_KEY,