forked from benadida/helios-server
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
re-added helios django app as part of the same git project
- Loading branch information
Showing
98 changed files
with
11,933 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
The Helios Django App | ||
===================== | ||
|
||
LICENSE: this code is released under the GPL v3 or later. | ||
|
||
NOTE: this used to be a separate git submodule, but now it's not. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
from django.conf import settings | ||
|
||
TEMPLATE_BASE = settings.HELIOS_TEMPLATE_BASE or "helios/templates/base.html" | ||
|
||
# a setting to ensure that only admins can create an election | ||
ADMIN_ONLY = settings.HELIOS_ADMIN_ONLY | ||
|
||
# allow upload of voters via CSV? | ||
VOTERS_UPLOAD = settings.HELIOS_VOTERS_UPLOAD | ||
|
||
# allow emailing of voters? | ||
VOTERS_EMAIL = settings.HELIOS_VOTERS_EMAIL | ||
|
||
from django.core.urlresolvers import reverse | ||
|
||
# get the short path for the URL | ||
def get_election_url(election): | ||
from views import election_shortcut | ||
return settings.URL_HOST + reverse(election_shortcut, args=[election.short_name]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# Copyright 2008 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 memcache | ||
from google.appengine.ext import db | ||
from google.appengine.api.datastore import _CurrentTransactionKey | ||
import random | ||
|
||
class GeneralCounterShardConfig(db.Model): | ||
"""Tracks the number of shards for each named counter.""" | ||
name = db.StringProperty(required=True) | ||
num_shards = db.IntegerProperty(required=True, default=20) | ||
|
||
|
||
class GeneralCounterShard(db.Model): | ||
"""Shards for each named counter""" | ||
name = db.StringProperty(required=True) | ||
count = db.IntegerProperty(required=True, default=0) | ||
|
||
def get_config(name): | ||
if _CurrentTransactionKey(): | ||
config = GeneralCounterShardConfig.get_by_key_name(name) | ||
if not config: | ||
config = GeneralCounterShardConfig(name=name) | ||
config.put() | ||
return config | ||
else: | ||
return GeneralCounterShardConfig.get_or_insert(name, name=name) | ||
|
||
def get_count(name): | ||
"""Retrieve the value for a given sharded counter. | ||
Parameters: | ||
name - The name of the counter | ||
""" | ||
total = memcache.get(name) | ||
if total is None: | ||
total = 0 | ||
for counter in GeneralCounterShard.all().filter('name = ', name): | ||
total += counter.count | ||
memcache.add(name, str(total), 60) | ||
return total | ||
|
||
|
||
def increment(name): | ||
"""Increment the value for a given sharded counter. | ||
Parameters: | ||
name - The name of the counter | ||
""" | ||
config = get_config(name) | ||
def txn(): | ||
index = random.randint(0, config.num_shards - 1) | ||
shard_name = name + str(index) | ||
counter = GeneralCounterShard.get_by_key_name(shard_name) | ||
if counter is None: | ||
counter = GeneralCounterShard(key_name=shard_name, name=name) | ||
counter.count += 1 | ||
counter.put() | ||
|
||
if _CurrentTransactionKey(): | ||
txn() | ||
else: | ||
db.run_in_transaction(txn) | ||
|
||
memcache.incr(name) | ||
|
||
|
||
def increase_shards(name, num): | ||
"""Increase the number of shards for a given sharded counter. | ||
Will never decrease the number of shards. | ||
Parameters: | ||
name - The name of the counter | ||
num - How many shards to use | ||
""" | ||
config = get_config(name) | ||
def txn(): | ||
if config.num_shards < num: | ||
config.num_shards = num | ||
config.put() | ||
|
||
if _CurrentTransactionKey(): | ||
txn() | ||
else: | ||
db.run_in_transaction(txn) | ||
|
Empty file.
Oops, something went wrong.