Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create notifications #37

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
Profile,
Challenge,
Project,
Notification,
NotificationInstance,
)

admin.site.register(SocialLink)
Expand All @@ -16,3 +18,5 @@
admin.site.register(Profile)
admin.site.register(Challenge)
admin.site.register(Project)
admin.site.register(Notification)
admin.site.register(NotificationInstance)
44 changes: 44 additions & 0 deletions app/migrations/0014_notification_followers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Generated by Django 3.0.10 on 2020-09-19 22:58

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


class Migration(migrations.Migration):

dependencies = [
('contenttypes', '0002_remove_content_type_name'),
('app', '0013_auto_20200919_0144'),
]

operations = [
migrations.CreateModel(
name='Notification',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('message', models.CharField(help_text='Notification message.', max_length=255, verbose_name='Message')),
('object_id', models.PositiveIntegerField(null=True, verbose_name='Linked Item ID')),
('created', models.DateTimeField(auto_now_add=True, help_text='Date this notification was sent.', verbose_name='Creation Date')),
('content_type', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType', verbose_name='Linked Item Type')),
],
),
migrations.AddField(
model_name='challenge',
name='followers',
field=models.ManyToManyField(help_text='Followers of this project who will receive notifications about it.', related_name='followed_challenges', to='app.Profile', verbose_name='Followers'),
),
migrations.AddField(
model_name='project',
name='followers',
field=models.ManyToManyField(help_text='Followers of this project who will receive notifications about it.', related_name='followed_projects', to='app.Profile', verbose_name='Followers'),
),
migrations.CreateModel(
name='NotificationInstance',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('is_read', models.BooleanField(default=False, help_text='Whether this notification has been read.', verbose_name='Is Read')),
('notification', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='notifications', to='app.Notification')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='notifications', to='app.Profile')),
],
),
]
79 changes: 79 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ class Challenge(models.Model):
help_text="Creators of this challenge which can edit its properties. Removing yourself "
+ "will make it impossible to edit this challenge.",
)
followers = models.ManyToManyField(
Profile,
related_name="followed_challenges",
verbose_name="Followers",
help_text="Followers of this project who will receive notifications about it.",
)
created = models.DateField(
auto_now_add=True,
verbose_name="Creation Date",
Expand Down Expand Up @@ -193,6 +199,12 @@ def get_absolute_url(self):
def get_edit_url(self):
return reverse("challenge_edit", args=[self.pk])

def get_follow_url(self):
return reverse("challenge_follow", args=[self.pk])

def get_unfollow_url(self):
return reverse("challenge_unfollow", args=[self.pk])

def is_open(self):
if not self.start or not self.end:
return True
Expand Down Expand Up @@ -235,6 +247,12 @@ class Project(models.Model):
help_text="Creators of this project which can edit its properties. Removing yourself will "
+ "make it impossible for you to edit this project.",
)
followers = models.ManyToManyField(
Profile,
related_name="followed_projects",
verbose_name="Followers",
help_text="Followers of this project who will receive notifications about it.",
)
created = models.DateField(
auto_now_add=True, verbose_name="Creation Date", help_text="Date this project was created."
)
Expand Down Expand Up @@ -262,6 +280,12 @@ def get_absolute_url(self):
def get_edit_url(self):
return reverse("project_edit", args=[self.pk])

def get_follow_url(self):
return reverse("project_follow", args=[self.pk])

def get_unfollow_url(self):
return reverse("project_unfollow", args=[self.pk])

def __str__(self):
return self.name

Expand All @@ -270,3 +294,58 @@ def __str__(self):
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)


class Notification(models.Model):
fluix-dev marked this conversation as resolved.
Show resolved Hide resolved
message = models.CharField(
max_length=255, verbose_name="Message", help_text="Notification message."
)
content_type = models.ForeignKey(
ContentType, null=True, verbose_name="Linked Item Type", on_delete=models.CASCADE
)
object_id = models.PositiveIntegerField(null=True, verbose_name="Linked Item ID")
linked_item = GenericForeignKey()
created = models.DateTimeField(
auto_now_add=True,
verbose_name="Creation Date",
help_text="Date this notification was sent.",
)


class NotificationInstance(models.Model):
notification = models.ForeignKey(
Notification, related_name="notifications", on_delete=models.CASCADE
)
user = models.ForeignKey(Profile, related_name="notifications", on_delete=models.CASCADE)
is_read = models.BooleanField(
default=False, verbose_name="Is Read", help_text="Whether this notification has been read."
)

def get_link_url(self):
item = self.notification.linked_item
if item:
return item.get_absolute_url()
return "#"

@property
def message(self):
return self.notification.message

@property
def created(self):
return self.notification.created


@receiver(post_save, sender=Project)
@receiver(post_save, sender=Challenge)
def notify_followers(sender, instance, created, raw, using, update_fields, **kwargs):
if not instance.followers.exists():
return
message = "The %s %s has been updated." % (instance, sender.__name__)
notification = Notification.objects.create(
message=message,
object_id=instance.pk,
content_type=ContentType.objects.get_for_model(sender),
)
for user in instance.followers.all():
NotificationInstance.objects.create(user=user, notification=notification)
19 changes: 17 additions & 2 deletions app/static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ footer{
letter-spacing: 2px;
}

hr{
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
hr{
hr {

margin: 1.5em 0;
background-color: var(--grey-light);
border: none;
height: 2px;
}


/**
* Front page banner
Expand Down Expand Up @@ -200,6 +207,14 @@ footer{
font-weight: 300;
}

.notification-link{
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.notification-link{
.notification-link {

color: var(--cirrus-primary);
}

.notification-link:hover{
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.notification-link:hover{
.notification-link:hover {

color: var(--grey-mid);
}


/**
* Challenge
Expand Down Expand Up @@ -247,15 +262,15 @@ footer{
}

#profile-block.card {
padding: 15px;
padding: 15px;
}

.badges > a {
display: inline;
}

.badges .modal img {
height: 100px;
height: 100px;
}

.badges a img {
Expand Down
1 change: 1 addition & 0 deletions app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<a class="nav-dropdown-link btn-primary">Welcome, {{ request.user.username }}</a>
<ul class="dropdown-menu" role="menu">
<li role="menu-item"><a href="{{ url('home') }}">Dashboard</a></li>
<li role="menu-item"><a href="{{ url('notifications') }}">Notifications</a></li>
<li role="menu-item"><a href="{{ url('edit_profile') }}">Edit Profile</a></li>
<li role="menu-item"><a href="{{ url('logout') }}">Logout</a></li>
</ul>
Expand Down
5 changes: 5 additions & 0 deletions app/templates/initiative.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ <h2>{{ initiative.name }}</h2>
{% if initiative.can_edit(request.user) %}
<a class="btn btn-primary" href="{{ initiative.get_edit_url() }}"><i class="zmdi zmdi-edit"></i> Edit</a>
{% endif %}
{% if following %}
<a class="btn btn-primary" href="{{ initiative.get_unfollow_url() }}"><i class="zmdi zmdi-eye-off"></i> Unfollow</a>
{% else %}
<a class="btn btn-primary" href="{{ initiative.get_follow_url() }}"><i class="zmdi zmdi-eye"></i> Follow</a>
{% endif %}
{% endblock %}
</div>
<div class="row">
Expand Down
24 changes: 24 additions & 0 deletions app/templates/notifications.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends "base.html" %}

{% block head %}
<title>{{ profile.username }}'s Notifications | HackItForward</title>
{% endblock %}

{% block body %}
<div class="content">
<div class="row">
<div class="col-12">
<h2 class="header-margin">Notifications</h2>
</div>
<div class="card col-12" id="challenge-frame">
{% for notif in notifications %}
{% if notif != notifications[0] and notif.notification.linked_item.__str__() != notifications[0].notification.linked_item.__str__() %}
fluix-dev marked this conversation as resolved.
Show resolved Hide resolved
<hr>
{% endif %}
<a class="notification-link font-alt font-bold" href="{{ notif.get_link_url() }}">{{ notif.notification.linked_item.__str__() }}</a>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need the __str__()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wait yeah you're right

<span class="font-light">{{ notif.message }}</span><br>
{% endfor %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not high priority but could add a message for when there are no notifications.

</div>
</div>
</div>
{% endblock %}
4 changes: 4 additions & 0 deletions app/templates/userhome.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

{% block body %}
<div class="content">

<!-- Project -->
fluix-dev marked this conversation as resolved.
Show resolved Hide resolved
<div class="row">
<div class="col-12">
{% if profile != request.user.profile %}
Expand Down Expand Up @@ -58,6 +60,8 @@ <h2 class="header-margin">Your Projects</h2>
{% endif %}
</div>
<br>

<!-- Profile -->
<div class="row">
<div class="col-12">
{% if profile != request.user.profile %}
Expand Down
13 changes: 13 additions & 0 deletions app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@
# TODO: Rename this to fit in line with project and challenge style.
path("home/", views.UserView.as_view(), name="home"),
path("home/<int:pk>", views.UserView.as_view(), name="user"),
path("notifications/", views.NotificationListView.as_view(), name="notifications"),
path("profile/edit", views.EditProfileView.as_view(), name="edit_profile"),
path("challenge/create/", views.ChallengeCreateView.as_view(), name="challenge_create"),
path("challenge/<int:pk>/", views.ChallengeView.as_view(), name="challenge"),
path("challenge/<int:pk>/edit/", views.ChallengeUpdateView.as_view(), name="challenge_edit"),
path(
"challenge/<int:pk>/follow/", views.ChallengeFollowView.as_view(), name="challenge_follow"
),
path(
"challenge/<int:pk>/unfollow/",
views.ChallengeUnFollowView.as_view(),
name="challenge_unfollow",
),
path("project/create/", views.ProjectCreateView.as_view(), name="project_create"),
path(
"project/create/<int:pk>/",
Expand All @@ -22,4 +31,8 @@
),
path("project/<int:pk>/", views.ProjectView.as_view(), name="project"),
path("project/<int:pk>/edit/", views.ProjectUpdateView.as_view(), name="project_edit"),
path("project/<int:pk>/follow/", views.ProjectFollowView.as_view(), name="project_follow"),
path(
"project/<int:pk>/unfollow/", views.ProjectUnFollowView.as_view(), name="project_unfollow"
),
]
Loading