Skip to content

Commit

Permalink
Rebase and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Negron authored and Raymond Negron committed Mar 28, 2023
1 parent 93ef0ab commit b1b2e13
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 46 deletions.
5 changes: 2 additions & 3 deletions estela-api/api/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,19 @@ class BaseViewSet(viewsets.GenericViewSet):


class NotificationsHandler(viewsets.GenericViewSet):

def get_users_email(self, project):
email_list = []
for user in project.users.all():
email_list.append(user.email)

return email_list

def save_notfication(self, user, message, project):
def save_notification(self, user, message, project):
email_list = self.get_users_email(project)
email_body = f"""
Dear User
{message}
Estela Notification Service.
"""
notification = Notification(
Expand Down
9 changes: 5 additions & 4 deletions estela-api/api/views/cronjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def create(self, request, *args, **kwargs):

# Send action notification
project = get_object_or_404(Project, pid=self.kwargs["pid"])
self.save_notfication(
self.save_notification(
user=request.user,
message=f"{request.user} has scheduled a new job at {spider.name} spider",
project=project,
Expand Down Expand Up @@ -127,22 +127,23 @@ def update(self, request, *args, **kwargs):
)
schedule_changed = (
True
if "schedule" in request.data or instance.schedule != request.data["schedule"]
if "schedule" in request.data
or instance.schedule != request.data["schedule"]
else False
)

# Send action notification
project = get_object_or_404(Project, pid=self.kwargs["pid"])
if status_changed:
message = f"{request.user.get_username()} has updated the status of a cronjob at {instance.spider.name} spider: {request.data['status']}"
self.save_notfication(
self.save_notification(
user=request.user,
message=message,
project=project,
)
if schedule_changed:
message = f"{request.user.get_username()} has updated the schedule of a cronjob at {instance.spider.name} spider: {request.data['schedule']}"
self.save_notfication(
self.save_notification(
user=request.user,
message=message,
project=project,
Expand Down
4 changes: 2 additions & 2 deletions estela-api/api/views/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def create(self, request, *args, **kwargs):
)

# Send action notification
self.save_notfication(
self.save_notification(
user=user,
message=f"{request.user} has made a new deployment for {project.name} Project.",
project=project,
Expand Down Expand Up @@ -101,7 +101,7 @@ def update(self, request, *args, **kwargs):

# Send action notification
project = get_object_or_404(Project, pid=self.kwargs["pid"])
self.save_notfication(
self.save_notification(
user=request.user,
message=f"{request.user} has made an update for {project.name} Project.",
project=project,
Expand Down
2 changes: 1 addition & 1 deletion estela-api/api/views/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def create(self, request, *args, **kwargs):

# Send action notification
project = get_object_or_404(Project, pid=self.kwargs["pid"])
self.save_notfication(
self.save_notification(
user=request.user,
message=f"{request.user} has created a new job for {spider.name} spider.",
project=project,
Expand Down
13 changes: 7 additions & 6 deletions estela-api/api/views/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ def perform_create(self, serializer):
requests_data_size=0,
logs_data_size=0,
)
# project = get_object_or_404(Project, pid=instance.pid)
self.save_notfication(
self.save_notification(
user=self.request.user,
message=f"{self.request.user} created a new Project: {instance.name}.",
project=instance,
Expand Down Expand Up @@ -99,7 +98,7 @@ def update(self, request, *args, **kwargs):
instance.name = name
# Send action notification
project = get_object_or_404(Project, pid=self.kwargs["pid"])
self.save_notfication(
self.save_notification(
user=request.user,
message=f"{request.user} updated the Project: {instance.name}.",
project=project,
Expand Down Expand Up @@ -130,7 +129,9 @@ def update(self, request, *args, **kwargs):
instance.users.add(
user, through_defaults={"permission": permission}
)
notification_message = f"{request.user} changed {user.get_username()}'s role of {instance.name} project, to {permission}.",
notification_message = (
f"{request.user} changed {user.get_username()}'s role of {instance.name} project, to {permission}.",
)
else:
raise ParseError({"error": "Action not supported."})
else:
Expand All @@ -143,7 +144,7 @@ def update(self, request, *args, **kwargs):
if notification_message:
# Send action notification
project = get_object_or_404(Project, pid=self.kwargs["pid"])
self.save_notfication(
self.save_notification(
user=request.user,
message=notification_message,
project=project,
Expand All @@ -158,7 +159,7 @@ def update(self, request, *args, **kwargs):
def destroy(self, request, *args, **kwargs):
instance = self.get_object()
project = get_object_or_404(Project, pid=self.kwargs["pid"])
self.save_notfication(
self.save_notification(
user=self.request.user,
message=f"{self.request.user} deleted Project: {instance.name}.",
project=project,
Expand Down
30 changes: 0 additions & 30 deletions estela-api/core/migrations/0024_notification.py

This file was deleted.

65 changes: 65 additions & 0 deletions estela-api/core/migrations/0025_notification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Generated by Django 3.1.14 on 2023-02-20 02:22

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


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("core", "0024_userprofile"),
]

operations = [
migrations.CreateModel(
name="Notification",
fields=[
(
"nid",
models.AutoField(
help_text="A unique integer value identifying each notification",
primary_key=True,
serialize=False,
),
),
(
"message",
models.CharField(
help_text="Notifications message.", max_length=1000
),
),
(
"notify_to",
models.CharField(
help_text="The direction where the notification will redirect.",
max_length=100,
),
),
(
"seen",
models.BooleanField(
default=False, help_text="Whether the notification was seen."
),
),
(
"created_at",
models.DateTimeField(
auto_now_add=True, help_text="Notification send date."
),
),
(
"user",
models.ForeignKey(
help_text="User whose this notification belongs to.",
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
options={
"ordering": ["-created_at"],
},
),
]
2 changes: 2 additions & 0 deletions estela-api/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ def create_user_profile(sender, instance, created, **kwargs):
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.userprofile.save()


class Notification(models.Model):
nid = models.AutoField(
primary_key=True,
Expand Down

0 comments on commit b1b2e13

Please sign in to comment.