Skip to content

Commit

Permalink
Feature/user request (#72) Add userRequest model and update related f…
Browse files Browse the repository at this point in the history
…iles

This pull request adds a new Django model called userRequest.py, creates corresponding migrations, updates the admin site, and adds various improvements. The new model includes a userBitmap Celery task, a new reviewer field in datasetBitmapPosition, and a new notes field for the user.

Changes:

Added userRequest.py
Added migrations for userRequest.py
Updated admin.py
Updated __init__.py
Added userBitmap celery task
Created 0045_datasetbitmapposition_reviewer.py with a new reviewer field
Added update_user_bitmap_task
Updated datasetBitmapPosition.py
Updated admin.py
Added profile__uid to user search in alignment with errors
Renamed subscriber field to user
Added notes field and appended latest note to changelog on save
--------
  • Loading branch information
danieleguido authored Oct 16, 2024
1 parent d7157e0 commit 2d0dd7b
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 6 deletions.
22 changes: 19 additions & 3 deletions impresso/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,25 @@
from .models import SearchQuery, ContentItem
from .models import Collection, CollectableItem, Tag, TaggableItem
from .models import Attachment, UploadedImage
from .models import UserBitmap, DatasetBitmapPosition
from .models import UserBitmap, DatasetBitmapPosition, UserRequest

from impresso.tasks import after_user_activation


@admin.register(UserRequest)
class UserRequestAdmin(admin.ModelAdmin):
list_display = (
"user",
"reviewer",
"subscription",
"status",
"date_created",
)
search_fields = ["subscriber__username", "subscription__name"]
list_filter = ["status"]
autocomplete_fields = ["user", "reviewer", "subscription"]


@admin.register(UserBitmap)
class UserBitmapAdmin(admin.ModelAdmin):
list_display = (
Expand Down Expand Up @@ -58,9 +72,10 @@ def user_plan_display(self, obj):

@admin.register(DatasetBitmapPosition)
class DatasetBitmapPositionAdmin(admin.ModelAdmin):
list_display = ("name", "bitmap_position")
search_fields = ["name"]
list_display = ("name", "bitmap_position", "reviewer")
search_fields = ["name", "reviewer__username", "reviewer__email"]
readonly_fields = ("bitmap_position",)
autocomplete_fields = ["reviewer"]


@admin.register(Issue)
Expand Down Expand Up @@ -224,6 +239,7 @@ class UserAdmin(BaseUserAdmin):
"max_parallel_jobs",
)
actions = ["make_active", "make_suspended"]
search_fields = ["username", "profile__uid", "email"]

@admin.action(description="ACTIVATE selected users")
def make_active(self, request, queryset):
Expand Down
34 changes: 34 additions & 0 deletions impresso/migrations/0043_userrequest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 5.0.8 on 2024-10-14 14:33

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('impresso', '0042_alter_userbitmap_subscriptions'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='UserRequest',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('date_created', models.DateTimeField(auto_now_add=True)),
('date_last_modified', models.DateTimeField(auto_now=True)),
('status', models.CharField(choices=[('pending', 'Pending'), ('approved', 'Approved'), ('rejected', 'Rejected')], default='pending', max_length=10)),
('change_logs', models.JSONField(blank=True, default=list, null=True)),
('reviewer', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='review', to=settings.AUTH_USER_MODEL)),
('subscriber', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='request', to=settings.AUTH_USER_MODEL)),
('subscription', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='impresso.datasetbitmapposition')),
],
options={
'verbose_name': 'User Subscription Request',
'verbose_name_plural': 'User Subscription Requests',
'unique_together': {('subscriber', 'subscription')},
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 5.0.8 on 2024-10-14 14:40

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('impresso', '0043_userrequest'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.RenameField(
model_name='userrequest',
old_name='change_logs',
new_name='changelog',
),
migrations.AlterField(
model_name='userrequest',
name='reviewer',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='review', to=settings.AUTH_USER_MODEL),
),
]
21 changes: 21 additions & 0 deletions impresso/migrations/0045_datasetbitmapposition_reviewer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 5.0.8 on 2024-10-15 14:07

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('impresso', '0044_rename_change_logs_userrequest_changelog_and_more'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.AddField(
model_name='datasetbitmapposition',
name='reviewer',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='reviewed_datasets', to=settings.AUTH_USER_MODEL),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 5.0.8 on 2024-10-16 09:24

from django.conf import settings
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('impresso', '0045_datasetbitmapposition_reviewer'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.RenameField(
model_name='userrequest',
old_name='subscriber',
new_name='user',
),
migrations.AlterUniqueTogether(
name='userrequest',
unique_together={('user', 'subscription')},
),
]
18 changes: 18 additions & 0 deletions impresso/migrations/0047_userrequest_notes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.8 on 2024-10-16 09:59

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('impresso', '0046_rename_subscriber_userrequest_user_and_more'),
]

operations = [
migrations.AddField(
model_name='userrequest',
name='notes',
field=models.TextField(blank=True, null=True),
),
]
1 change: 1 addition & 0 deletions impresso/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@

from .datasetBitmapPosition import DatasetBitmapPosition
from .userBitmap import UserBitmap
from .userRequest import UserRequest
7 changes: 7 additions & 0 deletions impresso/models/datasetBitmapPosition.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ class DatasetBitmapPosition(models.Model):
blank=True,
)
metadata = models.JSONField(default=dict, blank=True)
reviewer = models.ForeignKey(
"auth.User",
on_delete=models.SET_NULL,
related_name="reviewed_datasets",
null=True,
blank=True,
)

def __str__(self):
return self.name
Expand Down
54 changes: 54 additions & 0 deletions impresso/models/userRequest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from django.db import models
from django.contrib.auth.models import User
from .datasetBitmapPosition import DatasetBitmapPosition


class UserRequest(models.Model):
STATUS_PENDING = "pending"
STATUS_APPROVED = "approved"
STATUS_REJECTED = "rejected"

user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="request")
reviewer = models.ForeignKey(
User, on_delete=models.SET_NULL, related_name="review", null=True, blank=True
)
subscription = models.ForeignKey(
DatasetBitmapPosition, on_delete=models.SET_NULL, null=True
)
date_created = models.DateTimeField(auto_now_add=True)
date_last_modified = models.DateTimeField(auto_now=True)
status = models.CharField(
max_length=10,
default=STATUS_PENDING,
choices=(
(STATUS_PENDING, "Pending"),
(STATUS_APPROVED, "Approved"),
(STATUS_REJECTED, "Rejected"),
),
)
changelog = models.JSONField(null=True, blank=True, default=list)
notes = models.TextField(null=True, blank=True)

def __str__(self):
return f"{self.user.username} Request for {self.subscription.name if self.subscription else '[deleted subscription]'}"

class Meta:
unique_together = ("user", "subscription")
verbose_name = "User Subscription Request"
verbose_name_plural = "User Subscription Requests"

def save(self, *args, **kwargs):
if self.pk:
# Prepare the new changelog entry
changelog_entry = {
"status": self.status,
"subscription": self.subscription.name if self.subscription else None,
"date": self.date_last_modified.isoformat(),
"reviewer": self.reviewer.username if self.reviewer else None,
"notes": self.notes if self.notes else "",
}

# Append the new entry to the changelog list
self.changelog.append(changelog_entry)

super().save(*args, **kwargs)
16 changes: 13 additions & 3 deletions impresso/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from .utils.tasks.account import send_emails_after_user_registration
from .utils.tasks.account import send_emails_after_user_activation
from .utils.tasks.account import send_email_password_reset
from .utils.tasks.userBitmap import update_user_bitmap

logger = get_task_logger(__name__)

Expand Down Expand Up @@ -1027,6 +1028,15 @@ def update_collection(
items_ids=items_ids_to_remove,
method=METHOD_DEL_FROM_INDEX,
)
if items_ids_to_add or items_ids_to_remove:
# update count items in collection (db)
count_items_in_collection.delay(collection_id=collection_id)
# update count items in collection (db)
count_items_in_collection.delay(collection_id=collection_id)


@app.task(bind=True)
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
9 changes: 9 additions & 0 deletions impresso/utils/tasks/userBitmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from ...models import UserBitmap


def update_user_bitmap(user_id):
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")
user_bitmap.bitmap = bitmap_bytes
user_bitmap.save()

0 comments on commit 2d0dd7b

Please sign in to comment.