Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First batch of yapf auto-formatting. #630

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 29 additions & 22 deletions app/cloud_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,20 @@ def insert_object(self, object_name, content_type, data):
content_type (str): MIME type string of the object.
data (str): The content of the object.
"""
media = MediaIoBaseUpload(StringIO.StringIO(data), mimetype=content_type)
media = MediaIoBaseUpload(
StringIO.StringIO(data), mimetype=content_type)
self.service.objects().insert(
bucket=self.bucket_name,
name=object_name,
body={
# This let browsers to download the file instead of opening
# it in a browser.
'contentDisposition':
'attachment; filename=%s' % object_name,
'contentDisposition': 'attachment; filename=%s' % object_name,
},
media_body=media).execute()

def compose_objects(
self,
source_object_names,
destination_object_name,
destination_content_type):
def compose_objects(self, source_object_names, destination_object_name,
destination_content_type):
"""Concatenates the source objects to generate the destination object.

Args:
Expand All @@ -92,13 +89,16 @@ def compose_objects(
destinationBucket=self.bucket_name,
destinationObject=destination_object_name,
body={
'sourceObjects': [{'name': name} for name in source_object_names],
'sourceObjects': [{
'name': name
} for name in source_object_names],
'destination': {
'contentType': destination_content_type,
'contentType':
destination_content_type,
# This let browsers to download the file instead of opening
# it in a browser.
'contentDisposition':
'attachment; filename=%s' % destination_object_name,
'attachment; filename=%s' % destination_object_name,
},
}).execute()

Expand Down Expand Up @@ -146,7 +146,8 @@ def sign_url(self, object_name, url_lifetime):
'Expires': str(expiration_sec),
'Signature': base64.b64encode(signature),
}
return 'https://storage.googleapis.com%s?%s' % (path, urllib.urlencode(query_params))
return 'https://storage.googleapis.com%s?%s' % (
path, urllib.urlencode(query_params))

def set_objects_lifetime(self, lifetime_days):
"""Sets lifetime of all objects in the bucket in days.
Expand All @@ -155,13 +156,19 @@ def set_objects_lifetime(self, lifetime_days):

lifetime_days (int): Lifetime of objects in a number of days.
"""
self.service.buckets().patch(bucket=self.bucket_name, body={
'lifecycle': {
'rule': [
{
'action': {'type': 'Delete'},
'condition': {'age': lifetime_days},
},
],
},
}).execute()
self.service.buckets().patch(
bucket=self.bucket_name,
body={
'lifecycle': {
'rule': [
{
'action': {
'type': 'Delete'
},
'condition': {
'age': lifetime_days
},
},
],
},
}).execute()
30 changes: 18 additions & 12 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# 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.

"""Storage for configuration settings. Settings can be global or specific
to a repository, and their values can be of any JSON-encodable type.

Expand Down Expand Up @@ -47,9 +46,9 @@ class ConfigurationCache:

def flush(self):
self.storage.clear()
self.items_count=0
self.items_count = 0

def delete(self,key):
def delete(self, key):
"""Deletes the entry with given key from config_cache."""
if key in self.storage:
self.storage.pop(key)
Expand All @@ -67,12 +66,12 @@ def read(self, key, default=None):
"""Gets the value corresponding to the key from cache. If cache entry
has expired, it is deleted from the cache and None is returned."""
value, expiry = self.storage.get(key, (None, 0))
if value is None :
if value is None:
self.miss_count += 1
return default

now = utils.get_utcnow()
if (expiry > now) :
if (expiry > now):
self.hit_count += 1
return value
else:
Expand Down Expand Up @@ -100,7 +99,7 @@ def get_config(self, repo, name, default=None):
return default
logging.debug("Adding repository %r to config_cache" % repo)
config_dict = dict([(e.key().name().split(':', 1)[1],
simplejson.loads(e.value)) for e in entries])
simplejson.loads(e.value)) for e in entries])
self.add(repo, config_dict, self.expiry_time)

if name in config_dict:
Expand All @@ -110,13 +109,16 @@ def get_config(self, repo, name, default=None):
def enable(self, value):
"""Enable/disable caching of config."""
logging.info('Setting config_cache_enable to %s' % value)
db.put(ConfigEntry(key_name="*:config_cache_enable",
value=simplejson.dumps(bool(value))))
db.put(
ConfigEntry(
key_name="*:config_cache_enable",
value=simplejson.dumps(bool(value))))
self.delete('*')

def is_enabled(self):
return self.get_config('*', 'config_cache_enable', None)


cache = ConfigurationCache()


Expand All @@ -139,16 +141,19 @@ def get(name, default=None, repo='*'):
return simplejson.loads(entry.value)
return default


def set(repo='*', **kwargs):
"""Sets configuration settings."""
if 'launched_repos' in kwargs.keys():
raise Exception(
'Config "launched_repos" is deprecated. Use per-repository '
'config "launched" instead.')
db.put(ConfigEntry(key_name=repo + ':' + name,
value=simplejson.dumps(value)) for name, value in kwargs.items())
db.put(
ConfigEntry(key_name=repo + ':' + name, value=simplejson.dumps(value))
for name, value in kwargs.items())
cache.delete(repo)


# If calling from code where a Configuration object is available (e.g., from
# within a handler), prefer Configuration.get. Configuration objects get all
# config entries when they're initialized, so they don't need to make an
Expand All @@ -162,19 +167,20 @@ def get_for_repo(repo, name, default=None):
value = get(name, default, '*')
return value


def set_for_repo(repo, **kwargs):
"""Sets configuration settings for a particular repository. When used
with get_for_repo, has the effect of overriding global settings."""
set(str(repo), **kwargs)


class Configuration(UserDict.DictMixin):

def __init__(self, repo):
self.repo = repo
# We fetch all the config entries at once here, so that we don't have to
# make many Datastore queries for each individual entry later.
db_entries = model.filter_by_prefix(
ConfigEntry.all(), self.repo + ':')
db_entries = model.filter_by_prefix(ConfigEntry.all(), self.repo + ':')
self.entries = {
entry.key().name().split(':', 1)[1]: simplejson.loads(entry.value)
for entry in db_entries
Expand Down
20 changes: 10 additions & 10 deletions app/confirm_disable_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@

from django.utils.translation import ugettext as _


class DisableAndEnableNotesError(Exception):
"""Container for user-facing error messages when confirming to disable
or enable future nots to a record."""
pass


class Handler(utils.BaseHandler):
"""This handler lets the author confirm to disable future notes
to a person record."""
Expand All @@ -36,10 +38,11 @@ def get(self):
except DisableAndEnableNotesError, e:
return self.error(400, unicode(e))

self.render('confirm_disable_notes.html',
person=person,
id=self.params.id,
token=token)
self.render(
'confirm_disable_notes.html',
person=person,
id=self.params.id,
token=token)

def post(self):
try:
Expand All @@ -49,8 +52,7 @@ def post(self):

# Log the user action.
model.UserActionLog.put_new(
'disable_notes',
person,
'disable_notes', person,
self.request.get('reason_for_disabling_notes'))

# Update the notes_disabled flag in person record.
Expand All @@ -62,7 +64,7 @@ def post(self):

# Send subscribers a notice email.
subject = _('[Person Finder] Notes are now disabled for "%(full_name)s"'
) % {'full_name': person.primary_full_name}
) % {'full_name': person.primary_full_name}
email_addresses = person.get_associated_emails()
for address in email_addresses:
self.send_mail(
Expand All @@ -71,9 +73,7 @@ def post(self):
body=self.render_to_string(
'disable_notes_notice_email.txt',
full_name=person.primary_full_name,
record_url=record_url
)
)
record_url=record_url))

self.redirect(record_url)

Expand Down
15 changes: 5 additions & 10 deletions app/confirm_enable_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from confirm_disable_notes import DisableAndEnableNotesError


class Handler(utils.BaseHandler):
"""This handler lets the author confirm to disable future nots
to a person record."""
Expand All @@ -45,7 +46,7 @@ def get(self):

# Send subscribers a notice email.
subject = _('[Person Finder] Notes are now enabled on "%(full_name)s"'
) % {'full_name': person.primary_full_name}
) % {'full_name': person.primary_full_name}
email_addresses = person.get_associated_emails()
for address in email_addresses:
self.send_mail(
Expand All @@ -54,13 +55,10 @@ def get(self):
body=self.render_to_string(
'enable_notes_notice_email.txt',
full_name=person.primary_full_name,
record_url=record_url
)
)
record_url=record_url))

self.redirect(record_url)


def post(self):
try:
person, token = self.get_person_and_verify_params()
Expand All @@ -79,7 +77,7 @@ def post(self):

# Send subscribers a notice email.
subject = _('[Person Finder] Enabling notes notice for "%(full_name)s"'
) % {'full_name': person.primary_full_name}
) % {'full_name': person.primary_full_name}
email_addresses = person.get_associated_emails()
for address in email_addresses:
self.send_mail(
Expand All @@ -88,13 +86,10 @@ def post(self):
body=self.render_to_string(
'enable_notes_notice_email.txt',
full_name=person.primary_full_name,
record_url=record_url
)
)
record_url=record_url))

self.redirect(record_url)


def get_person_and_verify_params(self):
"""Check the request for a valid person id and valid crypto token.

Expand Down
20 changes: 12 additions & 8 deletions app/confirm_post_flagged_note.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@

from django.utils.translation import ugettext as _


class ConfirmPostNoteWithBadWordsError(Exception):
"""Container for user-facing error messages when confirming to post
a note with bad words."""
pass


class Handler(utils.BaseHandler):
"""This handler lets the author confirm to post a note containing
bad words."""
Expand Down Expand Up @@ -83,7 +85,7 @@ def confirm_note_with_bad_words(self, note):
(2) copy the note from NoteWithBadWords to Note;
(3) log user action;
(4) update person record. """
note.confirmed = True;
note.confirmed = True

# Check whether the record author disabled notes on
# this record during the time between the note author inputs the
Expand All @@ -97,7 +99,7 @@ def confirm_note_with_bad_words(self, note):
# during the time between the note author inputs the
# note in the UI and confirms the note through email.
if (self.params.status == 'believed_dead' and
not self.config.allow_believed_dead_via_ui):
not self.config.allow_believed_dead_via_ui):
return self.error(
200, _('Not authorized to post notes with the status '
'"believed_dead".'))
Expand All @@ -124,15 +126,17 @@ def confirm_note_with_bad_words(self, note):

# Specially log 'believed_dead'.
if note_confirmed.status == 'believed_dead':
model.UserActionLog.put_new(
'mark_dead', note_confirmed, person.primary_full_name,
self.request.remote_addr)
model.UserActionLog.put_new('mark_dead', note_confirmed,
person.primary_full_name,
self.request.remote_addr)

# Specially log a switch to an alive status.
if (note_confirmed.status in ['believed_alive', 'is_note_author'] and
person.latest_status not in ['believed_alive', 'is_note_author']):
model.UserActionLog.put_new(
'mark_alive', note_confirmed, person.primary_full_name)
person.latest_status not in [
'believed_alive', 'is_note_author'
]):
model.UserActionLog.put_new('mark_alive', note_confirmed,
person.primary_full_name)

# Update the Person based on the Note.
if person:
Expand Down
Loading