<s> from datetime import datetime , timedelta <EOL> from django . conf import settings <EOL> from django . contrib import messages <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . core . urlresolvers import reverse <EOL> from django . db . models import Q <EOL> from django . forms . models import modelformset_factory <EOL> from django . http import Http404 , HttpResponse <EOL> from django . shortcuts import get_object_or_404 , render_to_response , redirect <EOL> from django . template import RequestContext <EOL> from django . template . defaultfilters import slugify <EOL> from django . utils import simplejson <EOL> from django . views . generic import ListView , DetailView , View <EOL> from . forms import OrganizationUpdateForm , PersonUpdateForm <EOL> from . models import Person , Organization <EOL> from source . utils . json import render_json_to_response <EOL> USER_DEBUG = getattr ( settings , '<STR_LIT>' , False ) <EOL> class PersonList ( ListView ) : <EOL> model = Person <EOL> def get_queryset ( self ) : <EOL> queryset = Person . live_objects . exclude ( show_in_lists = False ) . prefetch_related ( '<STR_LIT>' ) <EOL> return queryset <EOL> class PersonDetail ( DetailView ) : <EOL> model = Person <EOL> def get_queryset ( self ) : <EOL> queryset = Person . live_objects . prefetch_related ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> return queryset <EOL> class PersonSearchJson ( View ) : <EOL> def get_queryset ( self ) : <EOL> queryset = Person . live_objects . exclude ( show_in_lists = False ) <EOL> return queryset <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> people = self . get_queryset ( ) <EOL> q = self . request . GET . get ( '<STR_LIT:q>' , None ) <EOL> if '<STR_LIT:q>' in self . request . GET : <EOL> people = people . filter ( Q ( first_name__icontains = q ) | Q ( last_name__icontains = q ) ) <EOL> people = people . values ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:email>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:id>' ) <EOL> for person in list ( people ) : <EOL> person [ '<STR_LIT:name>' ] = '<STR_LIT>' % ( person [ '<STR_LIT>' ] , person [ '<STR_LIT>' ] ) <EOL> return render_json_to_response ( list ( people ) ) <EOL> class OrganizationList ( ListView ) : <EOL> model = Organization <EOL> def get_queryset ( self ) : <EOL> queryset = Organization . live_objects . exclude ( show_in_lists = False ) . all ( ) <EOL> return queryset <EOL> class OrganizationDetail ( DetailView ) : <EOL> model = Organization <EOL> def get_queryset ( self ) : <EOL> queryset = Organization . live_objects . prefetch_related ( '<STR_LIT>' ) <EOL> return queryset <EOL> class PersonUpdate ( View ) : <EOL> template_name = "<STR_LIT>" <EOL> form_message = '<STR_LIT>' <EOL> def get_success_url ( self ) : <EOL> return reverse ( '<STR_LIT>' ) <EOL> def get_organization ( self ) : <EOL> user = self . request . user <EOL> if user . is_authenticated ( ) and user . is_active : <EOL> organization = get_object_or_404 ( Organization , is_live = True , email = user . email ) <EOL> return organization <EOL> elif USER_DEBUG : <EOL> organization = get_object_or_404 ( Organization , is_live = True , slug = '<STR_LIT>' ) <EOL> return organization <EOL> return None <EOL> def get_person ( self , pk = None , organization = None , task = None ) : <EOL> user = self . request . user <EOL> if USER_DEBUG or ( user . is_authenticated ( ) and user . is_active ) : <EOL> if pk and organization : <EOL> if task == '<STR_LIT>' : <EOL> person = get_object_or_404 ( Person , is_live = True , pk = pk ) <EOL> else : <EOL> person = get_object_or_404 ( Person , is_live = True , pk = pk , organizations = organization ) <EOL> else : <EOL> person = get_object_or_404 ( Person , is_live = True , email = user . email ) <EOL> return person <EOL> return None <EOL> def create_person ( self , data , organization ) : <EOL> name = data [ '<STR_LIT:name>' ] <EOL> if name : <EOL> try : <EOL> first_name , last_name = name . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> except : <EOL> first_name , last_name = name , '<STR_LIT>' <EOL> person_kwargs = { <EOL> '<STR_LIT>' : first_name , <EOL> '<STR_LIT>' : last_name , <EOL> '<STR_LIT>' : slugify ( '<STR_LIT:->' . join ( [ first_name , last_name ] ) ) <EOL> } <EOL> i = <NUM_LIT:0> <EOL> found = True <EOL> while found : <EOL> i += <NUM_LIT:1> <EOL> try : <EOL> person = Person . objects . get ( slug = person_kwargs [ '<STR_LIT>' ] ) <EOL> person_kwargs [ '<STR_LIT>' ] = slugify ( '<STR_LIT:->' . join ( [ first_name , last_name , str ( i ) ] ) ) <EOL> except ObjectDoesNotExist : <EOL> person = Person ( ** person_kwargs ) <EOL> person . save ( ) <EOL> person . organizations . add ( organization ) <EOL> found = False <EOL> return person <EOL> return None <EOL> def process_form ( self , person , data ) : <EOL> person_form = PersonUpdateForm ( instance = person , data = data ) <EOL> if person_form . is_valid ( ) : <EOL> person_form . save ( ) <EOL> form_message = '<STR_LIT>' <EOL> else : <EOL> error_message = '<STR_LIT>' <EOL> for field in person_form : <EOL> if field . errors : <EOL> add_label = field . label <EOL> add_errors = '<STR_LIT:U+002CU+0020>' . join ( [ error for error in field . errors ] ) <EOL> error_message += '<STR_LIT>' % ( add_label , add_errors ) <EOL> form_message = error_message <EOL> return form_message <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> data = request . POST <EOL> form_message = '<STR_LIT>' <EOL> success_url = self . get_success_url ( ) <EOL> if '<STR_LIT>' in data : <EOL> success_url = reverse ( '<STR_LIT>' ) <EOL> self . template_name = "<STR_LIT>" <EOL> task = data [ '<STR_LIT>' ] <EOL> organization = self . get_organization ( ) <EOL> if task == '<STR_LIT>' : <EOL> person = self . create_person ( data , organization ) <EOL> form_message = '<STR_LIT>' <EOL> success_url += '<STR_LIT>' % person . pk <EOL> else : <EOL> person = self . get_person ( data [ '<STR_LIT>' ] , organization , task ) <EOL> if task == '<STR_LIT>' : <EOL> form_message = self . process_form ( person , data ) <EOL> elif task == '<STR_LIT>' : <EOL> person . organizations . remove ( organization ) <EOL> form_message = '<STR_LIT>' <EOL> elif task == '<STR_LIT>' : <EOL> person . organizations . add ( organization ) <EOL> form_message = '<STR_LIT>' <EOL> else : <EOL> person = self . get_person ( ) <EOL> form_message = self . process_form ( person , data ) <EOL> if request . is_ajax ( ) : <EOL> result = { <EOL> '<STR_LIT:message>' : form_message , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:name>' : person . name ( ) , <EOL> '<STR_LIT>' : person . pk , <EOL> '<STR_LIT>' : person . first_name , <EOL> '<STR_LIT>' : person . last_name , <EOL> '<STR_LIT:email>' : person . email , <EOL> '<STR_LIT>' : person . twitter_username , <EOL> '<STR_LIT>' : person . github_username <EOL> } <EOL> } <EOL> return render_json_to_response ( result ) <EOL> messages . success ( request , form_message ) <EOL> return redirect ( success_url ) <EOL> class OrganizationUpdate ( View ) : <EOL> template_name = "<STR_LIT>" <EOL> error_message = "<STR_LIT>" <EOL> def get_organization ( self , user ) : <EOL> if user . is_authenticated ( ) and user . is_active : <EOL> try : <EOL> organization = Organization . objects . get ( is_live = True , email = user . email ) <EOL> return organization <EOL> except Organization . DoesNotExist : <EOL> self . error_message = "<STR_LIT>" <EOL> except Organization . MultipleObjectsReturned : <EOL> self . error_message = "<STR_LIT>" <EOL> return None <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> context = { } <EOL> user = request . user <EOL> if user . is_authenticated ( ) and user . is_active : <EOL> organization = self . get_organization ( user ) <EOL> if organization : <EOL> organization_form = OrganizationUpdateForm ( instance = organization ) <EOL> context . update ( { <EOL> '<STR_LIT:user>' : request . user , <EOL> '<STR_LIT>' : organization , <EOL> '<STR_LIT>' : organization_form , <EOL> '<STR_LIT>' : datetime . today ( ) . date ( ) + timedelta ( days = <NUM_LIT:30> ) <EOL> } ) <EOL> else : <EOL> context . update ( { <EOL> '<STR_LIT>' : self . error_message <EOL> } ) <EOL> return render_to_response ( self . template_name , context , context_instance = RequestContext ( request ) ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> context = { } <EOL> user = request . user <EOL> organization = self . get_organization ( user ) <EOL> if organization : <EOL> organization_form = OrganizationUpdateForm ( instance = organization , data = request . POST ) <EOL> context . update ( { <EOL> '<STR_LIT:user>' : request . user , <EOL> '<STR_LIT>' : organization , <EOL> '<STR_LIT>' : organization_form , <EOL> } ) <EOL> if organization_form . is_valid ( ) : <EOL> organization_form . save ( ) <EOL> if request . is_ajax ( ) : <EOL> result = { '<STR_LIT:success>' : '<STR_LIT:True>' } <EOL> return render_json_to_response ( result ) <EOL> messages . success ( request , '<STR_LIT>' ) <EOL> return render_to_response ( self . template_name , context , context_instance = RequestContext ( request ) ) </s>
