<s> import json <EOL> import requests <EOL> from decimal import Decimal <EOL> from django . conf import settings <EOL> from django . core . mail import send_mail <EOL> from django . db import models , transaction <EOL> from django . db . models . signals import post_save , pre_save <EOL> from huxley . core . constants import ContactGender , ContactType , ProgramTypes <EOL> class Conference ( models . Model ) : <EOL> session = models . PositiveSmallIntegerField ( default = <NUM_LIT:0> ) <EOL> start_date = models . DateField ( ) <EOL> end_date = models . DateField ( ) <EOL> reg_open = models . DateField ( ) <EOL> early_reg_close = models . DateField ( ) <EOL> reg_close = models . DateField ( ) <EOL> min_attendance = models . PositiveSmallIntegerField ( default = <NUM_LIT:0> ) <EOL> max_attendance = models . PositiveSmallIntegerField ( default = <NUM_LIT:0> ) <EOL> open_reg = models . BooleanField ( default = True ) <EOL> waitlist_reg = models . BooleanField ( default = False ) <EOL> def __unicode__ ( self ) : <EOL> return '<STR_LIT>' % self . session <EOL> class Meta : <EOL> db_table = u'<STR_LIT>' <EOL> get_latest_by = '<STR_LIT>' <EOL> class Country ( models . Model ) : <EOL> name = models . CharField ( max_length = <NUM_LIT> ) <EOL> special = models . BooleanField ( default = False ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> class Meta : <EOL> db_table = u'<STR_LIT>' <EOL> class Committee ( models . Model ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:8> ) <EOL> full_name = models . CharField ( max_length = <NUM_LIT> ) <EOL> countries = models . ManyToManyField ( Country , through = '<STR_LIT>' ) <EOL> delegation_size = models . PositiveSmallIntegerField ( default = <NUM_LIT:2> ) <EOL> special = models . BooleanField ( default = False ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> class Meta : <EOL> db_table = u'<STR_LIT>' <EOL> class School ( models . Model ) : <EOL> REGISTRATION_FEE = <NUM_LIT> <EOL> DELEGATE_FEE = <NUM_LIT> <EOL> PROGRAM_TYPE_OPTIONS = ( <EOL> ( ProgramTypes . CLUB , '<STR_LIT>' ) , <EOL> ( ProgramTypes . CLASS , '<STR_LIT>' ) , <EOL> ) <EOL> CONTACT_TYPE_OPTIONS = ( <EOL> ( ContactType . FACULTY , '<STR_LIT>' ) , <EOL> ( ContactType . STUDENT , '<STR_LIT>' ) , <EOL> ) <EOL> GENDER_OPTIONS = ( <EOL> ( ContactGender . MALE , '<STR_LIT>' ) , <EOL> ( ContactGender . FEMALE , '<STR_LIT>' ) , <EOL> ( ContactGender . OTHER , '<STR_LIT>' ) , <EOL> ( ContactGender . UNSPECIFIED , '<STR_LIT>' ) , <EOL> ) <EOL> registered = models . DateTimeField ( auto_now_add = True ) <EOL> name = models . CharField ( max_length = <NUM_LIT> ) <EOL> address = models . CharField ( max_length = <NUM_LIT> ) <EOL> city = models . CharField ( max_length = <NUM_LIT> ) <EOL> state = models . CharField ( max_length = <NUM_LIT:16> ) <EOL> zip_code = models . CharField ( max_length = <NUM_LIT:16> ) <EOL> country = models . CharField ( max_length = <NUM_LIT:64> ) <EOL> primary_name = models . CharField ( max_length = <NUM_LIT> ) <EOL> primary_gender = models . PositiveSmallIntegerField ( choices = GENDER_OPTIONS , default = ContactGender . UNSPECIFIED ) <EOL> primary_email = models . EmailField ( ) <EOL> primary_phone = models . CharField ( max_length = <NUM_LIT:32> ) <EOL> primary_type = models . PositiveSmallIntegerField ( choices = CONTACT_TYPE_OPTIONS , default = ContactType . FACULTY ) <EOL> secondary_name = models . CharField ( max_length = <NUM_LIT> , blank = True ) <EOL> secondary_gender = models . PositiveSmallIntegerField ( choices = GENDER_OPTIONS , blank = True , default = ContactGender . UNSPECIFIED ) <EOL> secondary_email = models . EmailField ( blank = True ) <EOL> secondary_phone = models . CharField ( max_length = <NUM_LIT:32> , blank = True ) <EOL> secondary_type = models . PositiveSmallIntegerField ( choices = CONTACT_TYPE_OPTIONS , blank = True , default = ContactType . FACULTY ) <EOL> program_type = models . PositiveSmallIntegerField ( choices = PROGRAM_TYPE_OPTIONS , default = ProgramTypes . CLUB ) <EOL> times_attended = models . PositiveSmallIntegerField ( default = <NUM_LIT:0> ) <EOL> international = models . BooleanField ( default = False ) <EOL> waitlist = models . BooleanField ( default = False ) <EOL> beginner_delegates = models . PositiveSmallIntegerField ( ) <EOL> intermediate_delegates = models . PositiveSmallIntegerField ( ) <EOL> advanced_delegates = models . PositiveSmallIntegerField ( ) <EOL> spanish_speaking_delegates = models . PositiveSmallIntegerField ( ) <EOL> chinese_speaking_delegates = models . PositiveSmallIntegerField ( ) <EOL> countrypreferences = models . ManyToManyField ( Country , through = '<STR_LIT>' ) <EOL> committeepreferences = models . ManyToManyField ( Committee , limit_choices_to = { '<STR_LIT>' : True } ) <EOL> registration_comments = models . TextField ( default = '<STR_LIT>' , blank = True ) <EOL> fees_owed = models . DecimalField ( max_digits = <NUM_LIT:6> , decimal_places = <NUM_LIT:2> , default = Decimal ( '<STR_LIT>' ) ) <EOL> fees_paid = models . DecimalField ( max_digits = <NUM_LIT:6> , decimal_places = <NUM_LIT:2> , default = Decimal ( '<STR_LIT>' ) ) <EOL> assignments_finalized = models . BooleanField ( default = False ) <EOL> def update_country_preferences ( self , country_ids ) : <EOL> '''<STR_LIT>''' <EOL> seen = set ( ) <EOL> processed_country_ids = [ ] <EOL> country_preferences = [ ] <EOL> for rank , country_id in enumerate ( country_ids ) : <EOL> if not country_id or country_id in seen : <EOL> continue <EOL> seen . add ( country_id ) <EOL> processed_country_ids . append ( country_id ) <EOL> country_preferences . append ( <EOL> CountryPreference ( <EOL> school = self , <EOL> country_id = country_id , <EOL> rank = rank , <EOL> ) <EOL> ) <EOL> if country_preferences : <EOL> with transaction . atomic ( ) : <EOL> self . countrypreferences . clear ( ) <EOL> CountryPreference . objects . bulk_create ( country_preferences ) <EOL> return processed_country_ids <EOL> @ classmethod <EOL> def update_fees ( cls , ** kwargs ) : <EOL> school = kwargs [ '<STR_LIT>' ] <EOL> delegate_fees = cls . DELEGATE_FEE * sum ( ( <EOL> school . beginner_delegates , <EOL> school . intermediate_delegates , <EOL> school . advanced_delegates , <EOL> ) ) <EOL> total_fees = cls . REGISTRATION_FEE + delegate_fees <EOL> school . fees_owed = Decimal ( total_fees ) + Decimal ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def update_waitlist ( cls , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> school = kwargs [ '<STR_LIT>' ] <EOL> if not school . id and settings . CONFERENCE_WAITLIST_OPEN : <EOL> school . waitlist = True <EOL> @ property <EOL> def country_preference_ids ( self ) : <EOL> '''<STR_LIT>''' <EOL> return [ country . id for country in self . countrypreferences . all ( ) ] <EOL> @ country_preference_ids . setter <EOL> def country_preference_ids ( self , country_ids ) : <EOL> '''<STR_LIT>''' <EOL> self . _pending_country_preference_ids = country_ids <EOL> def save ( self , * args , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> super ( School , self ) . save ( * args , ** kwargs ) <EOL> if getattr ( self , '<STR_LIT>' , [ ] ) : <EOL> self . update_country_preferences ( self . _pending_country_preference_ids ) <EOL> self . _pending_country_preference_ids = [ ] <EOL> @ classmethod <EOL> def email_comments ( cls , ** kwargs ) : <EOL> school = kwargs [ '<STR_LIT>' ] <EOL> if kwargs [ '<STR_LIT>' ] and school . registration_comments : <EOL> send_mail ( '<STR_LIT>' + school . name , school . name + <EOL> '<STR_LIT>' <EOL> + school . registration_comments , '<STR_LIT>' , <EOL> [ '<STR_LIT>' ] , fail_silently = True ) <EOL> @ classmethod <EOL> def email_confirmation ( cls , ** kwargs ) : <EOL> if kwargs [ '<STR_LIT>' ] : <EOL> school = kwargs [ '<STR_LIT>' ] <EOL> if school . waitlist : <EOL> send_mail ( '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> [ school . primary_email ] , fail_silently = True ) <EOL> else : <EOL> send_mail ( '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> [ school . primary_email ] , fail_silently = True ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> class Meta : <EOL> db_table = u'<STR_LIT>' <EOL> pre_save . connect ( School . update_fees , sender = School ) <EOL> pre_save . connect ( School . update_waitlist , sender = School ) <EOL> post_save . connect ( School . email_comments , sender = School ) <EOL> post_save . connect ( School . email_confirmation , sender = School ) <EOL> class Assignment ( models . Model ) : <EOL> committee = models . ForeignKey ( Committee ) <EOL> country = models . ForeignKey ( Country ) <EOL> school = models . ForeignKey ( School , null = True , blank = True , default = None ) <EOL> rejected = models . BooleanField ( default = False ) <EOL> @ classmethod <EOL> def update_assignments ( cls , new_assignments ) : <EOL> '''<STR_LIT>''' <EOL> assignments = cls . objects . all ( ) . values ( ) <EOL> assignment_dict = { ( a [ '<STR_LIT>' ] , a [ '<STR_LIT>' ] ) : a <EOL> for a in assignments } <EOL> additions = [ ] <EOL> deletions = [ ] <EOL> def add ( committee , country , school , rejected ) : <EOL> additions . append ( cls ( <EOL> committee_id = committee , <EOL> country_id = country , <EOL> school_id = school , <EOL> rejected = rejected , <EOL> ) ) <EOL> def remove ( assignment_data ) : <EOL> deletions . append ( assignment_data [ '<STR_LIT:id>' ] ) <EOL> for committee , country , school , rejected in new_assignments : <EOL> key = ( committee , country ) <EOL> old_assignment = assignment_dict . get ( key ) <EOL> if not old_assignment : <EOL> add ( committee , country , school , rejected ) <EOL> continue <EOL> if old_assignment [ '<STR_LIT>' ] != school : <EOL> remove ( old_assignment ) <EOL> add ( committee , country , school , rejected ) <EOL> del assignment_dict [ key ] <EOL> for old_assignment in assignment_dict . values ( ) : <EOL> remove ( old_assignment ) <EOL> with transaction . atomic ( ) : <EOL> Assignment . objects . filter ( id__in = deletions ) . delete ( ) <EOL> Assignment . objects . bulk_create ( additions ) <EOL> def __unicode__ ( self ) : <EOL> return self . committee . name + "<STR_LIT>" + self . country . name + "<STR_LIT>" + ( self . school . name if self . school else "<STR_LIT>" ) <EOL> class Meta : <EOL> db_table = u'<STR_LIT>' <EOL> unique_together = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class CountryPreference ( models . Model ) : <EOL> school = models . ForeignKey ( School ) <EOL> country = models . ForeignKey ( Country , limit_choices_to = { '<STR_LIT>' : False } ) <EOL> rank = models . PositiveSmallIntegerField ( ) <EOL> def __unicode__ ( self ) : <EOL> return '<STR_LIT>' % ( self . school . name , self . country . name , self . rank ) <EOL> class Meta : <EOL> db_table = u'<STR_LIT>' <EOL> ordering = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> unique_together = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class Delegate ( models . Model ) : <EOL> assignment = models . ForeignKey ( Assignment , related_name = '<STR_LIT>' ) <EOL> name = models . CharField ( max_length = <NUM_LIT:64> , blank = True ) <EOL> email = models . EmailField ( blank = True ) <EOL> created_at = models . DateTimeField ( auto_now_add = True ) <EOL> summary = models . TextField ( default = '<STR_LIT>' , null = True ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> @ property <EOL> def country ( self ) : <EOL> return self . assignment . country <EOL> @ property <EOL> def committee ( self ) : <EOL> return self . assignment . committee <EOL> @ property <EOL> def school ( self ) : <EOL> return self . assignment . school <EOL> class Meta : <EOL> db_table = u'<STR_LIT>' <EOL> ordering = [ '<STR_LIT>' ] </s>
