Skip to content

Commit

Permalink
Feature/TermsOfUse (#71)
Browse files Browse the repository at this point in the history
* add command to check redis connectivity

* add accepted_terms of use to user bitmap

* If no terms of use has been accepted, downgrade plan to USER_PLAN_GUEST

* Create 0042_alter_userbitmap_subscriptions.py

* apply django patch 5.0.8 https://docs.djangoproject.com/en/5.1/releases/5.0.8/
  • Loading branch information
danieleguido authored Oct 14, 2024
1 parent db5dfd4 commit f2e490f
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 142 deletions.
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pip = "*"
celery = "*"
requests = "*"
redis = "*"
django = "==5.0.7"
django = "==5.0.8"
pymysql = "*"
django-registration = "*"
gunicorn = "*"
Expand Down
263 changes: 139 additions & 124 deletions Pipfile.lock

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion impresso/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@

@admin.register(UserBitmap)
class UserBitmapAdmin(admin.ModelAdmin):
list_display = ("user", "bitmap_display", "user_plan_display", "num_subscriptions")
list_display = (
"user",
"bitmap_display",
"user_plan_display",
"num_subscriptions",
"date_accepted_terms",
)
search_fields = ["user__username", "user__email"]

def num_subscriptions(self, obj):
Expand Down
9 changes: 7 additions & 2 deletions impresso/management/commands/checksystemhealth.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,13 @@ def handle(self, *args, **options):

redis_host = settings.REDIS_HOST.split(":")[0]
redis_port = settings.REDIS_HOST.split(":")[-1]
redis_conn = redis.Redis(host=redis_host, port=redis_port, db="4")
redis_status = redis_conn.ping()
# if redis_port it is not numeric, then throw an error
if not redis_port.isnumeric():
self.stderr.write(f"Invalid Redis Port: {redis_port}")
return
self.stdout.write(f"Redis Host: \n - {redis_host}")
self.stdout.write(f"Redis Port: \n - {redis_port}")

redis_conn = redis.Redis(host=redis_host, port=redis_port, db="4")
redis_status = redis_conn.ping()
self.stdout.write(f"Redis Status: \n - {redis_status}")
31 changes: 19 additions & 12 deletions impresso/management/commands/checkuserbitmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ def handle(self, username, *args, **options):
self.stdout.write(f"User groups: \n \033[34m{'\n '.join(groups)}\033[0m")
# print out its related user bitmap. If no one, just create it.
user_bitmap = user.bitmap.get_up_to_date_bitmap()
user.bitmap = user_bitmap
user.save()
user_bitmap_length = user_bitmap.bit_length()
# print user_bitmap binary as sequence of 0 and 1
self.stdout.write(f"user get_up_to_date_bitmap(): \033[34m{bin(user_bitmap)}\033[0m")
self.stdout.write(
f"user get_up_to_date_bitmap(): \033[34m{bin(user_bitmap)}\033[0m"
)
# get the total number of bits
self.stdout.write(f"user bitmap length: \033[34m{user_bitmap_length}\033[0m")

self.stdout.write(
f"User bitmap plan max length: \033[34m{UserBitmap.BITMAP_PLAN_MAX_LENGTH}\033[0m"
)
Expand All @@ -37,20 +41,24 @@ def handle(self, username, *args, **options):
self.stdout.write(
f"User subscriptions: \n \033[34m{'\n '.join([s.get('name') for s in subscriptions])}\033[0m"
)
max_subscription_position = max(
[s["bitmap_position"] for s in subscriptions]
max_subscription_position = (
max([s["bitmap_position"] for s in subscriptions]) if subscriptions else -1
)
self.stdout.write(
f"Max subscription position: \033[34m{max_subscription_position}\033[0m"
)
self.stdout.write(
f"adjusted max subscription position with groups position: \033[34m{max_subscription_position + UserBitmap.BITMAP_PLAN_MAX_LENGTH}\033[0m"
)

self.stdout.write("Verify other subscriptions til max user bitmap position (the rest should be 0)")

self.stdout.write(
"Verify other subscriptions til max user bitmap position (the rest should be 0)"
)
# get all possible subscriptions
all_subscriptions = DatasetBitmapPosition.objects.filter(bitmap_position__lte=max_subscription_position).order_by("bitmap_position")

all_subscriptions = DatasetBitmapPosition.objects.filter(
bitmap_position__lte=max_subscription_position
).order_by("bitmap_position")

# print out all possible subscriptions
for subscription in all_subscriptions:
position = subscription.bitmap_position + 5
Expand All @@ -63,8 +71,7 @@ def handle(self, username, *args, **options):
f"\033[34m {subscription.name} at position: {position} is set: {is_set}\033[0m"
)
else:
self.stdout.write(
f" {subscription.name} at position: {position} is set: {is_set}"
)
self.stdout.write(
f" {subscription.name} at position: {position} is set: {is_set}"
)
self.stdout.write("\n---\nDone! \033[31m❤️\033[0m \n---\n")

18 changes: 18 additions & 0 deletions impresso/migrations/0041_userbitmap_date_accepted_terms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.4 on 2024-10-12 12:55

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('impresso', '0040_alter_datasetbitmapposition_metadata_and_more'),
]

operations = [
migrations.AddField(
model_name='userbitmap',
name='date_accepted_terms',
field=models.DateTimeField(blank=True, null=True),
),
]
18 changes: 18 additions & 0 deletions impresso/migrations/0042_alter_userbitmap_subscriptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.7 on 2024-10-14 06:56

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('impresso', '0041_userbitmap_date_accepted_terms'),
]

operations = [
migrations.AlterField(
model_name='userbitmap',
name='subscriptions',
field=models.ManyToManyField(blank=True, to='impresso.datasetbitmapposition'),
),
]
7 changes: 6 additions & 1 deletion impresso/models/userBitmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
class UserBitmap(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="bitmap")
bitmap = models.BinaryField(editable=False, null=True, blank=True)
subscriptions = models.ManyToManyField(DatasetBitmapPosition)
subscriptions = models.ManyToManyField(DatasetBitmapPosition, blank=True)
# date of acceptance of the term of use
date_accepted_terms = models.DateTimeField(null=True, blank=True)
# Guest - Unregisted User public NA (True by default) used only for test purposes
# Impresso Registered User impresso Account created, no academic afiliation
# Student or Teacher - Educational User educational Account created, educational academic afiliation
Expand All @@ -23,6 +25,9 @@ class UserBitmap(models.Model):
BITMAP_PLAN_MAX_LENGTH = 5

def get_up_to_date_bitmap(self):
# if the user hasn't accepted terms of use, return the default bitmap
if not self.date_accepted_terms:
return UserBitmap.USER_PLAN_GUEST
"""
Get the bitmap using the groups the user is affiliated to and the affiliations to the DatasetBitmapPosition
The four first bits (starting on the left, indices 0-3) are the ones relating to the user plans
Expand Down
2 changes: 1 addition & 1 deletion impresso/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
EMAIL_PORT = get_env_variable("EMAIL_PORT", 0)
DEFAULT_FROM_EMAIL = get_env_variable("DEFAULT_FROM_EMAIL", "info@")
# Celery
REDIS_HOST = get_env_variable("REDIS_HOST", "localhost")
REDIS_HOST = get_env_variable("REDIS_HOST", "localhost:6379")
CELERY_BROKER_URL = f"redis://{REDIS_HOST}/4"
CELERY_RESULT_BACKEND = f"redis://{REDIS_HOST}/5"
CELERYD_PREFETCH_MULTIPLIER = 2
Expand Down

0 comments on commit f2e490f

Please sign in to comment.