<s> import re <EOL> import dateutil <EOL> from django . core . urlresolvers import NoReverseMatch , reverse <EOL> from django . http import Http404 <EOL> from django . shortcuts import get_object_or_404 <EOL> from django . utils . dateparse import parse_datetime <EOL> from django . views . generic . detail import SingleObjectMixin <EOL> from extra_views . contrib . mixins import SearchableListMixin , SortableListMixin <EOL> from . import settings <EOL> from . compat import User <EOL> from . humanize import ( DESCRIBE_BUY_PERIODS , DESCRIBE_UNLOCK_NOW , <EOL> DESCRIBE_UNLOCK_LATER , DESCRIBE_BALANCE ) <EOL> from . models import ( CartItem , Charge , Coupon , Organization , Plan , <EOL> Subscription , get_broker ) <EOL> from . utils import datetime_or_now , get_roles , start_of_day <EOL> from . extras import OrganizationMixinBase <EOL> class CartMixin ( object ) : <EOL> @ staticmethod <EOL> def insert_item ( request , ** kwargs ) : <EOL> created = False <EOL> inserted_item = None <EOL> template_item = None <EOL> email = kwargs . get ( '<STR_LIT:email>' , '<STR_LIT>' ) <EOL> if request . user . is_authenticated ( ) : <EOL> plan = get_object_or_404 ( Plan , slug = kwargs [ '<STR_LIT>' ] ) <EOL> queryset = CartItem . objects . get_cart ( <EOL> request . user , plan = plan ) . order_by ( '<STR_LIT>' ) <EOL> if queryset . exists ( ) : <EOL> template_item = queryset . first ( ) <EOL> if template_item : <EOL> created = False <EOL> inserted_item = template_item <EOL> if email : <EOL> if template_item . email : <EOL> if email != template_item . email : <EOL> created = True <EOL> inserted_item = CartItem . objects . create ( <EOL> plan = template_item . plan , <EOL> coupon = template_item . coupon , <EOL> nb_periods = template_item . nb_periods , <EOL> first_name = kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> last_name = kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> email = email , user = request . user ) <EOL> else : <EOL> inserted_item . first_name = kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> inserted_item . last_name = kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> inserted_item . email = email <EOL> inserted_item . save ( ) <EOL> else : <EOL> created = True <EOL> item_queryset = CartItem . objects . get_cart ( user = request . user , <EOL> plan = plan , email = email ) <EOL> if item_queryset . exists ( ) : <EOL> inserted_item = item_queryset . get ( ) <EOL> else : <EOL> redeemed = request . session . get ( '<STR_LIT>' , None ) <EOL> if redeemed : <EOL> redeemed = Coupon . objects . active ( <EOL> plan . organization , redeemed ) . first ( ) <EOL> inserted_item = CartItem . objects . create ( <EOL> plan = plan , coupon = redeemed , <EOL> email = email , user = request . user , <EOL> nb_periods = kwargs . get ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> first_name = kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> last_name = kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> else : <EOL> cart_items = [ ] <EOL> if request . session . has_key ( '<STR_LIT>' ) : <EOL> cart_items = request . session [ '<STR_LIT>' ] <EOL> for item in cart_items : <EOL> if item [ '<STR_LIT>' ] == kwargs [ '<STR_LIT>' ] : <EOL> if not template_item : <EOL> template_item = item <EOL> elif ( '<STR_LIT:email>' in template_item and '<STR_LIT:email>' in item <EOL> and len ( template_item [ '<STR_LIT:email>' ] ) > len ( item [ '<STR_LIT:email>' ] ) ) : <EOL> template_item = item <EOL> if template_item : <EOL> created = False <EOL> inserted_item = template_item <EOL> if email : <EOL> if template_item . email : <EOL> if email != template_item . email : <EOL> created = True <EOL> cart_items += [ { '<STR_LIT>' : template_item [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : template_item [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:email>' : email } ] <EOL> else : <EOL> inserted_item [ '<STR_LIT>' ] = kwargs . get ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> inserted_item [ '<STR_LIT>' ] = kwargs . get ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> inserted_item [ '<STR_LIT:email>' ] = email <EOL> else : <EOL> created = True <EOL> cart_items += [ { '<STR_LIT>' : kwargs [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:email>' : email } ] <EOL> request . session [ '<STR_LIT>' ] = cart_items <EOL> return inserted_item , created <EOL> class ChargeMixin ( SingleObjectMixin ) : <EOL> """<STR_LIT>""" <EOL> model = Charge <EOL> slug_field = '<STR_LIT>' <EOL> slug_url_kwarg = '<STR_LIT>' <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( ChargeMixin , self ) . get_context_data ( ** kwargs ) <EOL> charge = self . object <EOL> context . update ( get_charge_context ( charge ) ) <EOL> urls_charge = { <EOL> '<STR_LIT>' : reverse ( '<STR_LIT>' , args = ( charge , ) ) , <EOL> '<STR_LIT>' : reverse ( '<STR_LIT>' , args = ( charge , ) ) , <EOL> '<STR_LIT>' : reverse ( <EOL> '<STR_LIT>' , args = ( charge , ) ) , <EOL> } <EOL> try : <EOL> urls_charge . update ( { '<STR_LIT>' : reverse ( <EOL> '<STR_LIT>' , <EOL> args = ( charge . customer , charge , ) ) } ) <EOL> except NoReverseMatch : <EOL> pass <EOL> if '<STR_LIT>' in context : <EOL> if '<STR_LIT>' in context [ '<STR_LIT>' ] : <EOL> context [ '<STR_LIT>' ] [ '<STR_LIT>' ] . update ( urls_charge ) <EOL> else : <EOL> context [ '<STR_LIT>' ] . update ( { '<STR_LIT>' : urls_charge } ) <EOL> else : <EOL> context . update ( { '<STR_LIT>' : { '<STR_LIT>' : urls_charge } } ) <EOL> return context <EOL> class OrganizationMixin ( OrganizationMixinBase , settings . EXTRA_MIXIN ) : <EOL> pass <EOL> class DateRangeMixin ( object ) : <EOL> natural_period = dateutil . relativedelta . relativedelta ( months = - <NUM_LIT:1> ) <EOL> def cache_fields ( self , request ) : <EOL> self . ends_at = datetime_or_now ( <EOL> parse_datetime ( request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) . strip ( '<STR_LIT:">' ) ) ) <EOL> self . start_at = request . GET . get ( '<STR_LIT>' , None ) <EOL> if self . start_at : <EOL> self . start_at = datetime_or_now ( parse_datetime ( <EOL> self . start_at . strip ( '<STR_LIT:">' ) ) ) <EOL> else : <EOL> self . start_at = start_of_day ( self . ends_at <EOL> + self . natural_period ) + dateutil . relativedelta . relativedelta ( <EOL> days = <NUM_LIT:1> ) <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> self . cache_fields ( request ) <EOL> return super ( DateRangeMixin , self ) . get ( request , * args , ** kwargs ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( DateRangeMixin , self ) . get_context_data ( ** kwargs ) <EOL> context . update ( { <EOL> '<STR_LIT>' : self . start_at . isoformat ( ) , <EOL> '<STR_LIT>' : self . ends_at . isoformat ( ) } ) <EOL> return context <EOL> class ProviderMixin ( OrganizationMixin ) : <EOL> """<STR_LIT>""" <EOL> def get_organization ( self ) : <EOL> if self . organization_url_kwarg in self . kwargs : <EOL> queryset = Organization . objects . filter ( <EOL> slug = self . kwargs . get ( self . organization_url_kwarg ) ) <EOL> if queryset . exists ( ) : <EOL> return queryset . get ( ) <EOL> return get_broker ( ) <EOL> @ staticmethod <EOL> def get_provider ( ) : <EOL> return get_broker ( ) <EOL> @ property <EOL> def provider ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _provider = self . get_organization ( ) <EOL> return self . _provider <EOL> class CouponMixin ( ProviderMixin ) : <EOL> """<STR_LIT>""" <EOL> coupon_url_kwarg = '<STR_LIT>' <EOL> def get_coupon ( self ) : <EOL> return get_object_or_404 ( Coupon , <EOL> code = self . kwargs . get ( self . coupon_url_kwarg ) , <EOL> organization = self . provider ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( CouponMixin , self ) . get_context_data ( ** kwargs ) <EOL> context . update ( { '<STR_LIT>' : self . get_coupon ( ) } ) <EOL> return context <EOL> class MetricsMixin ( ProviderMixin ) : <EOL> """<STR_LIT>""" <EOL> def cache_fields ( self , request ) : <EOL> self . start_at = self . request . GET . get ( '<STR_LIT>' , None ) <EOL> if self . start_at : <EOL> self . start_at = parse_datetime ( self . start_at ) <EOL> self . start_at = datetime_or_now ( self . start_at ) <EOL> self . ends_at = self . request . GET . get ( '<STR_LIT>' , None ) <EOL> if self . ends_at : <EOL> self . ends_at = parse_datetime ( self . ends_at ) <EOL> self . ends_at = datetime_or_now ( self . ends_at ) <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> self . cache_fields ( request ) <EOL> return super ( MetricsMixin , self ) . get ( request , * args , ** kwargs ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( MetricsMixin , self ) . get_context_data ( ** kwargs ) <EOL> context . update ( { '<STR_LIT>' : self . start_at , <EOL> '<STR_LIT>' : self . ends_at } ) <EOL> return context <EOL> class SubscriptionMixin ( object ) : <EOL> model = Subscription <EOL> def get_queryset ( self ) : <EOL> kwargs = { } <EOL> start_at = self . request . GET . get ( '<STR_LIT>' , None ) <EOL> if start_at : <EOL> start_at = datetime_or_now ( parse_datetime ( start_at ) ) <EOL> kwargs . update ( { '<STR_LIT>' : start_at } ) <EOL> ends_at = self . request . GET . get ( '<STR_LIT>' , None ) <EOL> if ends_at : <EOL> ends_at = parse_datetime ( ends_at ) <EOL> ends_at = datetime_or_now ( ends_at ) <EOL> return Subscription . objects . filter ( <EOL> organization__slug = self . kwargs . get ( '<STR_LIT>' ) , <EOL> ends_at__gte = ends_at , ** kwargs ) <EOL> def get_object ( self ) : <EOL> queryset = self . get_queryset ( ) <EOL> if '<STR_LIT>' in self . kwargs : <EOL> plan = self . kwargs . get ( '<STR_LIT>' ) <EOL> else : <EOL> plan = self . kwargs . get ( '<STR_LIT>' ) <EOL> return queryset . filter ( plan__slug = plan ) . get ( ) <EOL> class SubscriptionSmartListMixin ( SearchableListMixin , SortableListMixin ) : <EOL> """<STR_LIT>""" <EOL> search_fields = [ '<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> sort_fields_aliases = [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> class UserSmartListMixin ( DateRangeMixin , SearchableListMixin , <EOL> SortableListMixin ) : <EOL> """<STR_LIT>""" <EOL> search_fields = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:email>' ] <EOL> sort_fields_aliases = [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:email>' , '<STR_LIT:email>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> class ChurnedQuerysetMixin ( ProviderMixin ) : <EOL> """<STR_LIT>""" <EOL> model = Subscription <EOL> def get_queryset ( self ) : <EOL> kwargs = { } <EOL> start_at = self . request . GET . get ( '<STR_LIT>' , None ) <EOL> if start_at : <EOL> start_at = datetime_or_now ( parse_datetime ( start_at ) ) <EOL> kwargs . update ( { '<STR_LIT>' : start_at } ) <EOL> ends_at = self . request . GET . get ( '<STR_LIT>' , None ) <EOL> if ends_at : <EOL> ends_at = parse_datetime ( ends_at ) <EOL> ends_at = datetime_or_now ( ends_at ) <EOL> return Subscription . objects . filter ( <EOL> plan__organization = self . provider , <EOL> ends_at__lt = ends_at , ** kwargs ) . order_by ( '<STR_LIT>' ) <EOL> class SubscribedQuerysetMixin ( ProviderMixin ) : <EOL> """<STR_LIT>""" <EOL> model = Subscription <EOL> def get_queryset ( self ) : <EOL> kwargs = { } <EOL> start_at = self . request . GET . get ( '<STR_LIT>' , None ) <EOL> if start_at : <EOL> start_at = datetime_or_now ( parse_datetime ( start_at ) ) <EOL> kwargs . update ( { '<STR_LIT>' : start_at } ) <EOL> ends_at = self . request . GET . get ( '<STR_LIT>' , None ) <EOL> if ends_at : <EOL> ends_at = parse_datetime ( ends_at ) <EOL> ends_at = datetime_or_now ( ends_at ) <EOL> return Subscription . objects . filter ( <EOL> plan__organization = self . provider , <EOL> ends_at__gte = ends_at , ** kwargs ) . order_by ( '<STR_LIT>' ) <EOL> class UserMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> SHORT_LIST_CUT_OFF = <NUM_LIT:8> <EOL> user_url_kwarg = '<STR_LIT:user>' <EOL> @ property <EOL> def user ( self ) : <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> try : <EOL> self . _user = User . objects . get ( <EOL> username = self . kwargs . get ( self . user_url_kwarg ) ) <EOL> except User . DoesNotExist : <EOL> self . _user = self . request . user <EOL> return self . _user <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( UserMixin , self ) . get_context_data ( ** kwargs ) <EOL> user = self . user <EOL> top_accessibles = [ ] <EOL> queryset = Organization . objects . accessible_by ( <EOL> user ) . filter ( is_active = True ) [ : self . SHORT_LIST_CUT_OFF + <NUM_LIT:1> ] <EOL> for organization in queryset : <EOL> top_accessibles += [ { '<STR_LIT>' : organization . printable_name , <EOL> '<STR_LIT:location>' : reverse ( '<STR_LIT>' , args = ( organization , ) ) } ] <EOL> if len ( queryset ) > self . SHORT_LIST_CUT_OFF : <EOL> top_accessibles += [ { '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT:location>' : reverse ( <EOL> '<STR_LIT>' , args = ( user , ) ) } ] <EOL> context . update ( { '<STR_LIT>' : top_accessibles } ) <EOL> return context <EOL> class RelationMixin ( OrganizationMixin , UserMixin ) : <EOL> """<STR_LIT>""" <EOL> def get_queryset ( self ) : <EOL> return get_roles ( self . kwargs . get ( '<STR_LIT>' ) ) . filter ( <EOL> organization = self . organization , user = self . user ) <EOL> def get_object ( self ) : <EOL> queryset = self . get_queryset ( ) <EOL> try : <EOL> return queryset . get ( ) <EOL> except queryset . model . DoesNotExist : <EOL> raise Http404 ( '<STR_LIT>' <EOL> % queryset . model . _meta . object_name ) <EOL> def as_html_description ( transaction ) : <EOL> """<STR_LIT>""" <EOL> provider = transaction . orig_organization <EOL> subscriber = transaction . dest_organization <EOL> look = re . match ( DESCRIBE_BUY_PERIODS % { <EOL> '<STR_LIT>' : r'<STR_LIT>' , '<STR_LIT>' : r'<STR_LIT>' , '<STR_LIT>' : r'<STR_LIT>' } , <EOL> transaction . descr ) <EOL> if not look : <EOL> look = re . match ( DESCRIBE_UNLOCK_NOW % { <EOL> '<STR_LIT>' : r'<STR_LIT>' , '<STR_LIT>' : r'<STR_LIT>' } , <EOL> transaction . descr ) <EOL> if not look : <EOL> look = re . match ( DESCRIBE_UNLOCK_LATER % { <EOL> '<STR_LIT>' : r'<STR_LIT>' , '<STR_LIT>' : r'<STR_LIT>' , <EOL> '<STR_LIT>' : r'<STR_LIT>' } , transaction . descr ) <EOL> if not look : <EOL> look = re . match ( DESCRIBE_BALANCE % { <EOL> '<STR_LIT>' : r'<STR_LIT>' } , transaction . descr ) <EOL> if not look : <EOL> look = re . match ( r'<STR_LIT>' , transaction . descr ) <EOL> if look : <EOL> link = '<STR_LIT>' % ( reverse ( '<STR_LIT>' , <EOL> args = ( subscriber , look . group ( '<STR_LIT>' ) , ) ) , look . group ( '<STR_LIT>' ) ) <EOL> return transaction . descr . replace ( look . group ( '<STR_LIT>' ) , link ) <EOL> return transaction . descr <EOL> plan_link = ( '<STR_LIT>' % ( <EOL> product_url ( provider , subscriber ) , <EOL> look . group ( '<STR_LIT>' ) , look . group ( '<STR_LIT>' ) ) ) <EOL> return transaction . descr . replace ( look . group ( '<STR_LIT>' ) , plan_link ) <EOL> def get_charge_context ( charge ) : <EOL> """<STR_LIT>""" <EOL> context = { '<STR_LIT>' : charge , <EOL> '<STR_LIT>' : charge . line_items , <EOL> '<STR_LIT>' : charge . customer , <EOL> '<STR_LIT>' : charge . broker } <EOL> return context <EOL> def product_url ( provider , subscriber = None ) : <EOL> """<STR_LIT>""" <EOL> current_uri = '<STR_LIT:/>' <EOL> if settings . PROVIDER_SITE_CALLABLE : <EOL> from . compat import import_string <EOL> site = import_string ( settings . PROVIDER_SITE_CALLABLE ) ( str ( provider ) ) <EOL> if site and site . domain : <EOL> scheme = '<STR_LIT>' <EOL> current_uri = '<STR_LIT>' % ( scheme , site . domain ) <EOL> else : <EOL> current_uri += '<STR_LIT>' % provider <EOL> elif provider != get_broker ( ) : <EOL> current_uri += '<STR_LIT>' % provider <EOL> current_uri += '<STR_LIT>' <EOL> if subscriber : <EOL> current_uri += '<STR_LIT>' % subscriber <EOL> return current_uri </s>
