-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathmultiview.py
130 lines (113 loc) · 5.29 KB
/
multiview.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
127
128
129
130
# Copyright 2010 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 model import *
from utils import *
import pfif
import reveal
import subscribe
import view
from django.utils.translation import ugettext as _
# Fields to show for side-by-side comparison.
COMPARE_FIELDS = pfif.PFIF_1_4.fields['person'] + ['primary_full_name']
class Handler(BaseHandler):
def get(self):
# To handle multiple persons, we create a single object where
# each property is a list of values, one for each person.
# This makes page rendering easier.
person = dict([(prop, []) for prop in COMPARE_FIELDS])
any_person = dict([(prop, None) for prop in COMPARE_FIELDS])
# Get all persons from db.
# TODO: Can later optimize to use fewer DB calls.
for i in [1, 2, 3]:
id = self.request.get('id%d' % i)
if not id:
break
p = Person.get(self.repo, id)
if not p:
return self.error(
404,
_("This person's entry does not exist or has been "
"deleted."))
sanitize_urls(p)
for prop in COMPARE_FIELDS:
val = getattr(p, prop)
if prop == 'sex': # convert enum value to localized text
val = get_person_sex_text(p)
elif prop == 'photo_url' and val:
val = (self.should_show_inline_photo(val), val)
person[prop].append(val)
any_person[prop] = any_person[prop] or val
# Compute the local times for the date fields on the person and format.
person['source_datetime_local_string'] = map(
self.to_formatted_local_datetime, person['source_date'])
# Check if private info should be revealed.
content_id = 'multiview:' + ','.join(person['person_record_id'])
reveal_url = reveal.make_reveal_url(self, content_id)
show_private_info = reveal.verify(content_id, self.params.signature)
# TODO: Handle no persons found.
person['profile_pages'] = [view.get_profile_pages(
profile_urls, self.config, self.transitionary_get_url)
for profile_urls in person['profile_urls']]
any_person['profile_pages'] = any(person['profile_pages'])
# Note: we're not showing notes and linked persons information
# here at the moment.
self.render('multiview.html',
person=person, any=any_person,
cols=len(person['full_name']) + 1,
onload_function='view_page_loaded', markdup=True,
show_private_info=show_private_info, reveal_url=reveal_url)
def post(self):
if not self.params.text:
return self.error(
200, _('Message is required. Please go back and try again.'))
if not self.params.author_name:
return self.error(
200, _('Your name is required in the "About you" section. Please go back and try again.'))
# TODO: To reduce possible abuse, we currently limit to 3 person
# match. We could guard using e.g. an XSRF token, which I don't know how
# to build in GAE.
ids = set()
for i in [1, 2, 3]:
id = getattr(self.params, 'id%d' % i)
if not id:
break
ids.add(id)
if len(ids) > 1:
notes = []
for person_id in ids:
person = Person.get(self.repo, person_id)
person_notes = []
for other_id in ids - set([person_id]):
note = Note.create_original(
self.repo,
entry_date=get_utcnow(),
person_record_id=person_id,
linked_person_record_id=other_id,
text=self.params.text,
author_name=self.params.author_name,
author_phone=self.params.author_phone,
author_email=self.params.author_email,
source_date=get_utcnow())
person_notes.append(note)
# Notify person's subscribers of all new duplicates. We do not
# follow links since each Person record in the ids list gets its
# own note. However, 1) when > 2 records are marked as
# duplicates, subscribers will still receive multiple
# notifications, and 2) subscribers to already-linked Persons
# will not be notified of the new link.
subscribe.send_notifications(self, person, person_notes, False)
notes += person_notes
# Write all notes to store
db.put(notes)
self.redirect('/view', id=self.params.id1)