blob: ab8236c6a380f8c47ff4b6b9fcdc9df1612ac3c2 [file] [log] [blame]
# Copyright 2014 the Melange authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.
"""Module containing the notification related views."""
from django import forms as django_forms
from django import http
from django.utils import translation
from melange.logic import profile as profile_logic
from melange.models import profile as profile_model
from melange.request import access
from melange.request import exception
from soc.modules.gsoc.views import forms as gsoc_forms
from soc.views import base
from soc.views.helper import url_patterns
# TODO(daniel): it cannot work like that because it comes from GSoC.
# The question is what to do with it (where to move it?) if tabs do not
# make sense at all for Code-in.
from summerofcode.templates import tabs
_NOTIFICATION_SETTINGS_PAGE_NAME = translation.ugettext('Notification settings')
_ORG_CONNECTIONS_HELP_TEXT = translation.ugettext('TODO(daniel): complete')
_ORG_PROPOSAL_COMMENTS_HELP_TEXT = translation.ugettext(
'Check this box if you want to be notified of new comments once they are '
'sent for proposals to organizations for which you are a mentor or '
'organization administrator.')
_ORG_NEW_PROPOSALS_HELP_TEXT = translation.ugettext(
'Check this box if you want to be notified of new proposals once '
'they are submitted to one the organizations for which you are a mentor. '
'or organization administrator.')
_USER_CONNECTIONS_HELP_TEXT = translation.ugettext('TODO(daniel): complete')
_ORG_CONNECTIONS_LABEL = translation.ugettext(
'Notify of organization connections')
_ORG_PROPOSAL_COMMENTS_LABEL = translation.ugettext(
'Notify of new comments')
_ORG_NEW_PROPOSALS_LABEL = translation.ugettext(
'Notify of new proposals')
_ORG_UPDATED_PROPOSALS_LABEL = translation.ugettext(
'Notify of proposal updates')
_USER_CONNECTIONS_LABEL = translation.ugettext(
'Notify of user connections')
'org_connections', 'org_new_proposals', 'user_connections',
# TODO(daniel): this form mustn't inherit from GSoC form
class NotificationSettingsForm(gsoc_forms.GSoCModelForm):
"""Django form to show specific fields for notification settings.
Upon creation the form can be customized so as to accommodate
actual use cases.
org_connections = django_forms.BooleanField(
required=False, label=_ORG_CONNECTIONS_LABEL,
user_connections = django_forms.BooleanField(
required=False, label=_USER_CONNECTIONS_LABEL,
org_new_proposals = django_forms.BooleanField(
required=False, label=_ORG_NEW_PROPOSALS_LABEL,
org_proposal_comments = django_forms.BooleanField(
required=False, label=_ORG_PROPOSAL_COMMENTS_LABEL,
def getNotificationSettingsProperties(self):
"""Returns properties of the notification settings that were submitted
in this form.
A dict mapping notification settings properties to the
corresponding values.
return self._getPropertiesForFields(
def _getNotificationSettingsForm(**kwargs):
"""Returns a new instance of NotificationSettingsForm.
A new instance of NotificationSettingsForm.
form = NotificationSettingsForm(**kwargs)
# TODO(daniel): these fields have to be supported rather than removed :-P
del form.fields['org_connections']
del form.fields['user_connections']
return form
class NotificationSettingsPage(base.RequestHandler):
"""View to start connections with users as organization administrators."""
access_checker = access.HAS_PROFILE_ACCESS_CHECKER
def __init__(self, initializer, linker, renderer, error_handler,
url_pattern_constructor, url_names, template_path):
"""Initializes a new instance of the request handler for the specified
initializer: Implementation of initialize.Initializer interface.
linker: Instance of links.Linker class.
renderer: Implementation of render.Renderer interface.
error_handler: Implementation of error.ErrorHandler interface.
Implementation of url_patterns.UrlPatternConstructor.
url_names: Instance of url_names.UrlNames.
template_path: The path of the template to be used.
super(NotificationSettingsPage, self).__init__(
initializer, linker, renderer, error_handler)
self.url_pattern_constructor = url_pattern_constructor
self.url_names = url_names
self.template_path = template_path
def djangoURLPatterns(self):
"""See base.RequestHandler.djangoURLPatterns for specification."""
return [
r'profile/notifications/%s$' % url_patterns.PROGRAM,
self, name=self.url_names.PROFILE_NOTIFICATION_SETTINGS)
def templatePath(self):
"""See base.RequestHandler.templatePath for specification."""
return self.template_path
def context(self, data, check, mutator):
"""See base.RequestHandler.context for specification."""
form_data = data.ndb_profile.notification_settings.to_dict()
# TODO(daniel): different form should be passed for different types of users
form = _getNotificationSettingsForm(data=data.POST or form_data)
return {
'forms': [form],
'error': bool(form.errors),
'tabs': tabs.profileTabs(data, tabs.NOTIFICATION_SETTINGS_TAB_ID),
def post(self, data, check, mutator):
"""See for specification."""
form = _getNotificationSettingsForm(data=data.POST)
if form.is_valid():
properties = form.getNotificationSettingsProperties()
profile_properties = {
result = profile_logic.editProfile(
data.ndb_profile.key, profile_properties)
if not result:
raise exception.BadRequest(message=result.extra)
url = self.linker.program(
data.program, self.url_names.PROFILE_NOTIFICATION_SETTINGS)
return http.HttpResponseRedirect(url + '?verified=True')
# TODO(nathaniel): problematic self-call.
return self.get(data, check, mutator)