blob: f2990c7acb65b530990140f2bfaca7379eb39b2e [file] [log] [blame]
# Copyright 2011 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Module for the program host views."""
from google.appengine.ext import db
from django import http
from django.conf.urls.defaults import url as django_url
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext
from soc.logic.exceptions import AccessViolation
from soc.models.host import Host
from soc.views.base import SiteRequestHandler
from soc.views.helper import url_patterns
from soc.views.forms import ModelForm
DEF_DEVELOPER_ONLY = ugettext(
"You must be a developer to access other hosts' profile settings.")
DEF_NO_HOST = ugettext(
'You must be a host to access this page.')
class HostProfileForm(ModelForm):
"""Django form for the site settings."""
class Meta:
model = Host
fields = ['notify_slot_transfer']
class HostProfilePage(SiteRequestHandler):
"""View for the host profile."""
def djangoURLPatterns(self):
return [
django_url(r'^host/profile$', self, name='edit_host_profile'),
django_url(r'^host/profile/%s$' % url_patterns.USER, self,
name='edit_host_profile_linkid'),
]
def checkAccess(self, data, check, mutator):
check.isLoggedIn()
if data.is_developer:
mutator.hostFromKwargs()
return
# TODO(nathaniel): This shouldn't be necessary, but at the moment
# mutator.host() is not safe for calls when a user is logged in
# but does not have a user entity.
if not data.user:
raise AccessViolation(DEF_NO_HOST)
mutator.host()
if data.is_host:
if data.user.link_id != data.kwargs.get('link_id'):
raise AccessViolation(DEF_DEVELOPER_ONLY)
else:
raise AccessViolation(DEF_NO_HOST)
def templatePath(self):
return 'soc/host/base.html'
def context(self, data, check, mutator):
host_profile_form = HostProfileForm(data.POST or None, instance=data.host)
return {
'page_name': 'Host profile settings',
'forms': [host_profile_form],
}
def createOrUpdateHost(self, data):
"""Creates or Updates the host entity."""
host_profile_form = HostProfileForm(data.POST, instance=data.host)
if not host_profile_form.is_valid():
return None
if data.is_developer:
if data.host_user_key:
user_key = data.host_user_key
else:
user_key = data.user.key()
elif data.is_host:
user_key = data.user.key()
def create_or_update_host_txn():
if data.host:
# get the latest host entity
host = db.get(data.host.key())
host_profile_form.instance = host
host = host_profile_form.save(commit=True)
else:
user_entity = db.get(user_key)
host = host_profile_form.create(commit=True, parent=user_entity)
return host
return db.run_in_transaction(create_or_update_host_txn)
def post(self, data, check, mutator):
"""Handler for HTTP POST request."""
host = self.createOrUpdateHost(data)
if host:
link_id = data.kwargs.get('link_id')
if link_id:
kwargs = {'link_id': link_id}
return data.redirect.to('edit_host_profile_linkid', kwargs=kwargs)
else:
# TODO(nathaniel): redirection to self.
return data.redirect.to('edit_host_profile')
else:
# TODO(nathaniel): problematic self-call.
return self.get(data, check, mutator)