<s> import pytz <EOL> from django . db . models import manager <EOL> import recurrence <EOL> from recurrence import choices <EOL> def to_utc ( dt ) : <EOL> if not dt : <EOL> return dt <EOL> if dt . tzinfo : <EOL> return dt . astimezone ( pytz . utc ) <EOL> return pytz . utc . localize ( dt ) <EOL> class RuleManager ( manager . Manager ) : <EOL> def to_rule_object ( self , rule_model ) : <EOL> rule_args = ( rule_model . freq , ) <EOL> rule_kwargs = { <EOL> '<STR_LIT>' : rule_model . interval , <EOL> '<STR_LIT>' : rule_model . wkst , <EOL> '<STR_LIT:count>' : rule_model . count , <EOL> '<STR_LIT>' : to_utc ( rule_model . until ) , <EOL> } <EOL> for param in recurrence . Rule . byparams : <EOL> if param == '<STR_LIT>' : <EOL> rule_kwargs [ param ] = ( map ( <EOL> lambda v : recurrence . Weekday ( * v ) , <EOL> rule_model . params . filter ( param = param ) . values_list ( <EOL> '<STR_LIT:value>' , '<STR_LIT:index>' ) ) or None ) <EOL> else : <EOL> rule_kwargs [ param ] = ( map ( <EOL> lambda v : v [ <NUM_LIT:0> ] , <EOL> rule_model . params . filter ( param = param ) . values_list ( <EOL> '<STR_LIT:value>' <EOL> ) <EOL> ) or None ) <EOL> return recurrence . Rule ( * rule_args , ** rule_kwargs ) <EOL> def create_from_rule_object ( self , mode , rule_obj , recurrence_model ) : <EOL> until = to_utc ( rule_obj . until ) <EOL> rule_model = self . create ( <EOL> recurrence = recurrence_model , mode = mode , <EOL> freq = rule_obj . freq , interval = rule_obj . interval , wkst = rule_obj . wkst , <EOL> count = rule_obj . count , until = until ) <EOL> for param in recurrence . Rule . byparams : <EOL> value_list = getattr ( rule_obj , param , None ) <EOL> if not value_list : <EOL> continue <EOL> if not hasattr ( value_list , '<STR_LIT>' ) : <EOL> value_list = [ value_list ] <EOL> for value in value_list : <EOL> if param == '<STR_LIT>' : <EOL> weekday = recurrence . to_weekday ( value ) <EOL> rule_model . params . create ( <EOL> param = param , value = weekday . number , index = weekday . index ) <EOL> else : <EOL> rule_model . params . create ( param = param , value = value ) <EOL> return rule_model <EOL> class RecurrenceManager ( manager . Manager ) : <EOL> def to_recurrence_object ( self , recurrence_model ) : <EOL> rrules , exrules , rdates , exdates = [ ] , [ ] , [ ] , [ ] <EOL> for rule_model in recurrence_model . rules . filter ( mode = choices . INCLUSION ) : <EOL> rrules . append ( rule_model . to_rule_object ( ) ) <EOL> for exrule_model in recurrence_model . rules . filter ( mode = choices . EXCLUSION ) : <EOL> exrules . append ( exrule_model . to_rule_object ( ) ) <EOL> for rdate_model in recurrence_model . dates . filter ( mode = choices . INCLUSION ) : <EOL> rdates . append ( to_utc ( rdate_model . dt ) ) <EOL> for exdate_model in recurrence_model . dates . filter ( mode = choices . EXCLUSION ) : <EOL> exdates . append ( to_utc ( exdate_model . dt ) ) <EOL> dtstart = to_utc ( recurrence_model . dtstart ) <EOL> dtend = to_utc ( recurrence_model . dtend ) <EOL> return recurrence . Recurrence ( <EOL> dtstart , dtend , rrules , exrules , rdates , exdates ) <EOL> def create_from_recurrence_object ( self , recurrence_obj ) : <EOL> from recurrence import models <EOL> recurrence_model = self . create ( <EOL> dtstart = to_utc ( recurrence_obj . dtstart ) , <EOL> dtend = to_utc ( recurrence_obj . dtend ) ) <EOL> for rrule in recurrence_obj . rrules : <EOL> models . Rule . objects . create_from_rule_object ( <EOL> choices . INCLUSION , rrule , recurrence_model ) <EOL> for exrule in recurrence_obj . exrules : <EOL> models . Rule . objects . create_from_rule_object ( <EOL> choices . EXCLUSION , exrule , recurrence_model ) <EOL> for dt in recurrence_obj . rdates : <EOL> recurrence_model . dates . create ( mode = choices . INCLUSION , dt = to_utc ( dt ) ) <EOL> for dt in recurrence_obj . exdates : <EOL> recurrence_model . dates . create ( mode = choices . EXCLUSION , dt = to_utc ( dt ) ) <EOL> return recurrence_model </s>
