This repository has been archived by the owner on Feb 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathshare.py
73 lines (58 loc) · 2.71 KB
/
share.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/python
# Copyright 2012 Google Inc. All Rights Reserved.
#
# 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 distrib-
# uted 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
# specific language governing permissions and limitations under the License.
"""Share handler that deals with permission level and email distribution."""
__author__ = '[email protected] (Grace Muzny)'
import base_handler
import model
import perms
import users
import utils
from google.appengine.api import mail
SHARING_ROLES = {perms.Role.MAP_OWNER: 'owner',
perms.Role.MAP_EDITOR: 'editor',
perms.Role.MAP_VIEWER: 'viewer'}
class Share(base_handler.BaseHandler):
"""An interface for sharing maps between users."""
def Post(self, map_id, domain=None): # pylint: disable=unused-argument
"""Adds the recipient to the appropriate permission areas."""
map_object = model.Map.Get(map_id)
if map_object is None:
raise base_handler.Error(404, 'Map %r not found.' % map_id)
role = self.request.get('role')
recipient_email = self.request.get('recipient')
message = self.request.get('message', '')
# If these are empty or invalid, we shouldn't try to do anything.
if role not in SHARING_ROLES:
raise base_handler.Error(400, 'Invalid role parameter: %r.' % role)
if not utils.IsValidEmail(recipient_email):
raise base_handler.Error(
400, 'Invalid e-mail address: %r.' % recipient_email)
# Change the recipient's permission level as specified.
recipient = users.GetForEmail(recipient_email)
map_object.ChangePermissionLevel(role, recipient.id)
# Send the recipient an email.
self.SendPermissionChangeEmail(recipient_email, map_object, role, message)
self.response.set_status(201)
def SendPermissionChangeEmail(self, recipient_email, map_object,
role, message):
"""Sends recipient_email an email with info of map and permission level."""
email = users.GetCurrent().email
subject = map_object.title
url = (self.request.host_url + self.request.root_path + '/.maps/' +
map_object.id)
body = """
I've invited you to collaborate on the map "%s".
You can access the map at:
%s
You have %s access; please use this invitation within 30 days.
%s""" % (map_object.title, url, SHARING_ROLES[role], message)
mail.send_mail(email, recipient_email, subject, body)