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 all 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)
15 changes: 15 additions & 0 deletions app/static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ footer {
font-size: 1.5rem;
}

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;
}

.content {
margin-bottom: 0px;
}
Expand Down Expand Up @@ -212,6 +219,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);
}



/**
Expand Down
1 change: 1 addition & 0 deletions app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,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] %}
<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 %}
1 change: 1 addition & 0 deletions app/templates/userhome.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ <h2 class="header-margin">Your Projects</h2>
{% endif %}
</div>
<br>

<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 @@ -15,10 +15,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 @@ -27,4 +36,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"
),
]
45 changes: 43 additions & 2 deletions app/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from app.forms import ProfileUpdateForm
from app.models import Challenge, Profile, Project, SocialLinkAttachement, Tag
from app.models import Challenge, NotificationInstance, Profile, Project, SocialLinkAttachement, Tag

from django.core.exceptions import PermissionDenied
from django.contrib.auth import login
Expand All @@ -15,8 +15,9 @@
from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse, reverse_lazy
from django.views.generic.base import TemplateView, ContextMixin
from django.views.generic.detail import DetailView
from django.views.generic.detail import DetailView, SingleObjectMixin
from django.views.generic.edit import CreateView, FormView, UpdateView
from django.views.generic.list import ListView


class IndexView(TemplateView):
Expand Down Expand Up @@ -86,6 +87,15 @@ def get_context_data(self, **kwargs):
return context


class NotificationListView(LoginRequiredMixin, ListView):
template_name = "notifications.html"
model = NotificationInstance
context_object_name = "notifications"

def get_queryset(self):
return super().get_queryset().filter(user=self.request.user.profile)


class EditProfileView(LoginRequiredMixin, UpdateView):
template_name = "edit_profile.html"
form_class = ProfileUpdateForm
Expand Down Expand Up @@ -116,6 +126,7 @@ def get_context_data(self, **kwargs):
self.initiative = self.classname.objects.get(pk=pk)

context["initiative"] = self.initiative
context["following"] = self.initiative.followers.filter(user=self.request.user).exists()
context["links"] = SocialLinkAttachement.objects.filter(
object_id=pk, content_type=ContentType.objects.get_for_model(self.classname)
)
Expand All @@ -132,6 +143,20 @@ def get_context_data(self, **kwargs):
return context


class FollowMixin(DetailView):
def get(self, *args, **kwargs):
obj = self.get_object()
obj.followers.add(self.request.user.profile)
return redirect(obj.get_absolute_url())


class UnFollowMixin(DetailView):
def get(self, *args, **kwargs):
obj = self.get_object()
obj.followers.remove(self.request.user.profile)
return redirect(obj.get_absolute_url())


class ChallengeFormView(GenericFormMixin):
model = Challenge
fields = ["name", "image", "description", "creators", "start", "end", "tags"]
Expand Down Expand Up @@ -169,6 +194,14 @@ def get_context_data(self, **kwargs):
return context


class ChallengeFollowView(FollowMixin):
model = Challenge


class ChallengeUnFollowView(UnFollowMixin):
model = Challenge


class ProjectFormView(GenericFormMixin):
model = Project
fields = ["name", "image", "description", "creators", "contributors", "tags"]
Expand Down Expand Up @@ -212,3 +245,11 @@ def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["time_labels"] = [{"label": "Creation Time", "time": self.initiative.created}]
return context


class ProjectFollowView(FollowMixin):
model = Project


class ProjectUnFollowView(UnFollowMixin):
model = Project