Skip to content

Commit

Permalink
Add user interface for URL and email redirects (#3523)
Browse files Browse the repository at this point in the history
* Add user interface for URL and email redirects

* Move help text to fix failing test

* Fix less than/greater than

* Fix lint error

* Fix test?
  • Loading branch information
willgearty authored Apr 29, 2022
1 parent f56ddc2 commit 816d26d
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 4 deletions.
4 changes: 2 additions & 2 deletions esp/esp/dbmail/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,9 @@ class PlainRedirect(models.Model):
A simple catch-all for mail redirection.
"""

original = models.CharField(max_length=512)
original = models.CharField(max_length=512, help_text='A real or custom email address name (e.g., "directors" or "splash"). Any emails to <original>@<yourdomain> will be redirected to the destination email address(es).')

destination = models.CharField(max_length=512)
destination = models.CharField(max_length=512, help_text='A comma-seperated list of one or more real email address(es) that will receive the redirected email(s)')

def __unicode__(self):
return '%s --> %s' % (self.original, self.destination)
Expand Down
16 changes: 16 additions & 0 deletions esp/esp/program/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@

from esp.users.models import StudentInfo, K12School
from esp.program.models import Program, ProgramModule, ClassFlag, ClassFlagType, ClassCategories
from esp.dbmail.models import PlainRedirect
from esp.utils.widgets import DateTimeWidget
from django import forms
from django.core import validators
from django.contrib.redirects.models import Redirect
from django.contrib.sites.models import Site
from form_utils.forms import BetterModelForm, BetterForm
from django.utils.safestring import mark_safe
from esp.tagdict import all_global_tags, tag_categories
Expand Down Expand Up @@ -423,6 +426,19 @@ class Meta:
model = ClassCategories
fields = ['category','symbol','seq']

class RedirectForm(forms.ModelForm):
class Meta:
model = Redirect
fields = ['old_path', 'new_path']

class PlainRedirectForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(PlainRedirectForm, self).__init__(*args, **kwargs)
self.fields['original'].help_text = 'A real or custom email address name (e.g., "directors" or "splash"). Any emails to <original>@%s will be redirected to the destination email address(es).' % Site.objects.get_current().domain
class Meta:
model = PlainRedirect
fields = ['original', 'destination']

class TagSettingsForm(BetterForm):
""" Form for changing global tags. """
def __init__(self, *args, **kwargs):
Expand Down
1 change: 1 addition & 0 deletions esp/esp/program/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
url(r'^manage/emails/?$', views.emails),
url(r'^manage/categoriesandflags/?(?P<section>[^/]*)/?$', views.categoriesandflags),
url(r'^manage/tags/?(?P<section>[^/]*)/?$', views.tags),
url(r'^manage/redirects/?(?P<section>[^/]*)/?$', views.redirects),
url(r'^manage/statistics/?$', views.statistics),
url(r'^manage/preview/?$', views.template_preview),
url(r'^manage/mergeaccounts/?$', esp.users.views.merge.merge_accounts),
Expand Down
82 changes: 80 additions & 2 deletions esp/esp/program/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@

from django.contrib.auth.decorators import login_required
from django.contrib.contenttypes.models import ContentType
from django.contrib.redirects.models import Redirect
from django.contrib.sites.models import Site
from django.db.models.query import Q
from django.db.models import Min
from django.db import transaction
Expand All @@ -64,7 +66,7 @@

from esp.program.modules.module_ext import ClassRegModuleInfo, StudentClassRegModuleInfo
from esp.program.models import Program, TeacherBio, RegistrationType, ClassSection, StudentRegistration, VolunteerOffer, RegistrationProfile, ClassCategories, ClassFlagType
from esp.program.forms import ProgramCreationForm, StatisticsQueryForm, TagSettingsForm, CategoryForm, FlagTypeForm
from esp.program.forms import ProgramCreationForm, StatisticsQueryForm, TagSettingsForm, CategoryForm, FlagTypeForm, RedirectForm, PlainRedirectForm
from esp.program.setup import prepare_program, commit_program
from esp.program.controllers.confirmation import ConfirmationEmailController
from esp.program.controllers.studentclassregmodule import RegistrationTypeController as RTC
Expand All @@ -74,7 +76,7 @@
from esp.middleware import ESPError
from esp.accounting.controllers import ProgramAccountingController, IndividualAccountingController
from esp.accounting.models import CybersourcePostback
from esp.dbmail.models import MessageRequest, TextOfEmail
from esp.dbmail.models import MessageRequest, TextOfEmail, PlainRedirect
from esp.mailman import create_list, load_list_settings, apply_list_settings, add_list_members
from esp.resources.models import ResourceType
from esp.tagdict.models import Tag
Expand Down Expand Up @@ -839,6 +841,82 @@ def tags(request, section=""):

return render_to_response('program/modules/admincore/tags.html', request, context)

@admin_required
def redirects(request, section=""):
"""
View that lets admins create/edit URL and email redirects
"""
context = {}
redirect_form = RedirectForm()
email_redirect_form = PlainRedirectForm()

if request.method == 'POST':
if request.POST.get('object') == 'redirect':
section = 'redirects'
if request.POST.get('command') == 'add': # New redirect
redirect_form = RedirectForm(request.POST)
if redirect_form.is_valid():
redirect = redirect_form.save(commit=False)
redirect.site = Site.objects.get_current()
redirect.save()
redirect_form = RedirectForm()
elif request.POST.get('command') == 'load': # Load existing redirect into form
redirect_id = request.POST.get('id')
redirects = Redirect.objects.filter(id = redirect_id)
if redirects.count() == 1:
redirect = redirects[0]
redirect_form = RedirectForm(instance = redirect)
elif request.POST.get('command') == 'edit': # Edit existing redirect
redirect_id = request.POST.get('id')
redirects = Redirect.objects.filter(id = redirect_id)
if redirects.count() == 1:
redirect = redirects[0]
redirect_form = RedirectForm(request.POST, instance = redirect)
if redirect_form.is_valid():
redirect_form.save()
redirect_form = RedirectForm()
elif request.POST.get('command') == 'delete': # Delete redirect
redirect_id = request.POST.get('id')
redirects = Redirect.objects.filter(id = redirect_id)
if redirects.count() == 1:
redirect = redirects[0]
redirect.delete()
elif request.POST.get('object') == 'email_redirect':
section = 'email_redirects'
if request.POST.get('command') == 'add': # New email redirect
email_redirect_form = PlainRedirectForm(request.POST)
if email_redirect_form.is_valid():
email_redirect_form.save()
email_redirect_form = PlainRedirectForm()
elif request.POST.get('command') == 'load': # Load existing email redirect into form
redirect_id = request.POST.get('id')
redirects = PlainRedirect.objects.filter(id = redirect_id)
if redirects.count() == 1:
redirect = redirects[0]
email_redirect_form = PlainRedirectForm(instance = redirect)
elif request.POST.get('command') == 'edit': # Edit existing email redirect
redirect_id = request.POST.get('id')
redirects = PlainRedirect.objects.filter(id = redirect_id)
if redirects.count() == 1:
redirect = redirects[0]
email_redirect_form = PlainRedirectForm(request.POST, instance = redirect)
if email_redirect_form.is_valid():
email_redirect_form.save()
email_redirect_form = PlainRedirectForm()
elif request.POST.get('command') == 'delete': # Delete email redirect
redirect_id = request.POST.get('id')
redirects = PlainRedirect.objects.filter(id = redirect_id)
if redirects.count() == 1:
redirect = redirects[0]
redirect.delete()
context['open_section'] = section
context['redirect_form'] = redirect_form
context['email_redirect_form'] = email_redirect_form
context['redirects'] = Redirect.objects.all()
context['email_redirects'] = PlainRedirect.objects.all()

return render_to_response('program/redirects.html', request, context)

@admin_required
def categoriesandflags(request, section=""):
"""
Expand Down
2 changes: 2 additions & 0 deletions esp/templates/program/manage_programs.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ <h2>Hello Admins!</h2>

<li><a href="/manage/categoriesandflags/">Manage class categories and flag types</a></li>

<li><a href="/manage/redirects/">Manage URL and email redirects</a></li>

<li><a href="/manage/tags/">Modify tag settings (generic miscellaneous settings, for experts only)</a></li>
</ul>
</p>
Expand Down
173 changes: 173 additions & 0 deletions esp/templates/program/redirects.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
{% extends "main.html" %}

{% block title %}
URL and Email Redirect Management
{% endblock %}

{% block stylesheets %}
{{ block.super }}
<link rel="stylesheet" href="/media/styles/forms.css" type="text/css" />
<link rel="stylesheet" href="/media/styles/expand_display.css" type="text/css" />
{% endblock %}

{% block xtrajs %}
<script type="text/javascript">
function deleteRedirect() {
if (confirm('Are you sure you would like to delete this redirect?')) {
return true;
}
return false;
}

function deleteEmailRedirect() {
if (confirm('Are you sure you would like to delete this email redirect?')) {
return true;
}
return false;
}
</script>
{% endblock %}

{% block content %}

<h1>
URL and Email Redirect Management
</h1>

<p>Welcome to the redirect management page. Here you can create, edit, and delete <b>URL redirects</b> which redirect users from one URL to another and
<b>email redirects</b> which redirect emails sent to a custom or real email address on your domain to one or more real email addresses on any domain.</p>

<div id="program_form">

{% if open_section == "redirects" %}
<button class="dsphead active">
<b>URL Redirects</b> (click to expand/contract)
</button>

<div class="dspcont active">
{% else %}
<button class="dsphead">
<b>URL Redirects</b> (click to expand/contract)
</button>

<div class="dspcont">
{% endif %}

<form method="post">
<input type="hidden" name="command" value="{% if redirect_form.instance.id %}edit{% else %}add{% endif %}" />
{% if redirect_form.instance.id %}
<input type="hidden" name="id" value="{{ redirect_form.instance.id }}" />
{% endif %}
<input type="hidden" name="object" value="redirect" />
<table align="center" cellpadding="0" cellspacing="0" width="100%">
<tr><th colspan="2" class="small">Add/Edit URL Redirects</th></tr>
{{ redirect_form }}
<tr><td colspan="2" align="center"><input class="fancybutton" type="submit" value="{% if redirect_form.instance.id %}Edit{% else %}Add{% endif %} URL Redirect" /></td></tr>
</table>
</form>

<br />

<table align="center" cellpadding="0" cellspacing="0" width="100%">
<tr><th colspan="4">URL Redirects</th></tr>
<tr>
<td><b>Old URL</b></td>
<td></td>
<td><b>New URL</b></td>
<td><b>Options</b></td>
</tr>
{% for redirect in redirects %}
<tr>
<td>{{ redirect.old_path }}</td>
<td>&#10230;</td>
<td>{{ redirect.new_path }}</td>
<td>
<center>
<a href="{{ redirect.old_path }}" target="_blank" class="btn btn-primary" />Test</a>
<form method="post">
<input type="hidden" name="id" value="{{ redirect.id }}" />
<input type="hidden" name="command" value="load" />
<input type="hidden" name="object" value="redirect" />
<input type="submit" value="Edit" class="btn btn-primary" />
</form>
<form method="post" onsubmit="return deleteRedirect()">
<input type="hidden" name="id" value="{{ redirect.id }}" />
<input type="hidden" name="command" value="delete" />
<input type="hidden" name="object" value="redirect" />
<input type="submit" value="Delete" class="btn btn-danger" />
</form>
</center>
</td>
</tr>
{% endfor %}
</table>
</div>

{% if open_section == "email_redirects" %}
<button class="dsphead active">
<b>Email Redirects</b> (click to expand/contract)
</button>

<div class="dspcont active">
{% else %}
<button class="dsphead">
<b>Email Redirects</b> (click to expand/contract)
</button>

<div class="dspcont">
{% endif %}

<form method="post">
<input type="hidden" name="command" value="{% if email_redirect_form.instance.id %}edit{% else %}add{% endif %}" />
{% if email_redirect_form.instance.id %}
<input type="hidden" name="id" value="{{ email_redirect_form.instance.id }}" />
{% endif %}
<input type="hidden" name="object" value="email_redirect" />
<table align="center" cellpadding="0" cellspacing="0" width="100%">
<tr><th colspan="2" class="small">Add/Edit Email Redirects</th></tr>
{{ email_redirect_form }}
<tr><td colspan="2" align="center"><input class="fancybutton" type="submit" value="{% if email_redirect_form.instance.id %}Edit{% else %}Add{% endif %} Email Redirect" /></td></tr>
</table>
</form>

<br />

<table align="center" cellpadding="0" cellspacing="0" width="100%">
<tr><th colspan="6">Email Redirects</th></tr>
<tr>
<td><b>Original Email Address</b></td>
<td></td>
<td><b>Destination Email Address(es)</b></td>
<td><b>Options</b></td>
</tr>
{% for er in email_redirects %}
<tr>
<td>{{ er.original }}</td>
<td>&#10230;</td>
<td>{{ er.destination }}</td>
<td>
<center>
<form method="post">
<input type="hidden" name="id" value="{{ er.id }}" />
<input type="hidden" name="command" value="load" />
<input type="hidden" name="object" value="email_redirect" />
<input type="submit" value="Edit" class="btn btn-primary" />
</form>
<form method="post" onsubmit="return deleteEmailRedirect()">
<input type="hidden" name="id" value="{{ er.id }}" />
<input type="hidden" name="command" value="delete" />
<input type="hidden" name="object" value="email_redirect" />
<input type="submit" value="Delete" class="btn btn-danger" />
</form>
</center>
</td>
</tr>
{% endfor %}
</table>
</div>

</div>

<script type="text/javascript" src="/media/scripts/expand_display.js"></script>

{% endblock %}

0 comments on commit 816d26d

Please sign in to comment.