Skip to content

Commit

Permalink
Add readd user functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
sounak98 committed Aug 14, 2019
1 parent 8667811 commit afa856a
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 55 deletions.
134 changes: 83 additions & 51 deletions gsoc/common/utils/build_tasks.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
import json
import uuid
import urllib.parse

from django.utils import timezone
from django.conf import settings

from gsoc.models import UserProfile, GsocYear, BlogPostDueDate, Scheduler
from gsoc.models import UserProfile, GsocYear, BlogPostDueDate, Scheduler, ReaddUser
from gsoc.common.utils.tools import build_send_mail_json


def build_pre_blog_reminders(builder):
try:
data = json.loads(builder.data)
due_date = BlogPostDueDate.objects.get(pk=data['due_date_pk'])
due_date = BlogPostDueDate.objects.get(pk=data["due_date_pk"])
gsoc_year = GsocYear.objects.first()
profiles = UserProfile.objects.filter(gsoc_year=gsoc_year, role=3).all()
categories = (
(0, 'Weekly Check-In'),
(1, 'Blog Post'),
)
categories = ((0, "Weekly Check-In"), (1, "Blog Post"))
category = categories[due_date.category][1]
for profile in profiles:
if profile.current_blog_count is not 0 and not (profile.hidden or
profile.reminder_disabled):
if profile.current_blog_count is not 0 and not (
profile.hidden or profile.reminder_disabled
):
template_data = {
'current_blog_count': profile.current_blog_count,
'type': due_date.category,
'due_date': due_date.date.strftime('%d %B %Y')
}
"current_blog_count": profile.current_blog_count,
"type": due_date.category,
"due_date": due_date.date.strftime("%d %B %Y"),
}

scheduler_data = build_send_mail_json(profile.user.email,
template='pre_blog_reminder.html',
subject=f'Reminder for {category}',
template_data=template_data)
scheduler_data = build_send_mail_json(
profile.user.email,
template="pre_blog_reminder.html",
subject=f"Reminder for {category}",
template_data=template_data,
)

s = Scheduler.objects.create(command='send_email',
data=scheduler_data)
s = Scheduler.objects.create(command="send_email", data=scheduler_data)
return None
except Exception as e:
return str(e)
Expand All @@ -42,71 +44,73 @@ def build_post_blog_reminders(builder):
try:
data = json.loads(builder.data)
last_due_date = BlogPostDueDate.objects.last()
due_date = BlogPostDueDate.objects.get(pk=data['due_date_pk'])
due_date = BlogPostDueDate.objects.get(pk=data["due_date_pk"])
if due_date == last_due_date:
blogs_count = 0
else:
blogs_count = 1

categories = (
(0, 'Weekly Check-In'),
(1, 'Blog Post'),
)
categories = ((0, "Weekly Check-In"), (1, "Blog Post"))
category = categories[due_date.category][1]

gsoc_year = GsocYear.objects.first()
profiles = UserProfile.objects.filter(gsoc_year=gsoc_year, role=3).all()
for profile in profiles:
if profile.current_blog_count > blogs_count and not (profile.hidden or
profile.reminder_disabled):
if profile.current_blog_count > blogs_count and not (
profile.hidden or profile.reminder_disabled
):
suborg = profile.suborg_full_name
mentors = UserProfile.objects.filter(suborg_full_name=suborg, role=2)
suborg_admins = UserProfile.objects.filter(suborg_full_name=suborg, role=1)
suborg_admins = UserProfile.objects.filter(
suborg_full_name=suborg, role=1
)

activation_date = builder.activation_date.date()

if activation_date - due_date.date == timezone.timedelta(days=1):
student_template = 'first_post_blog_reminder_student.html'
student_template = "first_post_blog_reminder_student.html"

elif activation_date - due_date.date == timezone.timedelta(days=3):
student_template = 'second_post_blog_reminder_student.html'
student_template = "second_post_blog_reminder_student.html"

mentors_emails = ['gsoc-admins@python.org']
mentors_emails = ["gsoc-admins@python.org"]
mentors_emails.extend([_.user.email for _ in mentors])
mentors_emails.extend([_.user.email for _ in suborg_admins])

mentors_template_data = {
'student_username': profile.user.username,
'student_email': profile.user.email,
'suborg_name': profile.suborg_full_name.suborg_name,
'due_date': due_date.date.strftime('%d %B %Y'),
'current_blog_count': profile.current_blog_count
}
"student_username": profile.user.username,
"student_email": profile.user.email,
"suborg_name": profile.suborg_full_name.suborg_name,
"due_date": due_date.date.strftime("%d %B %Y"),
"current_blog_count": profile.current_blog_count,
}

scheduler_data_mentors = build_send_mail_json(
mentors_emails,
template='post_blog_reminder_mentors.html',
subject=f'{category} missed by a Student of your Sub-Org',
template_data=mentors_template_data
)
template="post_blog_reminder_mentors.html",
subject=f"{category} missed by a Student of your Sub-Org",
template_data=mentors_template_data,
)

Scheduler.objects.create(command='send_email',
data=scheduler_data_mentors)
Scheduler.objects.create(
command="send_email", data=scheduler_data_mentors
)

student_template_data = {
'current_blog_count': profile.current_blog_count,
'due_date': due_date.date.strftime('%d %B %Y')
}
"current_blog_count": profile.current_blog_count,
"due_date": due_date.date.strftime("%d %B %Y"),
}

scheduler_data_student = build_send_mail_json(
profile.user.email,
template=student_template,
subject=f'Reminder for {category}',
template_data=student_template_data
)
subject=f"Reminder for {category}",
template_data=student_template_data,
)

Scheduler.objects.create(command='send_email',
data=scheduler_data_student)
Scheduler.objects.create(
command="send_email", data=scheduler_data_student
)
return None
except Exception as e:
return str(e)
Expand All @@ -117,7 +121,35 @@ def build_revoke_student_perms(builder):
gsoc_year = GsocYear.objects.first()
profiles = UserProfile.objects.filter(gsoc_year=gsoc_year, role=3).all()
for profile in profiles:
Scheduler.objects.create(command='revoke_student_permissions',
data=profile.user.id)
Scheduler.objects.create(
command="revoke_student_permissions", data=profile.user.id
)
except Exception as e:
return str(e)


def build_remove_user_details(builder):
try:
gsoc_year = GsocYear.objects.first()
profiles = UserProfile.objects.filter(
gsoc_year=gsoc_year, role__in=[1, 2, 3]
).all()
for profile in profiles:
email = profile.user.email
profile.user.email = None
profile.user.save()
_uuid = uuid.uuid4()
ReaddUser.objects.create(user=profile.user, uuid=_uuid)
template_data = {
# TODO: change this after the view is created
"link": settings.INETLOCATION + "use reverse here"
}
scheduler_data = build_send_mail_json(
email,
template="readd_email.html",
subject="Your personal details have been removed from our database",
template_data=template_data,
)
Scheduler.objects.create(command='send_email' data=scheduler_data)
except Exception as e:
return str(e)
29 changes: 29 additions & 0 deletions gsoc/migrations/0051_auto_20190814_1454.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 2.1.10 on 2019-08-14 14:54

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),
('gsoc', '0050_auto_20190809_0529'),
]

operations = [
migrations.CreateModel(
name='ReaddUser',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('uuid', models.CharField(max_length=100)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.AlterField(
model_name='builder',
name='category',
field=models.CharField(choices=[('build_pre_blog_reminders', 'build_pre_blog_reminders'), ('build_post_blog_reminders', 'build_post_blog_reminders'), ('build_revoke_student_perms', 'build_revoke_student_perms'), ('build_remove_user_details', 'build_remove_user_details')], max_length=40),
),
]
10 changes: 10 additions & 0 deletions gsoc/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,15 @@ def send_review(self):
data=scheduler_data)


class ReaddUser(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
uuid = models.CharField(max_length=100)

def readd_user_details(self, email):
self.user.email = email
self.user.save()


class UserProfileManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(hidden=False)
Expand Down Expand Up @@ -428,6 +437,7 @@ class Builder(models.Model):
('build_pre_blog_reminders', 'build_pre_blog_reminders'),
('build_post_blog_reminders', 'build_post_blog_reminders'),
('build_revoke_student_perms', 'build_revoke_student_perms'),
('build_remove_user_details', 'build_remove_user_details'),
)

category = models.CharField(max_length=40, choices=categories)
Expand Down
4 changes: 4 additions & 0 deletions gsoc/templates/email/readd_email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Your sensitive information has been removed from our database. <br/>
<br/>
If you want to be subscribed for further notifications via email you
can subscribe using this <a href="{{link}}">link</a>.
15 changes: 15 additions & 0 deletions gsoc/templates/readd.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "base.html" %}

{% block title %}
Page Not Found
{% endblock %}

{% block content %}
<div class="content">
{% if success %}
<h2 class="content-head is-center">You are subscribed</h2>
{% else %}
<h2 class="content-head is-center">Subscribing failed</h2>
{% endif %}
</div>
{% endblock %}
11 changes: 8 additions & 3 deletions gsoc/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
url(r'^sitemap.xml', sitemap, {
'sitemaps': {
'blog_sitemaps': sitemaps.BlogListSitemap,
}
}),
}
}),
url(r'^robots.txt', TemplateView.as_view(template_name="robots.txt",
content_type="text/plain"), name="robots_file"),
content_type="text/plain"), name="robots_file"),
url(r'^favicon.ico', RedirectView.as_view(url=settings.STATIC_URL + 'favicon.ico')),
]

Expand Down Expand Up @@ -85,3 +85,8 @@
urlpatterns += [
url(r'^upload/', gsoc.views.upload_file)
]

# Readd user details
urlpatterns += [
url(r'^readd/(?P<uuid>[\w-]+)/', gsoc.views.readd_users, name='readd_users')
]
24 changes: 23 additions & 1 deletion gsoc/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .common.utils.memcached_stats import MemcachedStats
from .forms import ProposalUploadForm
from .models import (RegLink, ProposalTextValidator, Comment, ArticleReview,
GsocYear)
GsocYear, ReaddUser)

import io
import os
Expand Down Expand Up @@ -416,3 +416,25 @@ def publish_article(request, article_id):
else:
messages.error(request, 'User does not have permission to publish article')
return redirect(reverse('{}:article-detail'.format(a.app_config.namespace), args=[a.slug]))


def readd_users(request, uuid):
if request.method == 'GET':
readds = ReaddUser.objects.filter(uuid=uuid)
email = request.GET.get('email')
context = {
"success": False
}
if len(readds) > 0:
readd = readds.first()
if email:
readd.readd_user_details(email)
context = {
"success": True
}
else:
messages.error('Please provide your email')
else:
messages.error('Incorrect token, please use the correct token')

return shortcuts.render(request, "readd.html", context)
Binary file modified project.db
Binary file not shown.

0 comments on commit afa856a

Please sign in to comment.