<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import configuration as config
from datetime import datetime, timedelta
from google.appengine.api import memcache
from google.appengine.ext import db
try:
    import json
except ImportError:
    from django.utils import simplejson as json
import calendar as cal
from data import countries, calendar
import properties
from counter import Counter
from decimal import Decimal

FETCH_ALL_VALUES = 1000
DEFAULT_CACHE_TIMEOUT = 30   # 30 seconds.

# cache time out values in seconds.
FIVE_SECONDS = 5
TEN_SECONDS = 10
THIRTY_SECONDS = 30
ONE_MINUTE = 60
TWO_MINUTES = 120
FIVE_MINUTES = 300
TEN_MINUTES = FIVE_MINUTES * 2
TWENTY_MINUTES = FIVE_MINUTES * 4
THIRTY_MINUTES = FIVE_MINUTES * 6
HALF_HOUR = THIRTY_MINUTES
ONE_HOUR = HALF_HOUR * 2
TWELVE_HOURS = ONE_HOUR * 12
TWENTY_FOUR_HOURS = TWELVE_HOURS * 2
ONE_DAY = TWENTY_FOUR_HOURS
TWO_DAYS = ONE_DAY * 2
HALF_DAY = TWELVE_HOURS

HINT_QUESTIONS = {
    'mothers_maiden_name': "What is your mother's maiden name?",
    'name_of_pet': "What is the name of your pet?",
    'first_school_teacher': "Who was your first school teacher?",
    'first_phone_number': "What was your first phone number?",
    'favorite_passtime': "What is your favorite passtime?",
}
HINT_QUESTIONS_TUPLE_MAP = [(v,k) for k, v in HINT_QUESTIONS.iteritems()]
HINT_QUESTIONS_TUPLE_MAP.sort()
HINT_QUESTIONS_CHOICES = [k for k, v in HINT_QUESTIONS.iteritems()]
COUNTRIES_LIST = countries.COUNTRIES_SELECTION_LIST
MONTH_LIST = calendar.MONTH_NAMES

current_year = datetime.utcnow().year

BLOG_START_YEAR = 2009
BLOG_YEAR_LIST = range(BLOG_START_YEAR, current_year + 1)
YEAR_LIST = range(1900, current_year)
MILS_YEAR_LIST = range(1948, current_year + 1)[::-1]
MINIMUM_AGE_LIMIT = 18
BIRTH_YEAR_LIST = YEAR_LIST[:-(MINIMUM_AGE_LIMIT - 1)][::-1]
DEFAULT_BIRTHDATE = (BIRTH_YEAR_LIST[0], 1, 1)
DEFAULT_BIRTHDATE_STRING = '%d-%d-%d' % DEFAULT_BIRTHDATE

GENDER_CHOICES = (
    'male',
    'female'
)

BLOG_CONTENT_TYPES = (
    'markdown',
    'textile',
)

T_SHIRT_SIZES = {
    'small': 'Small',
    'medium': 'Medium',
    'large': 'Large',
    'extra_large': 'Extra Large',
}
T_SHIRT_TYPES_TUPLE_MAP = [(k, v) for k, v in T_SHIRT_SIZES.iteritems()]
T_SHIRT_TYPES = [k for k, v in T_SHIRT_SIZES.iteritems()]
T_SHIRT_TYPES.sort()

RAILWAY_LINES = {
    'western': 'Western',
    'central': 'Central',
    'harbor': 'Harbor',
    'other': 'Out of Mumbai',
}
RAILWAY_LINE_TYPES = [k for k, v in RAILWAY_LINES.iteritems()]
RAILWAY_LINE_TYPES.sort()
RAILWAY_LINES_TUPLE_MAP = [(k, v) for k, v in RAILWAY_LINES.iteritems()]

ADDRESS_TYPES = [
    'home',
    'residence',
    'work',
    'correspondence',
    'permanent',
    'temporary',
    'other',
]

PHONE_TYPES = [
    'mobile',
    'home',
    'work',
    'fax',
    'pager',
    'other',
]

AUTH_LEVEL_NEW_USER = 0
AUTH_LEVEL_REGISTERED_USER = 1
AUTH_LEVEL_ACTIVATED_USER = 2

MAIL_TEMPLATE_CHOICES = [
    'jinja2',
]

ARTICLE_SECTION_TYPE_ALUMNI = 'alumni'
ARTICLE_SECTION_TYPE_STUDENT = 'student'
ARTICLE_SECTION_TYPES = [ARTICLE_SECTION_TYPE_ALUMNI, ARTICLE_SECTION_TYPE_STUDENT]


def clear_datastore(including_articles=False):
    db.delete(User.all())
    db.delete(Person.all())
    db.delete(PersonAddress.all())
    db.delete(PersonPhone.all())
    db.delete(OpenID.all())
    db.delete(Book.all())
    db.delete(UserBookOrder.all())
    if including_articles:
        db.delete(Article.all())
        db.delete(ArticleComment.all())

class RegularModel(db.Expando):
    is_deleted = db.BooleanProperty(default=False)
    is_starred = db.BooleanProperty(default=False)
    is_active = db.BooleanProperty(default=False)
    when_created = db.DateTimeProperty(auto_now_add=True)
    when_modified = db.DateTimeProperty(auto_now=True)
    #who_modified = db.StringProperty()

    def to_json_dict(self, *props):
        properties = self.properties()
        if props:
            serializable_properties = props
        else:
            serializable_properties = getattr(self, '__serialize__', [])
            if not serializable_properties:
                serializable_properties = properties.keys()
            else:
                serializable_properties.extend([
                    'is_deleted',
                    'is_starred',
                    'is_active',
                    'when_created',
                    'when_modified',
                    ])
        output = {}
        output['key'] = str(self.key())
        for prop in set(serializable_properties):
            v = properties[prop]
            if isinstance(v, db.DateTimeProperty) or isinstance(v, db.DateProperty):
                convert_function = (lambda d: d.strftime('%Y-%m-%dT%H:%M:%S'))
                output[prop] = convert_function(getattr(self, prop))
            elif isinstance(v, db.ReferenceProperty):
                str_key = str(getattr(self, prop).key())
                output[prop] = str_key
                #output[prop + '_key'] = str_key
            else:
                output[prop] = getattr(self, prop)
        return output

    def to_json(self, *props):
        json_dict = self.to_json_dict(*props)
        return json.dumps(json_dict)

class Api(RegularModel):
    name = db.StringProperty()
    api_key = db.StringProperty()
    mode = db.StringProperty(choices=[config.MODE_PRODUCTION, config.MODE_DEVELOPMENT])
    
    @classmethod
    def get_api_key(cls, name, mode=config.DEPLOYMENT_MODE):
        cache_key = 'Api.get_api_key.' + name + '.' + mode
        api_key = memcache.get(cache_key)
        if not api_key:
            api = db.Query(Api).filter('name =', name).filter('mode =', mode).get()
            api_key = api.api_key
            memcache.set(cache_key, api_key, 120)
        return api_key

class Mail(RegularModel):
    
    subject = db.StringProperty()
    body = db.TextProperty()
    to_users = db.StringProperty()
    to_emails = db.StringListProperty()
    when_sent = db.DateTimeProperty()

    @classmethod
    def get_all(cls):
        cache_key = 'Mail.get_all'
        mails = memcache.get(cache_key)
        if not mails:
            mails = db.Query(Mail).order('-when_sent').order('-when_modified').order('-when_created').fetch(FETCH_ALL_VALUES)
            memcache.set(cache_key, mails, 15)
        return mails

class MailReceiver(RegularModel):
    email = db.EmailProperty()
    mail = db.ReferenceProperty(collection_name='receivers')

"""
class MailerTemplate(RegularModel):
    name = db.StringProperty()
    content = db.TextProperty()
    content_type = db.StringProperty(choices=MAIL_TEMPLATE_CHOICES)

class MailerList(RegularModel):
    name = db.StringProperty()
<commit_msg>change cache key for the api model<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import configuration as config
from datetime import datetime, timedelta
from google.appengine.api import memcache
from google.appengine.ext import db
try:
    import json
except ImportError:
    from django.utils import simplejson as json
import calendar as cal
from data import countries, calendar
import properties
from counter import Counter
from decimal import Decimal

FETCH_ALL_VALUES = 1000
DEFAULT_CACHE_TIMEOUT = 30   # 30 seconds.

# cache time out values in seconds.
FIVE_SECONDS = 5
TEN_SECONDS = 10
THIRTY_SECONDS = 30
ONE_MINUTE = 60
TWO_MINUTES = 120
FIVE_MINUTES = 300
TEN_MINUTES = FIVE_MINUTES * 2
TWENTY_MINUTES = FIVE_MINUTES * 4
THIRTY_MINUTES = FIVE_MINUTES * 6
HALF_HOUR = THIRTY_MINUTES
ONE_HOUR = HALF_HOUR * 2
TWELVE_HOURS = ONE_HOUR * 12
TWENTY_FOUR_HOURS = TWELVE_HOURS * 2
ONE_DAY = TWENTY_FOUR_HOURS
TWO_DAYS = ONE_DAY * 2
HALF_DAY = TWELVE_HOURS

HINT_QUESTIONS = {
    'mothers_maiden_name': "What is your mother's maiden name?",
    'name_of_pet': "What is the name of your pet?",
    'first_school_teacher': "Who was your first school teacher?",
    'first_phone_number': "What was your first phone number?",
    'favorite_passtime': "What is your favorite passtime?",
}
HINT_QUESTIONS_TUPLE_MAP = [(v,k) for k, v in HINT_QUESTIONS.iteritems()]
HINT_QUESTIONS_TUPLE_MAP.sort()
HINT_QUESTIONS_CHOICES = [k for k, v in HINT_QUESTIONS.iteritems()]
COUNTRIES_LIST = countries.COUNTRIES_SELECTION_LIST
MONTH_LIST = calendar.MONTH_NAMES

current_year = datetime.utcnow().year

BLOG_START_YEAR = 2009
BLOG_YEAR_LIST = range(BLOG_START_YEAR, current_year + 1)
YEAR_LIST = range(1900, current_year)
MILS_YEAR_LIST = range(1948, current_year + 1)[::-1]
MINIMUM_AGE_LIMIT = 18
BIRTH_YEAR_LIST = YEAR_LIST[:-(MINIMUM_AGE_LIMIT - 1)][::-1]
DEFAULT_BIRTHDATE = (BIRTH_YEAR_LIST[0], 1, 1)
DEFAULT_BIRTHDATE_STRING = '%d-%d-%d' % DEFAULT_BIRTHDATE

GENDER_CHOICES = (
    'male',
    'female'
)

BLOG_CONTENT_TYPES = (
    'markdown',
    'textile',
)

T_SHIRT_SIZES = {
    'small': 'Small',
    'medium': 'Medium',
    'large': 'Large',
    'extra_large': 'Extra Large',
}
T_SHIRT_TYPES_TUPLE_MAP = [(k, v) for k, v in T_SHIRT_SIZES.iteritems()]
T_SHIRT_TYPES = [k for k, v in T_SHIRT_SIZES.iteritems()]
T_SHIRT_TYPES.sort()

RAILWAY_LINES = {
    'western': 'Western',
    'central': 'Central',
    'harbor': 'Harbor',
    'other': 'Out of Mumbai',
}
RAILWAY_LINE_TYPES = [k for k, v in RAILWAY_LINES.iteritems()]
RAILWAY_LINE_TYPES.sort()
RAILWAY_LINES_TUPLE_MAP = [(k, v) for k, v in RAILWAY_LINES.iteritems()]

ADDRESS_TYPES = [
    'home',
    'residence',
    'work',
    'correspondence',
    'permanent',
    'temporary',
    'other',
]

PHONE_TYPES = [
    'mobile',
    'home',
    'work',
    'fax',
    'pager',
    'other',
]

AUTH_LEVEL_NEW_USER = 0
AUTH_LEVEL_REGISTERED_USER = 1
AUTH_LEVEL_ACTIVATED_USER = 2

MAIL_TEMPLATE_CHOICES = [
    'jinja2',
]

ARTICLE_SECTION_TYPE_ALUMNI = 'alumni'
ARTICLE_SECTION_TYPE_STUDENT = 'student'
ARTICLE_SECTION_TYPES = [ARTICLE_SECTION_TYPE_ALUMNI, ARTICLE_SECTION_TYPE_STUDENT]


def clear_datastore(including_articles=False):
    db.delete(User.all())
    db.delete(Person.all())
    db.delete(PersonAddress.all())
    db.delete(PersonPhone.all())
    db.delete(OpenID.all())
    db.delete(Book.all())
    db.delete(UserBookOrder.all())
    if including_articles:
        db.delete(Article.all())
        db.delete(ArticleComment.all())

class RegularModel(db.Expando):
    is_deleted = db.BooleanProperty(default=False)
    is_starred = db.BooleanProperty(default=False)
    is_active = db.BooleanProperty(default=False)
    when_created = db.DateTimeProperty(auto_now_add=True)
    when_modified = db.DateTimeProperty(auto_now=True)
    #who_modified = db.StringProperty()

    def to_json_dict(self, *props):
        properties = self.properties()
        if props:
            serializable_properties = props
        else:
            serializable_properties = getattr(self, '__serialize__', [])
            if not serializable_properties:
                serializable_properties = properties.keys()
            else:
                serializable_properties.extend([
                    'is_deleted',
                    'is_starred',
                    'is_active',
                    'when_created',
                    'when_modified',
                    ])
        output = {}
        output['key'] = str(self.key())
        for prop in set(serializable_properties):
            v = properties[prop]
            if isinstance(v, db.DateTimeProperty) or isinstance(v, db.DateProperty):
                convert_function = (lambda d: d.strftime('%Y-%m-%dT%H:%M:%S'))
                output[prop] = convert_function(getattr(self, prop))
            elif isinstance(v, db.ReferenceProperty):
                str_key = str(getattr(self, prop).key())
                output[prop] = str_key
                #output[prop + '_key'] = str_key
            else:
                output[prop] = getattr(self, prop)
        return output

    def to_json(self, *props):
        json_dict = self.to_json_dict(*props)
        return json.dumps(json_dict)

class Api(RegularModel):
    name = db.StringProperty()
    api_key = db.StringProperty()
    mode = db.StringProperty(choices=[config.MODE_PRODUCTION, config.MODE_DEVELOPMENT])
    
    @classmethod
    def get_api_key(cls, name, mode=config.DEPLOYMENT_MODE):
        cache_key = 'Api.api_key.' + name + '.' + mode
        api_key = memcache.get(cache_key)
        if not api_key:
            api = db.Query(Api).filter('name =', name).filter('mode =', mode).get()
            api_key = api.api_key
            memcache.set(cache_key, api_key, 7200)
        return api_key
        
    @classmethod
    def set_api_key(cls, name, api_key, mode=config.DEPLOYMENT_MODE):
        api = db.Query(Api).filter('name =', name).filter('mode =', mode).get()
        api.api_key = api_key
        cache_key = 'Api.api_key.' + name + '.' + mode
        memcache.set(cache_key, api_key)

class Mail(RegularModel):
    
    subject = db.StringProperty()
    body = db.TextProperty()
    to_users = db.StringProperty()
    to_emails = db.StringListProperty()
    when_sent = db.DateTimeProperty()

    @classmethod
    def get_all(cls):
        cache_key = 'Mail.get_all'
        mails = memcache.get(cache_key)
        if not mails:
            mails = db.Query(Mail).order('-when_sent').order('-when_modified').order('-when_created').fetch(FETCH_ALL_VALUES)
            memcache.set(cache_key, mails, 15)
        return mails

class MailReceiver(RegularModel):
    email = db.EmailProperty()
    mail = db.ReferenceProperty(collection_name='receivers')

"""
class MailerTemplate(RegularModel):
    name = db.StringProperty()
    content = db.TextProperty()
    content_type = db.StringProperty(choices=MAIL_TEMPLATE_CHOICES)

class MailerList(RegularModel):
    name = db.StringProperty()

{'fraction_non_alphanumeric': 0.11544085392601257, 'fraction_numerical': 0.008992542769410732, 'mean_word_length': 3.2653570314936076, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6221046', 'n_tokens_mistral': 4994, 'n_tokens_neox': 4756, 'n_words': 928}