Skip to content

Commit

Permalink
feat: Added notification mechanism for some operations
Browse files Browse the repository at this point in the history
  • Loading branch information
onuratakan committed Oct 9, 2024
1 parent d74b996 commit f062cff
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.9 on 2024-10-09 09:10

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('app', '0011_user_ldap'),
]

operations = [
migrations.AddField(
model_name='thenotifications',
name='section',
field=models.CharField(blank=True, max_length=1000),
),
migrations.AlterField(
model_name='user',
name='dark_mode',
field=models.BooleanField(blank=True, default=False, null=True),
),
]
18 changes: 18 additions & 0 deletions upsonic_on_prem/dash/app/migrations/0013_thenotifications_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.9 on 2024-10-09 09:40

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('app', '0012_thenotifications_section_alter_user_dark_mode'),
]

operations = [
migrations.AddField(
model_name='thenotifications',
name='type',
field=models.CharField(blank=True, max_length=1000),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 4.2.9 on 2024-10-09 11:36

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('app', '0013_thenotifications_type'),
]

operations = [
migrations.AlterModelOptions(
name='thenotifications',
options={'ordering': ['-date']},
),
migrations.AlterField(
model_name='thenotifications',
name='section',
field=models.CharField(blank=True, max_length=1000, null=True),
),
migrations.AlterField(
model_name='thenotifications',
name='type',
field=models.CharField(blank=True, max_length=1000, null=True),
),
]
8 changes: 6 additions & 2 deletions upsonic_on_prem/dash/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ class TheNotifications(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)

important = models.BooleanField(default=False)
section = models.CharField(max_length=1000, blank=True, null=True)
type = models.CharField(max_length=1000, blank=True, null=True)

def __str__(self):
return self.title + " - " + str(self.date)
class Meta:
ordering = ['-date']


class User(AbstractUser):
Expand Down Expand Up @@ -127,9 +131,9 @@ def can_read(self, scope):
def __str__(self):
return self.username

def notify(self, title, message, important=False):
def notify(self, title, message, type=None, important=False):
notification = TheNotifications(
title=title, message=message, important=important, owner=self
title=title, message=message, type=type, important=important, owner=self
)
notification.save()
self.notifications.add(notification)
Expand Down
4 changes: 4 additions & 0 deletions upsonic_on_prem/dash/app/templates/analyze_user.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@


<div class="uk-container uk-width-expand uk-margin-medium-top uk-margin-medium-left scrollable_side" style="height: 100vh;">


{% include 'notifications.html' %}

<div uk-grid class="uk-margin-medium-bottom">
<div class="uk-width-expand">
<h1 class="uk-h2 uk-margin text-foreground">Logs of User: {{user.get_username}}</h1>
Expand Down
2 changes: 2 additions & 0 deletions upsonic_on_prem/dash/app/templates/community.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@


<div class="uk-container uk-width-expand uk-margin-medium-top scrollable_side" style="height: 80vh;">
{% include 'notifications.html' %}

<div uk-grid class="uk-margin-medium-bottom">
<div class="uk-width-expand">
<h1 class="uk-h2 uk-margin text-foreground">Users</h1>
Expand Down
2 changes: 2 additions & 0 deletions upsonic_on_prem/dash/app/templates/control_user.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@


<div class="uk-container uk-width-expand uk-margin-medium-top uk-margin-medium-left scrollable_side" style="height: 100vh;">
{% include 'notifications.html' %}

<div uk-grid class="uk-margin-medium-bottom">
<div class="uk-width-expand">
<h1 class="uk-h2 uk-margin text-foreground"><a href="#" onclick="page_load('{% url 'community' %}');event.preventDefault();" class="uk-button uk-button-default" style="width: 1px;"><span uk-icon="chevron-left"></span></a>
Expand Down
7 changes: 7 additions & 0 deletions upsonic_on_prem/dash/app/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@


<div class="uk-container uk-width-expand uk-margin-medium-top scrollable_side" style="height: 80vh;">



{% include 'notifications.html' %}



<div uk-grid class="uk-margin-medium-bottom">
<div class="uk-width-expand">
<h1 class="uk-h2 uk-margin text-foreground">Your Libraries</h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@


<div class="uk-container uk-width-expand ">



<div uk-grid class="uk-height-1-1">
<div class="uk-width-auto" style=" padding-left: 0px;">

{% include 'cloud_side_bar.html' %}
</div>

<div class="uk-width-expand uk-margin-medium-top">
{% include 'notifications.html' %}

{% include 'libraries/control_library_header.html' %}

Expand Down
4 changes: 4 additions & 0 deletions upsonic_on_prem/dash/app/templates/libraries/libraries.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@


<div class="uk-container uk-width-expand " uk-height-viewport="offset-top: true" >


<div uk-grid>
<div class="uk-width-auto" style=" padding-left: 0px;">

{% include 'cloud_side_bar_other.html' %}
</div>


<div class="uk-width-expand uk-margin-medium-top scrollable_side" style="height: 80vh;">
{% include 'notifications.html' %}

{% include 'home/cardview.html' %}

Expand Down
55 changes: 55 additions & 0 deletions upsonic_on_prem/dash/app/templates/notifications.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{% load tags %}


<div class="uk-flex uk-flex-column ">

{% notifications as notifications %}

{% for notif in notifications %}


{% if notif.type == "info" or notif.type == "danger" %}



{% if notif.type == "info" %}
<div class="uk-alert uk-margin-small-bottom" uk-alert>

{% elif notif.type == "danger" %}
<div class="uk-alert uk-alert-danger uk-margin-small-bottom" uk-alert>



{% endif %}


{% if notif.section == "ai" %}
<svg uk-icon="nut" style="top: 0.8rem;"></svg>
{% endif %}
{% if notif.section == "edit" %}
<svg uk-icon="file-edit" style="top: 0.8rem;"></svg>
{% endif %}


<a href class="uk-alert-close" uk-close></a>



<div class="uk-alert-title">{{notif.title}}</div>



<div class="uk-alert-description">
{{notif.message}}
</div>
</div>

{% endif %}

{% endfor %}





</div>
10 changes: 10 additions & 0 deletions upsonic_on_prem/dash/app/templatetags/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ def pages_tag(context):
return []


@register.simple_tag(name="notifications", takes_context=True)
def notifications_tag(context):
request = context["request"]
the_notifications = request.user.notifications.filter(read=False)
for each in the_notifications:
each.read = True
each.save()
return the_notifications


@register.simple_tag(name="pages_len", takes_context=True)
def pages_len(context):
request = context["request"]
Expand Down
30 changes: 22 additions & 8 deletions upsonic_on_prem/dash/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def control_user(request, id):
request.user.notify(
"User Updated",
f"User {the_user.username} updated successfully, the user must login again to access",
"info"
)
return redirect(to="control_user", id=id)

Expand Down Expand Up @@ -189,6 +190,7 @@ def add_write_scope(request, id):
request.user.notify(
"Write Scope Added",
f"Scope {request.POST.get('scope')} added to user {the_user.username}",
"info"
)
return redirect(to="control_user", id=id)

Expand All @@ -204,7 +206,8 @@ def delete_write_scope(request, scope, id):
scope, the_user.access_key
)
request.user.notify(
"Write Scope Deleted", f"Scope {scope} deleted from user {the_user.username}"
"Write Scope Deleted", f"Scope {scope} deleted from user {the_user.username}",
"info"
)
return redirect(to="control_user", id=id)

Expand All @@ -223,6 +226,7 @@ def add_read_scope(request, id):
request.user.notify(
"Read Scope Added",
f"Scope {request.POST.get('scope')} added to user {the_user.username}",
"info"
)
return redirect(to="control_user", id=id)

Expand All @@ -238,7 +242,8 @@ def delete_read_scope(request, id, scope):
scope, the_user.access_key
)
request.user.notify(
"Read Scope Deleted", f"Scope {scope} deleted from user {the_user.username}"
"Read Scope Deleted", f"Scope {scope} deleted from user {the_user.username}",
"info"
)
return redirect(to="control_user", id=id)

Expand Down Expand Up @@ -643,6 +648,7 @@ def regenerate_documentation(request, id):
request.user.notify(
"Documentation is Generating",
f"Documentation for {id} is generating, it will be ready soon.",
"info"
)
models.AI_Task(
task_name="documentation",
Expand All @@ -665,6 +671,7 @@ def regenerate_documentation(request, id):
request.user.notify(
"Documentation Generated",
f"Documentation for {id} is generated, you can access it now.",
"info"
)
return redirect(to="control_element", id=id)

Expand Down Expand Up @@ -698,7 +705,8 @@ def delete_user(request, id):
the_user.delete_user(request.user.access_key)
the_user.delete()
request.user.notify(
"User Deleted", f"User {the_user.username} deleted successfully"
"User Deleted", f"User {the_user.username} deleted successfully",
"info"
)
return redirect(to="community")
else:
Expand All @@ -709,7 +717,8 @@ def delete_user(request, id):
@login_required
def delete_scope(request, id):
API_Integration(request.user.access_key).delete_code(id)
request.user.notify("Scope Deleted", f"Scope {id} deleted successfully")
request.user.notify("Scope Deleted", f"Scope {id} deleted successfully",
"info")
if "." in id:
return redirect(to="control_library", id=".".join(id.split(".")[:-1]))
return redirect(to="libraries")
Expand All @@ -722,7 +731,8 @@ def enable_user(request, id):
the_user = models.User.objects.get(id=id)
API_Integration(request.user.access_key).enable_user(the_user.access_key)
request.user.notify(
"User Enabled", f"User {the_user.username} enabled successfully"
"User Enabled", f"User {the_user.username} enabled successfully",
"info"
)
return redirect(to="community")

Expand All @@ -739,7 +749,8 @@ def disable_user(request, id):

API_Integration(request.user.access_key).disable_user(the_user.access_key)
request.user.notify(
"User Disabled", f"User {the_user.username} disabled successfully"
"User Disabled", f"User {the_user.username} disabled successfully",
"info"
)
return redirect(to="community")

Expand All @@ -750,7 +761,8 @@ def enable_admin(request, id):
return HttpResponse(status=403)
the_user = models.User.objects.get(id=id)
API_Integration(request.user.access_key).enable_admin(the_user.access_key)
request.user.notify("Admin Enabled", f"User {the_user.username} is now an admin")
request.user.notify("Admin Enabled", f"User {the_user.username} is now an admin",
"info")
return redirect(to="community")


Expand All @@ -767,7 +779,8 @@ def disable_admin(request, id):

API_Integration(request.user.access_key).disable_admin(the_user.access_key)
request.user.notify(
"Admin Disabled", f"User {the_user.username} is no longer an admin"
"Admin Disabled", f"User {the_user.username} is no longer an admin",
"info"
)
return redirect(to="community")

Expand All @@ -786,6 +799,7 @@ def add_user(request):
request.user.notify(
"User Added",
f"User {user_form.cleaned_data.get('username')} added successfully",
"info"
)
return redirect(to="community")
else:
Expand Down

0 comments on commit f062cff

Please sign in to comment.