Skip to content

Commit

Permalink
update task celery to retrieve a serialized version f the bitmap (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleguido authored Oct 17, 2024
1 parent 57d71df commit 43712f0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 11 deletions.
27 changes: 20 additions & 7 deletions impresso/management/commands/updateuserbitmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument("username", type=str)
parser.add_argument("bitmap", type=str, nargs="?", default=None)
parser.add_argument(
"--immediate",
action="store_true",
help="Run the task immediately instead of delaying it",
)

def handle(self, username, *args, **options):
def handle(self, username, immediate=False, *args, **options):
self.stdout.write(f"Get user with username: {username}")
self.stdout.write(f"Immediate: {immediate}")
user = User.objects.get(username=username)
self.stdout.write(f"User: pk={user.id} \033[34m{user.username}\033[0m")
# currrent user bitmap
Expand All @@ -31,14 +37,21 @@ def handle(self, username, *args, **options):
self.stdout.write(
f"SAVED ^ EXPECTED difference:\n \033[34m{bin(difference)}\033[0m"
)
if immediate:
# collection_id, user_id, items_ids_to_add=[], items_ids_to_remove=[]
instance = update_user_bitmap_task(
user_id=user.id,
)

self.stdout.write(
f"\nTask returned this updated bitmap: \n \033[34m{instance.get('bitmap')}\033[0m\n\n"
)
self.stdout.write(
f"Task returned this serialized object: \n \033[34m{instance}\033[0m\n\n"
)
return
# collection_id, user_id, items_ids_to_add=[], items_ids_to_remove=[]
message = update_user_bitmap_task.delay(
collection_id=collection.id,
user_id=user.id,
items_ids_to_add=items_to_add,
items_ids_to_remove=items_to_remove,
)
self.stdout.write(
f"\n5. Task \033[36m{message.id}\033[0m launched, check celery."
)
self.stdout.write(f"\n5. Task \033[36m{message}\033[0m launched, check celery.")
27 changes: 26 additions & 1 deletion impresso/models/userBitmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,33 @@ def get_up_to_date_bitmap(self):

return bitmap

def get_bitmap_as_int(self):
return int.from_bytes(self.bitmap, byteorder="big")

def get_user_plan(self):
if not self.bitmap:
return "GUEST"
if not self.date_accepted_terms:
return "GUEST"
bitmap_int = self.get_bitmap_as_int()
bitmap_length = bitmap_int.bit_length()
# Extract the first 5 bits
bitmap_plan = (
bitmap_int >> (bitmap_length - UserBitmap.BITMAP_PLAN_MAX_LENGTH)
) & 0b11111
if bitmap_plan == UserBitmap.USER_PLAN_GUEST:
return "GUEST"
if bitmap_plan == UserBitmap.USER_PLAN_AUTH_USER:
return "AUTH_USER"
if bitmap_plan == UserBitmap.USER_PLAN_EDUCATIONAL:
return "EDUCATIONAL"
if bitmap_plan == UserBitmap.USER_PLAN_RESEARCHER:
return "RESEARCHER"
return "AUTH_USER"

def __str__(self):
return f"{self.user.username} Bitmap"
bitmap = self.get_bitmap_as_int()
return f"{self.user.username} Bitmap {bin(bitmap)}"

class Meta:
verbose_name = "User Bitmap"
Expand Down
4 changes: 2 additions & 2 deletions impresso/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,5 +1038,5 @@ def update_user_bitmap_task(self, user_id):
Update the user bitmap for the given user.
"""
logger.info(f"User bitmap update request for user {user_id}")
update_user_bitmap(user_id=user_id)
return
updated_bitmap = update_user_bitmap(user_id=user_id)
return updated_bitmap
24 changes: 23 additions & 1 deletion impresso/utils/tasks/userBitmap.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
import json
from django.core.serializers import serialize
from ...models import UserBitmap


def update_user_bitmap(user_id):
"""
Updates the bitmap for a given user.
This function updates user bitmap to the most recent version taken into account user
groups, plan and terms of use acceptance.
Args:
user_id (int): The ID of the user whose bitmap needs to be updated.
Returns:
dict: A dictionary containing the updated bitmap as an integer.
"""
user_bitmap = UserBitmap.objects.get(user_id=user_id)
bitmap = user_bitmap.get_up_to_date_bitmap()
bitmap_bytes = bitmap.to_bytes((user_bitmap.bit_length() + 7) // 8, byteorder="big")
bitmap_bytes = bitmap.to_bytes((bitmap.bit_length() + 7) // 8, byteorder="big")
user_bitmap.bitmap = bitmap_bytes
user_bitmap.save()
serialized = json.loads(serialize("json", [user_bitmap]))[0].get("fields")
return {
"date_accepted_terms": serialized.get("date_accepted_terms"),
"bitmap_base64": serialized.get("bitmap"),
"subscriptions": serialized.get("subscriptions"),
"bitmap": bin(user_bitmap.get_bitmap_as_int()),
"plan": user_bitmap.get_user_plan(),
}

0 comments on commit 43712f0

Please sign in to comment.