<s> import time <EOL> import logging <EOL> import csv <EOL> import StringIO <EOL> from google . appengine . ext import db <EOL> from google . appengine . api import memcache <EOL> from . . forms import PetitionForm , AgencyForm , ContactForm <EOL> from . . utils . view import render_to_response , redirect_to , not_implemented , render_to_json , render_csv <EOL> from . . utils . mailer import send_to_contact <EOL> from . . models import FeedReference , Agency , NamedStat , TransitApp <EOL> from . . decorators import memcache_view_response , requires_GET , requires_POST <EOL> from django . template . context import RequestContext <EOL> from django . conf import settings <EOL> from django . http import HttpResponseRedirect <EOL> from google . appengine . api . users import create_login_url , create_logout_url <EOL> @ memcache_view_response ( time = settings . MEMCACHE_PAGE_SECONDS ) <EOL> def home ( request ) : <EOL> template_vars = { <EOL> '<STR_LIT>' : TransitApp . featured_by_most_recently_added ( ) . fetch ( <NUM_LIT:8> ) , <EOL> '<STR_LIT>' : PetitionForm ( ) , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : Agency . all ( ) . count ( ) , <EOL> '<STR_LIT>' : Agency . all ( ) . filter ( "<STR_LIT>" , None ) . filter ( "<STR_LIT>" , False ) . order ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : Agency . all ( ) . filter ( "<STR_LIT>" , None ) . order ( "<STR_LIT>" ) , <EOL> } <EOL> return render_to_response ( request , '<STR_LIT>' , template_vars ) <EOL> def contact ( request ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> contact_form = ContactForm ( request . POST ) <EOL> if contact_form . is_valid ( ) : <EOL> send_to_contact ( "<STR_LIT>" + contact_form . cleaned_data [ '<STR_LIT:name>' ] + "<STR_LIT:U+002CU+0020>" + contact_form . cleaned_data [ '<STR_LIT:email>' ] , contact_form . cleaned_data [ '<STR_LIT:message>' ] ) ; <EOL> return redirect_to ( '<STR_LIT>' ) <EOL> else : <EOL> contact_form = ContactForm ( ) <EOL> template_vars = { <EOL> '<STR_LIT>' : contact_form <EOL> } <EOL> return render_to_response ( request , '<STR_LIT>' , template_vars ) <EOL> def contact_thanks ( request ) : <EOL> petition_form = PetitionForm ( ) <EOL> template_vars = { <EOL> '<STR_LIT>' : petition_form <EOL> } <EOL> return render_to_response ( request , '<STR_LIT>' , template_vars ) <EOL> def static ( request , template ) : <EOL> return render_to_response ( request , template ) <EOL> def admin_login ( request ) : <EOL> return HttpResponseRedirect ( create_login_url ( "<STR_LIT:/>" ) ) <EOL> def admin_logout ( request ) : <EOL> return HttpResponseRedirect ( create_logout_url ( "<STR_LIT:/>" ) ) <EOL> def admin_home ( request ) : <EOL> all_stats = NamedStat . all ( ) <EOL> return render_to_response ( request , '<STR_LIT>' , { '<STR_LIT>' : all_stats } ) <EOL> def admin_integrity_check ( request ) : <EOL> def url_for_key_and_kind ( key , kind ) : <EOL> if settings . RUNNING_APP_ENGINE_LOCAL_SERVER : <EOL> return "<STR_LIT>" % ( key , kind ) <EOL> else : <EOL> return "<STR_LIT>" % key <EOL> from . . import models as modmod <EOL> model_classes = [ ] <EOL> for attr in dir ( modmod ) : <EOL> potential_class = getattr ( modmod , attr ) <EOL> try : <EOL> is_subclass = issubclass ( potential_class , db . Model ) <EOL> except Exception : <EOL> is_subclass = False <EOL> if is_subclass : <EOL> model_classes . append ( ( attr , potential_class ) ) <EOL> ref_properties = [ ] <EOL> ref_lists = [ ] <EOL> for model_name , model_class in model_classes : <EOL> for attr in dir ( model_class ) : <EOL> potential_ref = getattr ( model_class , attr ) <EOL> try : <EOL> is_key_list = False <EOL> is_instance = isinstance ( potential_ref , db . ReferenceProperty ) <EOL> if not is_instance : <EOL> is_list = isinstance ( potential_ref , db . ListProperty ) <EOL> if is_list : <EOL> is_key_list = ( potential_ref . item_type == db . Key ) <EOL> except Exception : <EOL> is_instance = False <EOL> is_key_list = False <EOL> if is_instance : <EOL> ref_properties . append ( ( model_name , model_class , attr ) ) <EOL> if is_key_list : <EOL> ref_lists . append ( ( model_name , model_class , attr ) ) <EOL> bad_refs = [ ] <EOL> for model_name , model_class , property_name in ref_properties : <EOL> for item in model_class . all ( ) : <EOL> try : <EOL> other_side_of_ref = eval ( "<STR_LIT>" + property_name ) <EOL> except Exception : <EOL> ref_info = { <EOL> "<STR_LIT>" : model_name , <EOL> "<STR_LIT>" : property_name , <EOL> "<STR_LIT>" : str ( item . key ( ) ) , <EOL> "<STR_LIT:description>" : str ( item ) , <EOL> "<STR_LIT>" : url_for_key_and_kind ( kind = model_name , key = str ( item . key ( ) ) ) , <EOL> } <EOL> bad_refs . append ( ref_info ) <EOL> bad_lists = [ ] <EOL> for model_name , model_class , property_name in ref_lists : <EOL> for item in model_class . all ( ) : <EOL> keys = eval ( "<STR_LIT>" + property_name ) <EOL> for key in keys : <EOL> try : <EOL> entity = db . get ( key ) <EOL> except Exception : <EOL> list_info = { <EOL> "<STR_LIT>" : model_name , <EOL> "<STR_LIT>" : property_name , <EOL> "<STR_LIT>" : str ( item . key ( ) ) , <EOL> "<STR_LIT:description>" : str ( item ) , <EOL> "<STR_LIT>" : url_for_key_and_kind ( kind = model_name , key = str ( item . key ( ) ) ) , <EOL> } <EOL> bad_lists . append ( list_info ) <EOL> break <EOL> template_vars = { <EOL> '<STR_LIT>' : len ( bad_refs ) , <EOL> '<STR_LIT>' : bad_refs , <EOL> '<STR_LIT>' : len ( bad_lists ) , <EOL> '<STR_LIT>' : bad_lists , <EOL> } <EOL> return render_to_response ( request , '<STR_LIT>' , template_vars ) <EOL> def comment ( request ) : <EOL> return render_to_response ( request , '<STR_LIT>' , { } ) <EOL> def admin_memcache_statistics ( request ) : <EOL> stats = [ ( k , v ) for k , v in memcache . get_stats ( ) . iteritems ( ) ] <EOL> template_vars = { <EOL> '<STR_LIT>' : stats , <EOL> } <EOL> return render_to_response ( request , "<STR_LIT>" , template_vars ) <EOL> @ requires_GET <EOL> def admin_memcache_statistics_json ( request ) : <EOL> stats = dict ( memcache . get_stats ( ) ) <EOL> return render_to_json ( stats ) <EOL> @ requires_POST <EOL> def admin_clear_memcache ( request ) : <EOL> success = memcache . flush_all ( ) <EOL> return render_to_json ( { "<STR_LIT:success>" : success } ) <EOL> @ requires_GET <EOL> def admin_apps_csv ( request ) : <EOL> string_io = StringIO . StringIO ( ) <EOL> writer = csv . writer ( string_io ) <EOL> writer . writerow ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> for transit_app in TransitApp . query_all ( visible_only = False ) : <EOL> writer . writerow ( [ transit_app . title . encode ( '<STR_LIT:utf8>' ) , transit_app . author_name . encode ( '<STR_LIT:utf8>' ) , str ( transit_app . author_email ) . encode ( '<STR_LIT:utf8>' ) , str ( transit_app . url ) . encode ( '<STR_LIT:utf8>' ) , transit_app . description . encode ( '<STR_LIT:utf8>' ) , repr ( transit_app . is_hidden ) . encode ( '<STR_LIT:utf8>' ) ] ) <EOL> csv_output = string_io . getvalue ( ) <EOL> string_io . close ( ) <EOL> return render_csv ( csv_output ) </s>
