Commit cc0c8889 authored by Benoit Alessandroni's avatar Benoit Alessandroni

Fixing some stuff on models and adding email notifications on models

parent 2eae1947
from django.db import models
from django.core.mail import send_mail
from djangoldp.models import Model
from django.conf import settings
from django_countries.fields import CountryField
from django.contrib.auth import get_user_model
from django.db.models.signals import post_save
from django.db.models.signals import post_save, m2m_changed
from django.dispatch import receiver
from djangoldp_conversation.models import Conversation, Message
from djangoldp_like.models import Like
from django.utils import timezone
from django.template import loader
class Language (Model):
......@@ -103,13 +107,15 @@ class Review (Model):
comment = models.TextField(verbose_name="Comment", blank=True)
status = models.CharField(max_length=32, choices=(('pending', 'Pending'), ('inappropriate', 'Inappropriate'), ('validated', 'Validated'), ('to_improve', 'Improvement required')), verbose_name="Resource status", blank=True, null=True)
reviewer = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.CASCADE, related_name='reviews')
created_on = models.DateTimeField(default=timezone.now)
class Meta:
ordering = ['-created_on']
auto_author='reviewer'
owner_field = 'reviewer'
anonymous_perms = ['view']
authenticated_perms = ['inherit', 'add']
serializer_fields=["@id", "reviewer", "resources", "comment", "status"]
serializer_fields=["@id", "reviewer", "resources", "comment", "status", "created_on"]
nested_fields=["reviewer", "resources"]
owner_perms = ['inherit', 'change', 'control', 'delete']
container_path = 'reviews/'
......@@ -148,8 +154,8 @@ class Resource (Model):
related = models.ManyToManyField("self", blank=True)
conversations = models.ManyToManyField(Conversation, blank=True, related_name='resources')
likes = models.ManyToManyField(Like, blank=True, related_name='resources')
reviews = models.ManyToManyField(Review, verbose_name="Associated review", blank=True, null=True, related_name='resources')
reviews = models.ManyToManyField(Review, verbose_name="Associated review", blank=True, related_name='resources')
class Meta:
auto_author='submitter'
owner_field = 'submitter'
......@@ -158,12 +164,18 @@ class Resource (Model):
"type", "submitter", "related", "likes", "reviews"]
serializer_fields=["@id", "name", "description", "skills", "author", "target", "uri", "publication_year", "format",\
"conversations", "steps", "language", "fields",\
"type", "submitter", "related", "likes", "reviews"]
"type", "submitter", "related", "likes", "reviews", "review"]
container_path = 'resources/'
rdf_type = 'coopstarter:resource'
anonymous_perms = ['view']
authenticated_perms = ['inherit', 'add']
def review(self):
try:
return self.reviews.all()[:1]
except:
return None
def dehydrate_country(self):
return self.country.name
......@@ -198,4 +210,42 @@ class Request (Model):
return self.country.name
def __str__(self):
return self.name
\ No newline at end of file
return self.name
@receiver(m2m_changed, sender=Resource.reviews.through)
def send_review_notification(sender, instance, **kwargs):
if instance.resources:
resource = instance.resources.first()
if resource and instance.status:
if instance.status == 'validated':
message = loader.render_to_string(
'resource_validation_notification.txt',
{
'review': instance,
'resource': resource
}
)
elif instance.status == 'to_improve':
message = loader.render_to_string(
'resource_improvement_notification.txt',
{
'review': instance,
'resource': resource
}
)
else:
message = loader.render_to_string(
'resource_refusal_notification.txt',
{
'review': instance,
'resource': resource
}
)
send_mail(
'The resource you submitted have been reviewed',
message,
resource.submitter.email,
[resource.submitter.email]
)
\ No newline at end of file
Hello {{resource.submitter.name}}
{{review.reviewer.name|safe}} proposed an improvement to the following resource you submitted.
Title: {{resource.name}}
Description: {{resource.description}}
URI: {{resource.uri}}
Improvement proposal detail: {{review.comment}}
Have a nice day,
The Knowledge Base Team.
\ No newline at end of file
Hello {{resource.submitter.name}}
{{review.reviewer.name|safe}} did refuse the following resource you submitted.
Title: {{resource.name}}
Description: {{resource.description}}
URI: {{resource.uri}}
Reason of the refusal: {{review.comment}}
Have a nice day,
The Knowledge Base Team.
\ No newline at end of file
Hello {{resource.submitter.name}}
{{review.reviewer.name|safe}} did validate the following resource you submitted.
Title: {{resource.name}}
Description: {{resource.description}}
URI: {{resource.uri}}
Have a nice day,
The Knowledge Base Team.
\ No newline at end of file
......@@ -6,5 +6,5 @@ class PendingResourcesViewSet(LDPViewSet):
def get_queryset(self, *args, **kwargs):
return super().get_queryset(*args, **kwargs)\
.filter(review=None, language__in=self.request.user.mentor_profile.languages.all(), fields__in=self.request.user.mentor_profile.fields.all())\
.filter(reviews=None, language__in=self.request.user.mentor_profile.languages.all(), fields__in=self.request.user.mentor_profile.fields.all())\
.exclude(submitter=self.request.user)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment