-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathadd_note.py
126 lines (111 loc) · 4.9 KB
/
add_note.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Copyright 2015 Google Inc.
#
# 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.
from google.appengine.api import datastore_errors
import create
from model import *
from photo import create_photo, PhotoError
from utils import *
from detect_spam import SpamDetector
import extend
import subscribe
import utils
from django.utils.translation import ugettext as _
from urlparse import urlparse
# how many days left before we warn about imminent expiration.
# Make this at least 1.
EXPIRY_WARNING_THRESHOLD = 7
class Handler(BaseHandler):
def get(self):
# Check the request parameters.
if not self.params.id:
return self.error(404, _('No person id was specified.'))
try:
person = Person.get(self.repo, self.params.id)
# TODO(ichikawa) Consider removing this "except" clause.
# I don't think ValueError is thrown here.
except ValueError:
return self.error(404,
_("This person's entry does not exist or has been deleted."))
if not person:
return self.error(404,
_("This person's entry does not exist or has been deleted."))
# Render the page.
self.render('add_note.html', person=person)
def post(self):
"""Post a note in person's record view page"""
try:
create.validate_note_data(
config=self.config,
status=self.params.status,
author_name=self.params.author_name,
author_email=self.params.author_email,
author_made_contact=self.params.author_made_contact,
text=self.params.text)
except create.CreationError as e:
return self.error(400, e.user_readable_message)
person = Person.get(self.repo, self.params.id)
if person.notes_disabled:
return self.error(
400, _('The author has disabled status updates '
'on this record.'))
# If a photo was uploaded, create and store a new Photo entry and get
# the URL where it's served; otherwise, use the note_photo_url provided.
photo, photo_url = (None, self.params.note_photo_url)
if self.params.note_photo is not None:
try:
photo, photo_url = create_photo(
self.params.note_photo, self.repo,
self.transitionary_get_url)
except PhotoError, e:
return self.error(400, e.message)
photo.put()
try:
note = create.create_note(
repo=self.repo,
person=person,
config=self.config,
user_ip_address=self.request.remote_addr,
status=self.params.status,
source_date=get_utcnow(),
author_name=self.params.author_name,
author_email=self.params.author_email,
author_phone=self.params.author_phone,
author_made_contact=bool(self.params.author_made_contact),
note_photo=photo,
note_photo_url=photo_url,
text=self.params.text,
email_of_found_person=self.params.email_of_found_person,
phone_of_found_person=self.params.phone_of_found_person,
last_known_location=self.params.last_known_location,
validate_data=False)
except create.FlaggedNoteException as e:
return self.redirect(
'/post_flagged_note', id=e.note.get_record_id(),
author_email=e.note.author_email, repo=self.repo)
# Update the Person based on the Note.
if person:
person.update_from_note(note)
# Send notification to all people
# who subscribed to updates on this person
subscribe.send_notifications(self, person, [note])
# write the updated person record to datastore
db.put(person)
# If user wants to subscribe to updates, redirect to the subscribe page
if self.params.subscribe:
return self.redirect('/subscribe',
id=person.record_id,
subscribe_email=self.params.author_email,
context='add_note')
# Redirect to view page so the browser's back button works properly.
self.redirect('/view', id=self.params.id, query=self.params.query)