<s> import copy <EOL> import datetime <EOL> import re <EOL> from collections import defaultdict <EOL> from decimal import Decimal <EOL> import logging <EOL> import json <EOL> import cStringIO <EOL> import pytz <EOL> from couchdbkit import ResourceNotFound <EOL> import dateutil <EOL> from django . core . paginator import Paginator <EOL> from django . views . generic import View <EOL> from django . db . models import Sum <EOL> from django . conf import settings <EOL> from django . template . loader import render_to_string <EOL> from django . utils . decorators import method_decorator <EOL> from django . utils . http import urlsafe_base64_decode <EOL> from django . utils . safestring import mark_safe <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponseRedirect , HttpResponse , Http404 <EOL> from django . shortcuts import redirect , render <EOL> from django . contrib import messages <EOL> from django . contrib . auth . views import password_reset_confirm <EOL> from django . views . decorators . http import require_POST <EOL> from PIL import Image <EOL> from django . utils . translation import ugettext as _ , ugettext_lazy <EOL> from django . contrib . auth . models import User <EOL> from corehq . apps . app_manager . dbaccessors import get_apps_in_domain <EOL> from corehq . apps . case_search . models import CaseSearchConfig , CaseSearchConfigJSON <EOL> from corehq . apps . hqwebapp . templatetags . hq_shared_tags import toggle_js_domain_cachebuster <EOL> from corehq . const import USER_DATE_FORMAT <EOL> from corehq . tabs . tabclasses import ProjectSettingsTab <EOL> from custom . dhis2 . forms import Dhis2SettingsForm <EOL> from custom . dhis2 . models import Dhis2Settings <EOL> from corehq . apps . accounting . async_handlers import Select2BillingInfoHandler <EOL> from corehq . apps . accounting . invoicing import DomainWireInvoiceFactory <EOL> from corehq . apps . hqwebapp . tasks import send_mail_async <EOL> from corehq . apps . style . decorators import ( <EOL> use_bootstrap3 , <EOL> use_jquery_ui , <EOL> use_jquery_ui_multiselect , <EOL> use_select2 , <EOL> ) <EOL> from corehq . apps . accounting . exceptions import ( <EOL> NewSubscriptionError , <EOL> PaymentRequestError , <EOL> ) <EOL> from corehq . apps . accounting . payment_handlers import ( <EOL> BulkStripePaymentHandler , <EOL> CreditStripePaymentHandler , <EOL> InvoiceStripePaymentHandler , <EOL> ) <EOL> from corehq . apps . accounting . subscription_changes import DomainDowngradeStatusHandler <EOL> from corehq . apps . accounting . forms import EnterprisePlanContactForm <EOL> from corehq . apps . accounting . utils import ( <EOL> get_change_status , get_privileges , fmt_dollar_amount , <EOL> quantize_accounting_decimal , get_customer_cards , <EOL> log_accounting_error , <EOL> ) <EOL> from corehq . apps . hqwebapp . async_handler import AsyncHandlerMixin <EOL> from corehq . apps . smsbillables . async_handlers import SMSRatesAsyncHandler , SMSRatesSelect2AsyncHandler <EOL> from corehq . apps . smsbillables . forms import SMSRateCalculatorForm <EOL> from corehq . apps . users . models import Invitation , CouchUser <EOL> from corehq . apps . fixtures . models import FixtureDataType <EOL> from corehq . toggles import NAMESPACE_DOMAIN , all_toggles , CAN_EDIT_EULA , TRANSFER_DOMAIN <EOL> from dimagi . utils . couch . resource_conflict import retry_resource <EOL> from corehq import privileges , feature_previews <EOL> from django_prbac . utils import has_privilege <EOL> from corehq . apps . accounting . models import ( <EOL> Subscription , CreditLine , SoftwareProductType , SubscriptionType , <EOL> DefaultProductPlan , SoftwarePlanEdition , BillingAccount , <EOL> BillingAccountType , <EOL> Invoice , BillingRecord , InvoicePdf , PaymentMethodType , <EOL> EntryPoint , WireInvoice , SoftwarePlanVisibility , FeatureType , <EOL> StripePaymentMethod , LastPayment , <EOL> UNLIMITED_FEATURE_USAGE , <EOL> ) <EOL> from corehq . apps . accounting . usage import FeatureUsageCalculator <EOL> from corehq . apps . accounting . user_text import ( <EOL> get_feature_name , <EOL> PricingTable , <EOL> DESC_BY_EDITION , <EOL> get_feature_recurring_interval , <EOL> ) <EOL> from corehq . apps . domain . calculations import CALCS , CALC_FNS , CALC_ORDER , dom_calc <EOL> from corehq . apps . domain . decorators import ( <EOL> domain_admin_required , login_required , require_superuser , login_and_domain_required <EOL> ) <EOL> from corehq . apps . domain . forms import ( <EOL> DomainGlobalSettingsForm , DomainMetadataForm , SnapshotSettingsForm , <EOL> SnapshotApplicationForm , DomainInternalForm , PrivacySecurityForm , <EOL> ConfirmNewSubscriptionForm , ProBonoForm , EditBillingAccountInfoForm , <EOL> ConfirmSubscriptionRenewalForm , SnapshotFixtureForm , TransferDomainForm , <EOL> SelectSubscriptionTypeForm , INTERNAL_SUBSCRIPTION_MANAGEMENT_FORMS , AdvancedExtendedTrialForm , <EOL> ContractedPartnerForm , DimagiOnlyEnterpriseForm ) <EOL> from corehq . apps . domain . models import ( <EOL> Domain , <EOL> LICENSES , <EOL> TransferDomainRequest , <EOL> ) <EOL> from corehq . apps . domain . utils import normalize_domain_name <EOL> from corehq . apps . hqwebapp . views import BaseSectionPageView , BasePageView , CRUDPaginatedViewMixin <EOL> from corehq . apps . domain . forms import ProjectSettingsForm <EOL> from dimagi . utils . decorators . memoized import memoized <EOL> from dimagi . utils . web import get_ip , json_response , get_site_domain <EOL> from corehq . apps . users . decorators import require_can_edit_web_users <EOL> from corehq . apps . repeaters . forms import GenericRepeaterForm , FormRepeaterForm <EOL> from corehq . apps . repeaters . models import Repeater , FormRepeater , CaseRepeater , ShortFormRepeater , AppStructureRepeater , RepeatRecord , repeater_types , RegisterGenerator <EOL> from corehq . apps . repeaters . dbaccessors import ( <EOL> get_paged_repeat_records , <EOL> get_repeat_record_count , <EOL> ) <EOL> from corehq . apps . repeaters . const import ( <EOL> RECORD_FAILURE_STATE , <EOL> RECORD_PENDING_STATE , <EOL> RECORD_SUCCESS_STATE , <EOL> ) <EOL> from corehq . apps . reports . generic import GenericTabularReport <EOL> from corehq . apps . reports . dispatcher import DomainReportDispatcher <EOL> from corehq . apps . reports . datatables import DataTablesHeader , DataTablesColumn <EOL> from dimagi . utils . post import simple_post <EOL> from toggle . models import Toggle <EOL> from corehq . apps . hqwebapp . tasks import send_html_email_async <EOL> from corehq . apps . hqwebapp . signals import clear_login_attempts <EOL> PAYMENT_ERROR_MESSAGES = { <EOL> <NUM_LIT> : ugettext_lazy ( '<STR_LIT>' ) , <EOL> <NUM_LIT> : ugettext_lazy ( '<STR_LIT>' ) , <EOL> <NUM_LIT> : ugettext_lazy ( '<STR_LIT>' ) , <EOL> <NUM_LIT> : ugettext_lazy ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> } <EOL> @ login_required <EOL> def select ( request , domain_select_template = '<STR_LIT>' , do_not_redirect = False ) : <EOL> domains_for_user = Domain . active_for_user ( request . user ) <EOL> if not domains_for_user : <EOL> return redirect ( '<STR_LIT>' ) <EOL> email = request . couch_user . get_email ( ) <EOL> open_invitations = [ e for e in Invitation . by_email ( email ) if not e . is_expired ] <EOL> additional_context = { <EOL> '<STR_LIT>' : domains_for_user , <EOL> '<STR_LIT>' : open_invitations , <EOL> '<STR_LIT>' : { '<STR_LIT>' : _ ( '<STR_LIT>' ) } , <EOL> } <EOL> last_visited_domain = request . session . get ( '<STR_LIT>' ) <EOL> if open_invitations or do_not_redirect or not last_visited_domain : <EOL> return render ( request , domain_select_template , additional_context ) <EOL> else : <EOL> domain = Domain . get_by_name ( last_visited_domain ) <EOL> if domain and domain . is_active : <EOL> if ( <EOL> request . couch_user . is_member_of ( domain ) or domain . is_public <EOL> or ( request . user . is_superuser and not domain . restrict_superusers ) <EOL> or domain . is_snapshot <EOL> ) : <EOL> try : <EOL> from corehq . apps . dashboard . views import dashboard_default <EOL> return dashboard_default ( request , last_visited_domain ) <EOL> except Http404 : <EOL> pass <EOL> del request . session [ '<STR_LIT>' ] <EOL> return render ( request , domain_select_template , additional_context ) <EOL> class DomainViewMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> strict_domain_fetching = False <EOL> @ property <EOL> @ memoized <EOL> def domain ( self ) : <EOL> domain = self . args [ <NUM_LIT:0> ] if len ( self . args ) > <NUM_LIT:0> else self . kwargs . get ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> return normalize_domain_name ( domain ) <EOL> @ property <EOL> @ memoized <EOL> def domain_object ( self ) : <EOL> domain = Domain . get_by_name ( self . domain , strict = self . strict_domain_fetching ) <EOL> if not domain : <EOL> raise Http404 ( ) <EOL> return domain <EOL> class LoginAndDomainMixin ( object ) : <EOL> @ method_decorator ( login_and_domain_required ) <EOL> def dispatch ( self , * args , ** kwargs ) : <EOL> return super ( LoginAndDomainMixin , self ) . dispatch ( * args , ** kwargs ) <EOL> class SubscriptionUpgradeRequiredView ( LoginAndDomainMixin , BasePageView , <EOL> DomainViewMixin ) : <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> template_name = "<STR_LIT>" <EOL> @ property <EOL> def page_url ( self ) : <EOL> return self . request . get_full_path <EOL> @ property <EOL> def page_name ( self ) : <EOL> return _ ( "<STR_LIT>" ) % { <EOL> '<STR_LIT>' : self . feature_name , <EOL> } <EOL> @ property <EOL> def is_domain_admin ( self ) : <EOL> if not hasattr ( self . request , '<STR_LIT>' ) : <EOL> return False <EOL> return self . request . couch_user . is_domain_admin ( self . domain ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . domain , <EOL> '<STR_LIT>' : self . feature_name , <EOL> '<STR_LIT>' : self . required_plan_name , <EOL> '<STR_LIT>' : reverse ( SelectPlanView . urlname , <EOL> args = [ self . domain ] ) , <EOL> '<STR_LIT>' : self . is_domain_admin , <EOL> } <EOL> @ property <EOL> def missing_privilege ( self ) : <EOL> return self . args [ <NUM_LIT:1> ] <EOL> @ property <EOL> def feature_name ( self ) : <EOL> return privileges . Titles . get_name_from_privilege ( self . missing_privilege ) <EOL> @ property <EOL> def required_plan_name ( self ) : <EOL> return DefaultProductPlan . get_lowest_edition_by_domain ( <EOL> self . domain_object , [ self . missing_privilege ] <EOL> ) <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> self . request = request <EOL> self . args = args <EOL> return super ( SubscriptionUpgradeRequiredView , self ) . get ( <EOL> request , * args , ** kwargs <EOL> ) <EOL> class BaseDomainView ( LoginAndDomainMixin , BaseSectionPageView , DomainViewMixin ) : <EOL> @ property <EOL> def main_context ( self ) : <EOL> main_context = super ( BaseDomainView , self ) . main_context <EOL> main_context . update ( { <EOL> '<STR_LIT>' : self . domain , <EOL> } ) <EOL> return main_context <EOL> @ property <EOL> @ memoized <EOL> def page_url ( self ) : <EOL> if self . urlname : <EOL> return reverse ( self . urlname , args = [ self . domain ] ) <EOL> class BaseProjectSettingsView ( BaseDomainView ) : <EOL> section_name = ugettext_lazy ( "<STR_LIT>" ) <EOL> template_name = "<STR_LIT>" <EOL> @ property <EOL> def main_context ( self ) : <EOL> main_context = super ( BaseProjectSettingsView , self ) . main_context <EOL> main_context . update ( { <EOL> '<STR_LIT>' : ProjectSettingsTab ( <EOL> self . request , <EOL> domain = self . domain , <EOL> couch_user = self . request . couch_user , <EOL> project = self . request . project <EOL> ) , <EOL> '<STR_LIT>' : True , <EOL> } ) <EOL> return main_context <EOL> @ property <EOL> @ memoized <EOL> def section_url ( self ) : <EOL> return reverse ( EditMyProjectSettingsView . urlname , args = [ self . domain ] ) <EOL> class DefaultProjectSettingsView ( BaseDomainView ) : <EOL> urlname = '<STR_LIT>' <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> if request . couch_user . is_domain_admin ( self . domain ) : <EOL> return HttpResponseRedirect ( reverse ( EditBasicProjectInfoView . urlname , args = [ self . domain ] ) ) <EOL> return HttpResponseRedirect ( reverse ( EditMyProjectSettingsView . urlname , args = [ self . domain ] ) ) <EOL> class BaseAdminProjectSettingsView ( BaseProjectSettingsView ) : <EOL> """<STR_LIT>""" <EOL> @ method_decorator ( domain_admin_required ) <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( BaseProjectSettingsView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> class BaseEditProjectInfoView ( BaseAdminProjectSettingsView ) : <EOL> """<STR_LIT>""" <EOL> strict_domain_fetching = True <EOL> @ property <EOL> def autocomplete_fields ( self ) : <EOL> return [ ] <EOL> @ property <EOL> def main_context ( self ) : <EOL> context = super ( BaseEditProjectInfoView , self ) . main_context <EOL> context . update ( { <EOL> '<STR_LIT>' : self . autocomplete_fields , <EOL> '<STR_LIT>' : self . domain_object . commtrack_enabled , <EOL> '<STR_LIT>' : self . domain_object . call_center_config . enabled , <EOL> '<STR_LIT>' : self . domain_object . cloudcare_releases , <EOL> } ) <EOL> return context <EOL> class EditBasicProjectInfoView ( BaseEditProjectInfoView ) : <EOL> template_name = '<STR_LIT>' <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> @ method_decorator ( domain_admin_required ) <EOL> @ use_select2 <EOL> @ use_bootstrap3 <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( BaseProjectSettingsView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> def can_user_see_meta ( self ) : <EOL> return self . request . couch_user . is_previewer ( ) <EOL> @ property <EOL> def can_use_custom_logo ( self ) : <EOL> return has_privilege ( self . request , privileges . CUSTOM_BRANDING ) <EOL> @ property <EOL> @ memoized <EOL> def basic_info_form ( self ) : <EOL> initial = { <EOL> '<STR_LIT>' : self . domain_object . hr_name or self . domain_object . name , <EOL> '<STR_LIT>' : self . domain_object . default_timezone , <EOL> '<STR_LIT>' : json . dumps ( self . domain_object . case_sharing ) , <EOL> '<STR_LIT>' : self . domain_object . call_center_config . enabled , <EOL> '<STR_LIT>' : self . initial_call_center_type , <EOL> '<STR_LIT>' : self . initial_call_center_case_owner , <EOL> '<STR_LIT>' : self . domain_object . call_center_config . case_type , <EOL> '<STR_LIT>' : self . domain_object . commtrack_enabled , <EOL> } <EOL> if self . request . method == '<STR_LIT:POST>' : <EOL> if self . can_user_see_meta : <EOL> return DomainMetadataForm ( <EOL> self . request . POST , <EOL> self . request . FILES , <EOL> user = self . request . couch_user , <EOL> domain = self . domain_object . name , <EOL> can_use_custom_logo = self . can_use_custom_logo , <EOL> ) <EOL> return DomainGlobalSettingsForm ( <EOL> self . request . POST , <EOL> self . request . FILES , <EOL> domain = self . domain_object . name , <EOL> can_use_custom_logo = self . can_use_custom_logo <EOL> ) <EOL> if self . can_user_see_meta : <EOL> initial . update ( { <EOL> '<STR_LIT>' : self . domain_object . is_test , <EOL> '<STR_LIT>' : self . domain_object . cloudcare_releases , <EOL> } ) <EOL> return DomainMetadataForm ( <EOL> can_use_custom_logo = self . can_use_custom_logo , <EOL> user = self . request . couch_user , <EOL> domain = self . domain_object . name , <EOL> initial = initial <EOL> ) <EOL> return DomainGlobalSettingsForm ( <EOL> initial = initial , <EOL> domain = self . domain_object . name , <EOL> can_use_custom_logo = self . can_use_custom_logo <EOL> ) <EOL> @ property <EOL> @ memoized <EOL> def initial_call_center_case_owner ( self ) : <EOL> config = self . domain_object . call_center_config <EOL> if config . use_user_location_as_owner : <EOL> if config . user_location_ancestor_level == <NUM_LIT:1> : <EOL> return DomainGlobalSettingsForm . USE_PARENT_LOCATION_CHOICE <EOL> return DomainGlobalSettingsForm . USE_LOCATION_CHOICE <EOL> return self . domain_object . call_center_config . case_owner_id <EOL> @ property <EOL> @ memoized <EOL> def initial_call_center_type ( self ) : <EOL> if self . domain_object . call_center_config . use_fixtures : <EOL> return DomainGlobalSettingsForm . CASES_AND_FIXTURES_CHOICE <EOL> return DomainGlobalSettingsForm . CASES_ONLY_CHOICE <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . basic_info_form , <EOL> } <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if self . basic_info_form . is_valid ( ) : <EOL> if self . basic_info_form . save ( request , self . domain_object ) : <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> else : <EOL> messages . error ( request , _ ( "<STR_LIT>" ) ) <EOL> return self . get ( request , * args , ** kwargs ) <EOL> class EditMyProjectSettingsView ( BaseProjectSettingsView ) : <EOL> template_name = '<STR_LIT>' <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> @ method_decorator ( login_and_domain_required ) <EOL> @ use_bootstrap3 <EOL> def dispatch ( self , * args , ** kwargs ) : <EOL> return super ( LoginAndDomainMixin , self ) . dispatch ( * args , ** kwargs ) <EOL> @ property <EOL> @ memoized <EOL> def my_project_settings_form ( self ) : <EOL> initial = { '<STR_LIT>' : self . domain_object . default_timezone } <EOL> if self . domain_membership : <EOL> initial . update ( { <EOL> '<STR_LIT>' : self . domain_membership . override_global_tz , <EOL> '<STR_LIT>' : ( self . domain_membership . timezone if self . domain_membership . override_global_tz <EOL> else self . domain_object . default_timezone ) , <EOL> } ) <EOL> else : <EOL> initial . update ( { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : initial [ "<STR_LIT>" ] , <EOL> } ) <EOL> if self . request . method == '<STR_LIT:POST>' : <EOL> return ProjectSettingsForm ( self . request . POST , initial = initial ) <EOL> return ProjectSettingsForm ( initial = initial ) <EOL> @ property <EOL> @ memoized <EOL> def domain_membership ( self ) : <EOL> return self . request . couch_user . get_domain_membership ( self . domain ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . my_project_settings_form , <EOL> '<STR_LIT>' : self . domain_membership . override_global_tz if self . domain_membership else False , <EOL> '<STR_LIT>' : not self . domain_membership , <EOL> } <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if self . my_project_settings_form . is_valid ( ) : <EOL> self . my_project_settings_form . save ( self . request . couch_user , self . domain ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return self . get ( request , * args , ** kwargs ) <EOL> class EditDhis2SettingsView ( BaseProjectSettingsView ) : <EOL> template_name = '<STR_LIT>' <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> @ property <EOL> @ memoized <EOL> def dhis2_settings_form ( self ) : <EOL> settings_ = Dhis2Settings . for_domain ( self . domain_object . name ) <EOL> initial = settings_ . dhis2 if settings_ else { '<STR_LIT>' : False } <EOL> if self . request . method == '<STR_LIT:POST>' : <EOL> return Dhis2SettingsForm ( self . request . POST , initial = initial ) <EOL> return Dhis2SettingsForm ( initial = initial ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . dhis2_settings_form , <EOL> } <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if self . dhis2_settings_form . is_valid ( ) : <EOL> if self . dhis2_settings_form . save ( self . domain_object ) : <EOL> messages . success ( request , _ ( '<STR_LIT>' ) ) <EOL> else : <EOL> messages . error ( request , _ ( '<STR_LIT>' ) ) <EOL> return self . get ( request , * args , ** kwargs ) <EOL> @ require_POST <EOL> @ require_can_edit_web_users <EOL> def drop_repeater ( request , domain , repeater_id ) : <EOL> rep = Repeater . get ( repeater_id ) <EOL> rep . retire ( ) <EOL> messages . success ( request , "<STR_LIT>" ) <EOL> return HttpResponseRedirect ( reverse ( DomainForwardingOptionsView . urlname , args = [ domain ] ) ) <EOL> @ require_POST <EOL> @ require_can_edit_web_users <EOL> def test_repeater ( request , domain ) : <EOL> url = request . POST [ "<STR_LIT:url>" ] <EOL> repeater_type = request . POST [ '<STR_LIT>' ] <EOL> format = request . POST . get ( '<STR_LIT>' , None ) <EOL> repeater_class = repeater_types [ repeater_type ] <EOL> form = GenericRepeaterForm ( <EOL> { "<STR_LIT:url>" : url , "<STR_LIT>" : format } , <EOL> domain = domain , <EOL> repeater_class = repeater_class <EOL> ) <EOL> if form . is_valid ( ) : <EOL> url = form . cleaned_data [ "<STR_LIT:url>" ] <EOL> format = format or RegisterGenerator . default_format_by_repeater ( repeater_class ) <EOL> generator_class = RegisterGenerator . generator_class_by_repeater_format ( repeater_class , format ) <EOL> generator = generator_class ( repeater_class ( ) ) <EOL> fake_post = generator . get_test_payload ( ) <EOL> headers = generator . get_headers ( ) <EOL> try : <EOL> resp = simple_post ( fake_post , url , headers = headers ) <EOL> if <NUM_LIT:200> <= resp . status_code < <NUM_LIT> : <EOL> return HttpResponse ( json . dumps ( { "<STR_LIT:success>" : True , <EOL> "<STR_LIT>" : resp . content , <EOL> "<STR_LIT:status>" : resp . status_code } ) ) <EOL> else : <EOL> return HttpResponse ( json . dumps ( { "<STR_LIT:success>" : False , <EOL> "<STR_LIT>" : resp . content , <EOL> "<STR_LIT:status>" : resp . status_code } ) ) <EOL> except Exception , e : <EOL> errors = str ( e ) <EOL> return HttpResponse ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : errors } ) ) <EOL> else : <EOL> return HttpResponse ( json . dumps ( { "<STR_LIT:success>" : False , "<STR_LIT>" : "<STR_LIT>" } ) ) <EOL> def autocomplete_fields ( request , field ) : <EOL> prefix = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> results = Domain . field_by_prefix ( field , prefix ) <EOL> return HttpResponse ( json . dumps ( results ) ) <EOL> def logo ( request , domain ) : <EOL> logo = Domain . get_by_name ( domain ) . get_custom_logo ( ) <EOL> if logo is None : <EOL> raise Http404 ( ) <EOL> return HttpResponse ( logo [ <NUM_LIT:0> ] , content_type = logo [ <NUM_LIT:1> ] ) <EOL> class DomainAccountingSettings ( BaseAdminProjectSettingsView ) : <EOL> @ method_decorator ( login_and_domain_required ) <EOL> @ use_bootstrap3 <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( DomainAccountingSettings , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> @ memoized <EOL> def product ( self ) : <EOL> return SoftwareProductType . get_type_by_domain ( self . domain_object ) <EOL> @ property <EOL> @ memoized <EOL> def account ( self ) : <EOL> return BillingAccount . get_account_by_domain ( self . domain ) <EOL> @ property <EOL> def current_subscription ( self ) : <EOL> return Subscription . get_subscribed_plan_by_domain ( self . domain_object ) [ <NUM_LIT:1> ] <EOL> class DomainSubscriptionView ( DomainAccountingSettings ) : <EOL> urlname = '<STR_LIT>' <EOL> template_name = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> @ property <EOL> def can_purchase_credits ( self ) : <EOL> return self . request . couch_user . is_domain_admin ( self . domain ) <EOL> @ property <EOL> @ memoized <EOL> def plan ( self ) : <EOL> plan_version , subscription = Subscription . get_subscribed_plan_by_domain ( self . domain_object ) <EOL> date_end = None <EOL> next_subscription = { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT:name>' : None , <EOL> '<STR_LIT>' : None , <EOL> } <EOL> cards = None <EOL> if subscription : <EOL> cards = get_customer_cards ( self . account , self . request . user . username , self . domain ) <EOL> date_end = ( subscription . date_end . strftime ( USER_DATE_FORMAT ) <EOL> if subscription . date_end is not None else "<STR_LIT>" ) <EOL> if subscription . date_end is not None : <EOL> if subscription . is_renewed : <EOL> next_product = self . get_product_summary ( subscription . next_subscription . plan_version , <EOL> self . account , <EOL> subscription ) <EOL> next_subscription . update ( { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : subscription . next_subscription . date_start . strftime ( USER_DATE_FORMAT ) , <EOL> '<STR_LIT:name>' : subscription . next_subscription . plan_version . plan . name , <EOL> '<STR_LIT>' : next_product [ '<STR_LIT>' ] , <EOL> } ) <EOL> else : <EOL> days_left = ( subscription . date_end - datetime . date . today ( ) ) . days <EOL> next_subscription . update ( { <EOL> '<STR_LIT>' : days_left <= <NUM_LIT:30> , <EOL> '<STR_LIT>' : reverse ( SubscriptionRenewalView . urlname , args = [ self . domain ] ) , <EOL> } ) <EOL> info = { <EOL> '<STR_LIT>' : [ self . get_product_summary ( plan_version , self . account , subscription ) ] , <EOL> '<STR_LIT>' : self . get_feature_summary ( plan_version , self . account , subscription ) , <EOL> '<STR_LIT>' : self . _fmt_credit ( self . _credit_grand_total ( <EOL> CreditLine . get_credits_by_subscription_and_features ( <EOL> subscription <EOL> ) if subscription else None <EOL> ) ) , <EOL> '<STR_LIT>' : self . _fmt_credit ( self . _credit_grand_total ( <EOL> CreditLine . get_credits_for_account ( <EOL> self . account <EOL> ) if self . account else None <EOL> ) ) , <EOL> '<STR_LIT>' : "<STR_LIT>" % plan_version . plan . edition . lower ( ) , <EOL> '<STR_LIT>' : subscription . do_not_invoice if subscription is not None else False , <EOL> '<STR_LIT>' : subscription . is_trial if subscription is not None else False , <EOL> '<STR_LIT>' : ( subscription . date_start . strftime ( USER_DATE_FORMAT ) <EOL> if subscription is not None else None ) , <EOL> '<STR_LIT>' : date_end , <EOL> '<STR_LIT>' : cards , <EOL> '<STR_LIT>' : next_subscription , <EOL> } <EOL> info [ '<STR_LIT>' ] = ( <EOL> any ( <EOL> product_info [ '<STR_LIT>' ] and product_info [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> for product_info in info [ '<STR_LIT>' ] <EOL> ) <EOL> or info [ '<STR_LIT>' ] and info [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> ) <EOL> info . update ( plan_version . user_facing_description ) <EOL> return info <EOL> def _fmt_credit ( self , credit_amount = None ) : <EOL> if credit_amount is None : <EOL> return { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> } <EOL> return { <EOL> '<STR_LIT>' : fmt_dollar_amount ( credit_amount ) , <EOL> '<STR_LIT>' : credit_amount != Decimal ( '<STR_LIT>' ) , <EOL> } <EOL> def _credit_grand_total ( self , credit_lines ) : <EOL> return sum ( [ c . balance for c in credit_lines ] ) if credit_lines else Decimal ( '<STR_LIT>' ) <EOL> def get_product_summary ( self , plan_version , account , subscription ) : <EOL> product_rate = plan_version . product_rate <EOL> product_type = product_rate . product . product_type <EOL> return { <EOL> '<STR_LIT:name>' : product_type , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) % product_rate . monthly_fee , <EOL> '<STR_LIT:type>' : product_type , <EOL> '<STR_LIT>' : self . _fmt_credit ( self . _credit_grand_total ( <EOL> CreditLine . get_credits_by_subscription_and_features ( <EOL> subscription , product_type = SoftwareProductType . ANY <EOL> ) if subscription else None <EOL> ) ) , <EOL> '<STR_LIT>' : self . _fmt_credit ( self . _credit_grand_total ( <EOL> CreditLine . get_credits_for_account ( <EOL> account , product_type = SoftwareProductType . ANY <EOL> ) if account else None <EOL> ) ) , <EOL> } <EOL> def get_feature_summary ( self , plan_version , account , subscription ) : <EOL> def _get_feature_info ( feature_rate ) : <EOL> usage = FeatureUsageCalculator ( feature_rate , self . domain ) . get_usage ( ) <EOL> feature_type = feature_rate . feature . feature_type <EOL> return { <EOL> '<STR_LIT:name>' : get_feature_name ( feature_type , self . product ) , <EOL> '<STR_LIT>' : usage , <EOL> '<STR_LIT>' : ( <EOL> feature_rate . monthly_limit - usage <EOL> if feature_rate . monthly_limit != UNLIMITED_FEATURE_USAGE <EOL> else _ ( '<STR_LIT>' ) <EOL> ) , <EOL> '<STR_LIT:type>' : feature_type , <EOL> '<STR_LIT>' : get_feature_recurring_interval ( feature_type ) , <EOL> '<STR_LIT>' : self . _fmt_credit ( self . _credit_grand_total ( <EOL> CreditLine . get_credits_by_subscription_and_features ( <EOL> subscription , feature_type = feature_type <EOL> ) if subscription else None <EOL> ) ) , <EOL> '<STR_LIT>' : self . _fmt_credit ( self . _credit_grand_total ( <EOL> CreditLine . get_credits_for_account ( <EOL> account , feature_type = feature_type <EOL> ) if account else None <EOL> ) ) , <EOL> } <EOL> return map ( _get_feature_info , plan_version . feature_rates . all ( ) ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . plan , <EOL> '<STR_LIT>' : reverse ( SelectPlanView . urlname , args = [ self . domain ] ) , <EOL> '<STR_LIT>' : self . can_purchase_credits , <EOL> '<STR_LIT>' : reverse ( CreditsStripePaymentView . urlname , args = [ self . domain ] ) , <EOL> '<STR_LIT>' : reverse ( CreditsWireInvoiceView . urlname , args = [ self . domain ] ) , <EOL> '<STR_LIT>' : settings . STRIPE_PUBLIC_KEY , <EOL> '<STR_LIT>' : PAYMENT_ERROR_MESSAGES , <EOL> '<STR_LIT>' : reverse ( SMSRatesView . urlname , <EOL> args = [ self . domain ] ) , <EOL> '<STR_LIT>' : self . request . couch_user . username , <EOL> '<STR_LIT>' : any ( <EOL> feature [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) <EOL> for feature in self . plan . get ( '<STR_LIT>' ) <EOL> ) <EOL> } <EOL> class EditExistingBillingAccountView ( DomainAccountingSettings , AsyncHandlerMixin ) : <EOL> template_name = '<STR_LIT>' <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> async_handlers = [ <EOL> Select2BillingInfoHandler , <EOL> ] <EOL> @ property <EOL> @ memoized <EOL> def billing_info_form ( self ) : <EOL> if self . request . method == '<STR_LIT:POST>' : <EOL> return EditBillingAccountInfoForm ( <EOL> self . account , self . domain , self . request . couch_user . username , data = self . request . POST <EOL> ) <EOL> return EditBillingAccountInfoForm ( self . account , self . domain , self . request . couch_user . username ) <EOL> @ use_select2 <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> if self . account is None : <EOL> raise Http404 ( ) <EOL> return super ( EditExistingBillingAccountView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . billing_info_form , <EOL> '<STR_LIT>' : self . _get_cards ( ) , <EOL> '<STR_LIT>' : settings . STRIPE_PUBLIC_KEY , <EOL> '<STR_LIT>' : reverse ( CardsView . url_name , args = [ self . domain ] ) , <EOL> } <EOL> def _get_cards ( self ) : <EOL> user = self . request . user . username <EOL> payment_method , new_payment_method = StripePaymentMethod . objects . get_or_create ( <EOL> web_user = user , <EOL> method_type = PaymentMethodType . STRIPE , <EOL> ) <EOL> return payment_method . all_cards_serialized ( self . account ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if self . async_response is not None : <EOL> return self . async_response <EOL> if self . billing_info_form . is_valid ( ) : <EOL> is_saved = self . billing_info_form . save ( ) <EOL> if not is_saved : <EOL> messages . error ( <EOL> request , _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> else : <EOL> messages . success ( <EOL> request , _ ( "<STR_LIT>" ) <EOL> ) <EOL> return HttpResponseRedirect ( reverse ( EditExistingBillingAccountView . urlname , args = [ self . domain ] ) ) <EOL> return self . get ( request , * args , ** kwargs ) <EOL> class DomainBillingStatementsView ( DomainAccountingSettings , CRUDPaginatedViewMixin ) : <EOL> template_name = '<STR_LIT>' <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> limit_text = ugettext_lazy ( "<STR_LIT>" ) <EOL> empty_notification = ugettext_lazy ( "<STR_LIT>" ) <EOL> loading_message = ugettext_lazy ( "<STR_LIT>" ) <EOL> @ property <EOL> def parameters ( self ) : <EOL> return self . request . POST if self . request . method == '<STR_LIT:POST>' else self . request . GET <EOL> @ property <EOL> def stripe_cards ( self ) : <EOL> return get_customer_cards ( self . account , self . request . user . username , self . domain ) <EOL> @ property <EOL> def show_hidden ( self ) : <EOL> if not self . request . user . is_superuser : <EOL> return False <EOL> return bool ( self . request . POST . get ( '<STR_LIT>' ) ) <EOL> @ property <EOL> def show_unpaid ( self ) : <EOL> try : <EOL> return json . loads ( self . request . POST . get ( '<STR_LIT>' ) ) <EOL> except TypeError : <EOL> return False <EOL> @ property <EOL> def invoices ( self ) : <EOL> invoices = Invoice . objects . filter ( subscription__subscriber__domain = self . domain ) <EOL> if not self . show_hidden : <EOL> invoices = invoices . filter ( is_hidden = False ) <EOL> if self . show_unpaid : <EOL> invoices = invoices . filter ( date_paid__exact = None ) <EOL> return invoices . order_by ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ property <EOL> def total ( self ) : <EOL> return self . paginated_invoices . count <EOL> @ property <EOL> @ memoized <EOL> def paginated_invoices ( self ) : <EOL> return Paginator ( self . invoices , self . limit ) <EOL> @ property <EOL> def total_balance ( self ) : <EOL> """<STR_LIT>""" <EOL> invoices = ( Invoice . objects <EOL> . filter ( subscription__subscriber__domain = self . domain ) <EOL> . filter ( date_paid__exact = None ) <EOL> . filter ( is_hidden = False ) ) <EOL> return invoices . aggregate ( <EOL> total_balance = Sum ( '<STR_LIT>' ) <EOL> ) . get ( '<STR_LIT>' ) or <NUM_LIT> <EOL> @ property <EOL> def column_names ( self ) : <EOL> return [ <EOL> _ ( "<STR_LIT>" ) , <EOL> _ ( "<STR_LIT>" ) , <EOL> _ ( "<STR_LIT>" ) , <EOL> _ ( "<STR_LIT>" ) , <EOL> _ ( "<STR_LIT>" ) , <EOL> _ ( "<STR_LIT>" ) , <EOL> ] <EOL> @ property <EOL> def page_context ( self ) : <EOL> pagination_context = self . pagination_context <EOL> pagination_context . update ( { <EOL> '<STR_LIT>' : settings . STRIPE_PUBLIC_KEY , <EOL> '<STR_LIT>' : PAYMENT_ERROR_MESSAGES , <EOL> '<STR_LIT>' : reverse ( <EOL> InvoiceStripePaymentView . urlname , <EOL> args = [ self . domain ] , <EOL> ) , <EOL> '<STR_LIT>' : reverse ( <EOL> BulkStripePaymentView . urlname , <EOL> args = [ self . domain ] , <EOL> ) , <EOL> '<STR_LIT>' : reverse ( <EOL> WireInvoiceView . urlname , <EOL> args = [ self . domain ] , <EOL> ) , <EOL> '<STR_LIT>' : self . stripe_cards , <EOL> '<STR_LIT>' : self . total_balance , <EOL> } ) <EOL> return pagination_context <EOL> @ property <EOL> def can_pay_invoices ( self ) : <EOL> return self . request . couch_user . is_domain_admin ( self . domain ) <EOL> @ property <EOL> def paginated_list ( self ) : <EOL> for invoice in self . paginated_invoices . page ( self . page ) . object_list : <EOL> try : <EOL> last_billing_record = BillingRecord . objects . filter ( <EOL> invoice = invoice <EOL> ) . latest ( '<STR_LIT>' ) <EOL> if invoice . is_paid : <EOL> payment_status = ( _ ( "<STR_LIT>" ) <EOL> % invoice . date_paid . strftime ( USER_DATE_FORMAT ) ) <EOL> payment_class = "<STR_LIT>" <EOL> else : <EOL> payment_status = _ ( "<STR_LIT>" ) <EOL> payment_class = "<STR_LIT>" <EOL> date_due = ( <EOL> ( invoice . date_due . strftime ( USER_DATE_FORMAT ) <EOL> if not invoice . is_paid else _ ( "<STR_LIT>" ) ) <EOL> if invoice . date_due else _ ( "<STR_LIT:None>" ) <EOL> ) <EOL> yield { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : invoice . id , <EOL> '<STR_LIT>' : invoice . invoice_number , <EOL> '<STR_LIT:start>' : invoice . date_start . strftime ( USER_DATE_FORMAT ) , <EOL> '<STR_LIT:end>' : invoice . date_end . strftime ( USER_DATE_FORMAT ) , <EOL> '<STR_LIT>' : invoice . subscription . plan_version . user_facing_description , <EOL> '<STR_LIT>' : payment_status , <EOL> '<STR_LIT>' : payment_class , <EOL> '<STR_LIT>' : date_due , <EOL> '<STR_LIT>' : reverse ( <EOL> BillingStatementPdfView . urlname , <EOL> args = [ self . domain , last_billing_record . pdf_data_id ] <EOL> ) , <EOL> '<STR_LIT>' : ( not invoice . is_paid <EOL> and self . can_pay_invoices ) , <EOL> '<STR_LIT>' : "<STR_LIT:%s>" % quantize_accounting_decimal ( invoice . balance ) , <EOL> } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> except BillingRecord . DoesNotExist : <EOL> log_accounting_error ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % { <EOL> '<STR_LIT>' : invoice . id , <EOL> '<STR_LIT>' : self . domain , <EOL> } <EOL> ) <EOL> def refresh_item ( self , item_id ) : <EOL> pass <EOL> def post ( self , * args , ** kwargs ) : <EOL> return self . paginate_crud_response <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> if self . account is None : <EOL> raise Http404 ( ) <EOL> return super ( DomainBillingStatementsView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> class BaseStripePaymentView ( DomainAccountingSettings ) : <EOL> http_method_names = [ '<STR_LIT>' ] <EOL> @ property <EOL> def account ( self ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ property <EOL> @ memoized <EOL> def domain_admin ( self ) : <EOL> if self . request . couch_user . is_domain_admin ( self . domain ) : <EOL> return self . request . couch_user . username <EOL> else : <EOL> raise PaymentRequestError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> def get_or_create_payment_method ( self ) : <EOL> return StripePaymentMethod . objects . get_or_create ( <EOL> web_user = self . domain_admin , <EOL> method_type = PaymentMethodType . STRIPE , <EOL> ) [ <NUM_LIT:0> ] <EOL> def get_payment_handler ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> try : <EOL> payment_handler = self . get_payment_handler ( ) <EOL> response = payment_handler . process_request ( request ) <EOL> except PaymentRequestError as e : <EOL> log_accounting_error ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % { <EOL> '<STR_LIT>' : self . domain , <EOL> '<STR_LIT>' : self . request . user . username , <EOL> '<STR_LIT:error>' : e , <EOL> } <EOL> ) <EOL> response = { <EOL> '<STR_LIT:error>' : { <EOL> '<STR_LIT:message>' : _ ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> } <EOL> } <EOL> return json_response ( response ) <EOL> class CreditsStripePaymentView ( BaseStripePaymentView ) : <EOL> urlname = '<STR_LIT>' <EOL> @ property <EOL> @ memoized <EOL> def account ( self ) : <EOL> return BillingAccount . get_or_create_account_by_domain ( <EOL> self . domain , <EOL> created_by = self . request . user . username , <EOL> account_type = BillingAccountType . USER_CREATED , <EOL> entry_point = EntryPoint . SELF_STARTED , <EOL> last_payment_method = LastPayment . CC_ONE_TIME , <EOL> ) [ <NUM_LIT:0> ] <EOL> def get_payment_handler ( self ) : <EOL> return CreditStripePaymentHandler ( <EOL> self . get_or_create_payment_method ( ) , <EOL> self . domain , <EOL> self . account , <EOL> subscription = Subscription . get_subscribed_plan_by_domain ( self . domain_object ) [ <NUM_LIT:1> ] , <EOL> post_data = self . request . POST . copy ( ) , <EOL> ) <EOL> class CreditsWireInvoiceView ( DomainAccountingSettings ) : <EOL> http_method_names = [ '<STR_LIT>' ] <EOL> urlname = '<STR_LIT>' <EOL> @ method_decorator ( login_and_domain_required ) <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( CreditsWireInvoiceView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> emails = request . POST . get ( '<STR_LIT>' , [ ] ) . split ( ) <EOL> amount = Decimal ( request . POST . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> wire_invoice_factory = DomainWireInvoiceFactory ( request . domain , contact_emails = emails ) <EOL> try : <EOL> wire_invoice_factory . create_wire_credits_invoice ( self . _get_items ( request ) , amount ) <EOL> except Exception as e : <EOL> return json_response ( { '<STR_LIT:error>' : { '<STR_LIT:message>' : str ( e ) } } ) <EOL> return json_response ( { '<STR_LIT:success>' : True } ) <EOL> def _get_items ( self , request ) : <EOL> product_type = SoftwareProductType . get_type_by_domain ( Domain . get_by_name ( self . domain ) ) <EOL> features = [ { '<STR_LIT:type>' : get_feature_name ( feature_type [ <NUM_LIT:0> ] , product_type ) , <EOL> '<STR_LIT>' : Decimal ( request . POST . get ( feature_type [ <NUM_LIT:0> ] , <NUM_LIT:0> ) ) } <EOL> for feature_type in FeatureType . CHOICES <EOL> if Decimal ( request . POST . get ( feature_type [ <NUM_LIT:0> ] , <NUM_LIT:0> ) ) > <NUM_LIT:0> ] <EOL> products = [ { '<STR_LIT:type>' : pt [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : Decimal ( request . POST . get ( pt [ <NUM_LIT:0> ] , <NUM_LIT:0> ) ) } <EOL> for pt in SoftwareProductType . CHOICES <EOL> if Decimal ( request . POST . get ( pt [ <NUM_LIT:0> ] , <NUM_LIT:0> ) ) > <NUM_LIT:0> ] <EOL> return products + features <EOL> class InvoiceStripePaymentView ( BaseStripePaymentView ) : <EOL> urlname = '<STR_LIT>' <EOL> @ property <EOL> @ memoized <EOL> def invoice ( self ) : <EOL> try : <EOL> invoice_id = self . request . POST [ '<STR_LIT>' ] <EOL> except IndexError : <EOL> raise PaymentRequestError ( "<STR_LIT>" ) <EOL> try : <EOL> return Invoice . objects . get ( pk = invoice_id ) <EOL> except Invoice . DoesNotExist : <EOL> raise PaymentRequestError ( <EOL> "<STR_LIT>" <EOL> % invoice_id <EOL> ) <EOL> @ property <EOL> def account ( self ) : <EOL> return self . invoice . subscription . account <EOL> def get_payment_handler ( self ) : <EOL> return InvoiceStripePaymentHandler ( <EOL> self . get_or_create_payment_method ( ) , self . domain , self . invoice <EOL> ) <EOL> class BulkStripePaymentView ( BaseStripePaymentView ) : <EOL> urlname = '<STR_LIT>' <EOL> @ property <EOL> def account ( self ) : <EOL> return BillingAccount . get_account_by_domain ( self . domain ) <EOL> def get_payment_handler ( self ) : <EOL> return BulkStripePaymentHandler ( <EOL> self . get_or_create_payment_method ( ) , self . domain <EOL> ) <EOL> class WireInvoiceView ( View ) : <EOL> http_method_names = [ '<STR_LIT>' ] <EOL> urlname = '<STR_LIT>' <EOL> @ method_decorator ( login_and_domain_required ) <EOL> @ method_decorator ( domain_admin_required ) <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( WireInvoiceView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> emails = request . POST . get ( '<STR_LIT>' , [ ] ) . split ( ) <EOL> balance = Decimal ( request . POST . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> wire_invoice_factory = DomainWireInvoiceFactory ( request . domain , contact_emails = emails ) <EOL> try : <EOL> wire_invoice_factory . create_wire_invoice ( balance ) <EOL> except Exception , e : <EOL> return json_response ( { '<STR_LIT:error>' : { '<STR_LIT:message>' , e } } ) <EOL> return json_response ( { '<STR_LIT:success>' : True } ) <EOL> class BillingStatementPdfView ( View ) : <EOL> urlname = '<STR_LIT>' <EOL> @ method_decorator ( login_and_domain_required ) <EOL> @ method_decorator ( domain_admin_required ) <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( BillingStatementPdfView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> domain = args [ <NUM_LIT:0> ] <EOL> statement_id = kwargs . get ( '<STR_LIT>' ) <EOL> if statement_id is None or domain is None : <EOL> raise Http404 ( ) <EOL> try : <EOL> invoice_pdf = InvoicePdf . get ( statement_id ) <EOL> except ResourceNotFound : <EOL> raise Http404 ( ) <EOL> try : <EOL> if invoice_pdf . is_wire : <EOL> invoice = WireInvoice . objects . get ( <EOL> pk = invoice_pdf . invoice_id , <EOL> domain = domain <EOL> ) <EOL> else : <EOL> invoice = Invoice . objects . get ( <EOL> pk = invoice_pdf . invoice_id , <EOL> subscription__subscriber__domain = domain <EOL> ) <EOL> except ( Invoice . DoesNotExist , WireInvoice . DoesNotExist ) : <EOL> raise Http404 ( ) <EOL> if invoice . is_wire : <EOL> edition = '<STR_LIT>' <EOL> else : <EOL> edition = DESC_BY_EDITION [ invoice . subscription . plan_version . plan . edition ] [ '<STR_LIT:name>' ] <EOL> filename = "<STR_LIT>" % { <EOL> '<STR_LIT>' : invoice_pdf . _id , <EOL> '<STR_LIT>' : domain , <EOL> '<STR_LIT>' : edition , <EOL> '<STR_LIT:filename>' : invoice_pdf . get_filename ( invoice ) , <EOL> } <EOL> try : <EOL> data = invoice_pdf . get_data ( invoice ) <EOL> response = HttpResponse ( data , content_type = '<STR_LIT>' ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' % filename <EOL> except Exception as e : <EOL> log_accounting_error ( '<STR_LIT>' % e ) <EOL> return HttpResponse ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> return response <EOL> class InternalSubscriptionManagementView ( BaseAdminProjectSettingsView ) : <EOL> template_name = '<STR_LIT>' <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> form_classes = INTERNAL_SUBSCRIPTION_MANAGEMENT_FORMS <EOL> @ method_decorator ( require_superuser ) <EOL> @ use_bootstrap3 <EOL> @ use_jquery_ui <EOL> @ use_select2 <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( InternalSubscriptionManagementView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ method_decorator ( require_superuser ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> form = self . get_post_form <EOL> if form . is_valid ( ) : <EOL> try : <EOL> form . process_subscription_management ( ) <EOL> return HttpResponseRedirect ( reverse ( DomainSubscriptionView . urlname , args = [ self . domain ] ) ) <EOL> except NewSubscriptionError as e : <EOL> messages . error ( self . request , e . message ) <EOL> return self . get ( request , * args , ** kwargs ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . is_form_editable , <EOL> '<STR_LIT>' : Subscription . get_subscribed_plan_by_domain ( self . domain ) [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : self . select_subscription_type_form , <EOL> '<STR_LIT>' : self . slug_to_form . values ( ) , <EOL> '<STR_LIT>' : datetime . date . today ( ) , <EOL> } <EOL> @ property <EOL> def get_post_form ( self ) : <EOL> return self . slug_to_form [ self . request . POST . get ( '<STR_LIT>' ) ] <EOL> @ property <EOL> @ memoized <EOL> def slug_to_form ( self ) : <EOL> def create_form ( form_class ) : <EOL> if self . request . method == '<STR_LIT:POST>' and form_class . slug == self . request . POST . get ( '<STR_LIT>' ) : <EOL> return form_class ( self . domain , self . request . couch_user . username , self . request . POST ) <EOL> return form_class ( self . domain , self . request . couch_user . username ) <EOL> return { form_class . slug : create_form ( form_class ) for form_class in self . form_classes } <EOL> @ property <EOL> @ memoized <EOL> def select_subscription_type_form ( self ) : <EOL> if self . request . method == '<STR_LIT:POST>' and '<STR_LIT>' in self . request . POST : <EOL> return SelectSubscriptionTypeForm ( { <EOL> '<STR_LIT>' : self . request . POST [ '<STR_LIT>' ] , <EOL> } ) <EOL> subscription_type = None <EOL> subscription = Subscription . get_subscribed_plan_by_domain ( self . domain_object ) [ <NUM_LIT:1> ] <EOL> if subscription is None : <EOL> subscription_type = None <EOL> else : <EOL> plan = subscription . plan_version . plan <EOL> if subscription . service_type == SubscriptionType . IMPLEMENTATION : <EOL> subscription_type = ContractedPartnerForm . slug <EOL> elif plan . edition == SoftwarePlanEdition . ENTERPRISE : <EOL> subscription_type = DimagiOnlyEnterpriseForm . slug <EOL> elif plan . edition == SoftwarePlanEdition . ADVANCED : <EOL> subscription_type = AdvancedExtendedTrialForm . slug <EOL> return SelectSubscriptionTypeForm ( <EOL> { '<STR_LIT>' : subscription_type } , <EOL> disable_input = not self . is_form_editable , <EOL> ) <EOL> @ property <EOL> def is_form_editable ( self ) : <EOL> return not self . slug_to_form [ ContractedPartnerForm . slug ] . is_uneditable <EOL> class SelectPlanView ( DomainAccountingSettings ) : <EOL> template_name = '<STR_LIT>' <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> step_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> edition = None <EOL> lead_text = ugettext_lazy ( "<STR_LIT>" ) <EOL> @ property <EOL> def edition_name ( self ) : <EOL> if self . edition : <EOL> return DESC_BY_EDITION [ self . edition ] [ '<STR_LIT:name>' ] <EOL> @ property <EOL> def is_non_ops_superuser ( self ) : <EOL> if not self . request . couch_user . is_superuser : <EOL> return False <EOL> return not has_privilege ( self . request , privileges . ACCOUNTING_ADMIN ) <EOL> @ property <EOL> def parent_pages ( self ) : <EOL> return [ <EOL> { <EOL> '<STR_LIT:title>' : DomainSubscriptionView . page_title , <EOL> '<STR_LIT:url>' : reverse ( DomainSubscriptionView . urlname , args = [ self . domain ] ) , <EOL> } <EOL> ] <EOL> @ property <EOL> def steps ( self ) : <EOL> edition_name = u"<STR_LIT>" % self . edition_name if self . edition_name else "<STR_LIT>" <EOL> return [ <EOL> { <EOL> '<STR_LIT:title>' : _ ( u"<STR_LIT>" ) % { <EOL> "<STR_LIT>" : edition_name <EOL> } , <EOL> '<STR_LIT:url>' : reverse ( SelectPlanView . urlname , args = [ self . domain ] ) , <EOL> } <EOL> ] <EOL> @ property <EOL> def main_context ( self ) : <EOL> context = super ( SelectPlanView , self ) . main_context <EOL> context . update ( { <EOL> '<STR_LIT>' : self . steps , <EOL> '<STR_LIT>' : self . step_title , <EOL> '<STR_LIT>' : self . lead_text , <EOL> } ) <EOL> return context <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : PricingTable . get_table_by_product ( self . product , domain = self . domain ) , <EOL> '<STR_LIT>' : ( self . current_subscription . plan_version . plan . edition . lower ( ) <EOL> if self . current_subscription is not None <EOL> and not self . current_subscription . is_trial <EOL> else "<STR_LIT>" ) , <EOL> '<STR_LIT>' : self . is_non_ops_superuser , <EOL> } <EOL> class EditPrivacySecurityView ( BaseAdminProjectSettingsView ) : <EOL> template_name = "<STR_LIT>" <EOL> urlname = "<STR_LIT>" <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> @ method_decorator ( domain_admin_required ) <EOL> @ use_bootstrap3 <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( BaseProjectSettingsView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> @ memoized <EOL> def privacy_form ( self ) : <EOL> initial = { <EOL> "<STR_LIT>" : self . domain_object . secure_submissions , <EOL> "<STR_LIT>" : self . domain_object . restrict_superusers , <EOL> "<STR_LIT>" : self . domain_object . allow_domain_requests , <EOL> "<STR_LIT>" : self . domain_object . hipaa_compliant , <EOL> "<STR_LIT>" : self . domain_object . secure_sessions , <EOL> "<STR_LIT>" : self . domain_object . two_factor_auth , <EOL> } <EOL> if self . request . method == '<STR_LIT:POST>' : <EOL> return PrivacySecurityForm ( self . request . POST , initial = initial , <EOL> user_name = self . request . couch_user . username , <EOL> domain = self . request . domain ) <EOL> return PrivacySecurityForm ( initial = initial , user_name = self . request . couch_user . username , <EOL> domain = self . request . domain ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . privacy_form <EOL> } <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if self . privacy_form . is_valid ( ) : <EOL> self . privacy_form . save ( self . domain_object ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return self . get ( request , * args , ** kwargs ) <EOL> class SelectedEnterprisePlanView ( SelectPlanView ) : <EOL> template_name = '<STR_LIT>' <EOL> urlname = '<STR_LIT>' <EOL> step_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> edition = SoftwarePlanEdition . ENTERPRISE <EOL> @ property <EOL> def steps ( self ) : <EOL> last_steps = super ( SelectedEnterprisePlanView , self ) . steps <EOL> last_steps . append ( { <EOL> '<STR_LIT:title>' : _ ( "<STR_LIT>" ) , <EOL> '<STR_LIT:url>' : reverse ( SelectedEnterprisePlanView . urlname , args = [ self . domain ] ) , <EOL> } ) <EOL> return last_steps <EOL> @ property <EOL> @ memoized <EOL> def is_not_redirect ( self ) : <EOL> return not '<STR_LIT>' in self . request . POST <EOL> @ property <EOL> @ memoized <EOL> def enterprise_contact_form ( self ) : <EOL> if self . request . method == '<STR_LIT:POST>' and self . is_not_redirect : <EOL> return EnterprisePlanContactForm ( self . domain , self . request . couch_user , data = self . request . POST ) <EOL> return EnterprisePlanContactForm ( self . domain , self . request . couch_user ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . enterprise_contact_form , <EOL> } <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if self . is_not_redirect and self . enterprise_contact_form . is_valid ( ) : <EOL> self . enterprise_contact_form . send_message ( ) <EOL> messages . success ( request , _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> return HttpResponseRedirect ( reverse ( DomainSubscriptionView . urlname , args = [ self . domain ] ) ) <EOL> return self . get ( request , * args , ** kwargs ) <EOL> class ConfirmSelectedPlanView ( SelectPlanView ) : <EOL> template_name = '<STR_LIT>' <EOL> urlname = '<STR_LIT>' <EOL> step_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> @ property <EOL> def steps ( self ) : <EOL> last_steps = super ( ConfirmSelectedPlanView , self ) . steps <EOL> last_steps . append ( { <EOL> '<STR_LIT:title>' : _ ( "<STR_LIT>" ) , <EOL> '<STR_LIT:url>' : reverse ( SelectPlanView . urlname , args = [ self . domain ] ) , <EOL> } ) <EOL> return last_steps <EOL> @ property <EOL> @ memoized <EOL> def edition ( self ) : <EOL> edition = self . request . POST . get ( '<STR_LIT>' ) . title ( ) <EOL> if edition not in [ e [ <NUM_LIT:0> ] for e in SoftwarePlanEdition . CHOICES ] : <EOL> raise Http404 ( ) <EOL> return edition <EOL> @ property <EOL> @ memoized <EOL> def selected_plan_version ( self ) : <EOL> return DefaultProductPlan . get_default_plan_by_domain ( self . domain , self . edition ) . plan . get_version ( ) <EOL> @ property <EOL> def downgrade_messages ( self ) : <EOL> current_plan_version , subscription = Subscription . get_subscribed_plan_by_domain ( self . domain_object ) <EOL> if subscription is None : <EOL> current_plan_version = None <EOL> downgrades = get_change_status ( current_plan_version , self . selected_plan_version ) [ <NUM_LIT:1> ] <EOL> downgrade_handler = DomainDowngradeStatusHandler ( <EOL> self . domain_object , self . selected_plan_version , downgrades , <EOL> ) <EOL> return downgrade_handler . get_response ( ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . downgrade_messages , <EOL> '<STR_LIT>' : ( self . current_subscription . plan_version . user_facing_description <EOL> if self . current_subscription is not None else None ) , <EOL> '<STR_LIT>' : ( self . edition == SoftwarePlanEdition . COMMUNITY <EOL> and self . current_subscription is None ) , <EOL> } <EOL> @ property <EOL> def main_context ( self ) : <EOL> context = super ( ConfirmSelectedPlanView , self ) . main_context <EOL> context . update ( { <EOL> '<STR_LIT>' : self . selected_plan_version . user_facing_description , <EOL> } ) <EOL> return context <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> return HttpResponseRedirect ( reverse ( SelectPlanView . urlname , args = [ self . domain ] ) ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if self . edition == SoftwarePlanEdition . ENTERPRISE : <EOL> return HttpResponseRedirect ( reverse ( SelectedEnterprisePlanView . urlname , args = [ self . domain ] ) ) <EOL> return super ( ConfirmSelectedPlanView , self ) . get ( request , * args , ** kwargs ) <EOL> class ConfirmBillingAccountInfoView ( ConfirmSelectedPlanView , AsyncHandlerMixin ) : <EOL> template_name = '<STR_LIT>' <EOL> urlname = '<STR_LIT>' <EOL> step_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> is_new = False <EOL> async_handlers = [ <EOL> Select2BillingInfoHandler , <EOL> ] <EOL> @ use_select2 <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( ConfirmBillingAccountInfoView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> def steps ( self ) : <EOL> last_steps = super ( ConfirmBillingAccountInfoView , self ) . steps <EOL> last_steps . append ( { <EOL> '<STR_LIT:title>' : _ ( "<STR_LIT>" ) , <EOL> '<STR_LIT:url>' : reverse ( ConfirmBillingAccountInfoView . urlname , args = [ self . domain ] ) , <EOL> } ) <EOL> return last_steps <EOL> @ property <EOL> @ memoized <EOL> def account ( self ) : <EOL> if self . current_subscription : <EOL> return self . current_subscription . account <EOL> account , self . is_new = BillingAccount . get_or_create_account_by_domain ( <EOL> self . domain , <EOL> created_by = self . request . couch_user . username , <EOL> account_type = BillingAccountType . USER_CREATED , <EOL> entry_point = EntryPoint . SELF_STARTED , <EOL> ) <EOL> return account <EOL> @ property <EOL> def payment_method ( self ) : <EOL> user = self . request . user . username <EOL> payment_method , __ = StripePaymentMethod . objects . get_or_create ( <EOL> web_user = user , <EOL> method_type = PaymentMethodType . STRIPE , <EOL> ) <EOL> return payment_method <EOL> @ property <EOL> @ memoized <EOL> def is_form_post ( self ) : <EOL> return '<STR_LIT>' in self . request . POST <EOL> @ property <EOL> @ memoized <EOL> def billing_account_info_form ( self ) : <EOL> initial = None <EOL> if self . edition == SoftwarePlanEdition . ENTERPRISE and self . request . couch_user . is_superuser : <EOL> initial = { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> } <EOL> if self . request . method == '<STR_LIT:POST>' and self . is_form_post : <EOL> return ConfirmNewSubscriptionForm ( <EOL> self . account , self . domain , self . request . couch_user . username , <EOL> self . selected_plan_version , self . current_subscription , data = self . request . POST , initial = initial <EOL> ) <EOL> return ConfirmNewSubscriptionForm ( self . account , self . domain , self . request . couch_user . username , <EOL> self . selected_plan_version , self . current_subscription , initial = initial ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . billing_account_info_form , <EOL> '<STR_LIT>' : settings . STRIPE_PUBLIC_KEY , <EOL> '<STR_LIT>' : self . payment_method . all_cards_serialized ( self . account ) <EOL> } <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if self . async_response is not None : <EOL> return self . async_response <EOL> if self . edition == SoftwarePlanEdition . ENTERPRISE and not self . request . couch_user . is_superuser : <EOL> return HttpResponseRedirect ( reverse ( SelectedEnterprisePlanView . urlname , args = [ self . domain ] ) ) <EOL> if self . is_form_post and self . billing_account_info_form . is_valid ( ) : <EOL> is_saved = self . billing_account_info_form . save ( ) <EOL> software_plan_name = DESC_BY_EDITION [ self . selected_plan_version . plan . edition ] [ '<STR_LIT:name>' ] . encode ( '<STR_LIT:utf-8>' ) <EOL> if not is_saved : <EOL> messages . error ( <EOL> request , _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % software_plan_name ) <EOL> else : <EOL> messages . success ( <EOL> request , _ ( "<STR_LIT>" <EOL> % software_plan_name ) <EOL> ) <EOL> return HttpResponseRedirect ( reverse ( DomainSubscriptionView . urlname , args = [ self . domain ] ) ) <EOL> return super ( ConfirmBillingAccountInfoView , self ) . post ( request , * args , ** kwargs ) <EOL> class SubscriptionMixin ( object ) : <EOL> @ property <EOL> @ memoized <EOL> def subscription ( self ) : <EOL> subscription = Subscription . get_subscribed_plan_by_domain ( self . domain_object ) [ <NUM_LIT:1> ] <EOL> if subscription is None : <EOL> raise Http404 <EOL> if subscription . is_renewed : <EOL> raise Http404 <EOL> return subscription <EOL> class SubscriptionRenewalView ( SelectPlanView , SubscriptionMixin ) : <EOL> urlname = "<STR_LIT>" <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> step_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> @ property <EOL> def lead_text ( self ) : <EOL> return ugettext_lazy ( "<STR_LIT>" <EOL> . format ( plan = self . current_subscription . plan_version . plan . edition ) ) <EOL> @ property <EOL> def main_context ( self ) : <EOL> context = super ( SubscriptionRenewalView , self ) . main_context <EOL> context . update ( { '<STR_LIT>' : True } ) <EOL> return context <EOL> @ property <EOL> def page_context ( self ) : <EOL> context = super ( SubscriptionRenewalView , self ) . page_context <EOL> current_privs = get_privileges ( self . subscription . plan_version ) <EOL> plan = DefaultProductPlan . get_lowest_edition_by_domain ( <EOL> self . domain , current_privs , return_plan = False , <EOL> ) . lower ( ) <EOL> context [ '<STR_LIT>' ] = ( plan <EOL> if self . current_subscription is not None <EOL> and not self . current_subscription . is_trial <EOL> else "<STR_LIT>" ) <EOL> return context <EOL> class ConfirmSubscriptionRenewalView ( DomainAccountingSettings , AsyncHandlerMixin , SubscriptionMixin ) : <EOL> template_name = '<STR_LIT>' <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> async_handlers = [ <EOL> Select2BillingInfoHandler , <EOL> ] <EOL> @ method_decorator ( require_POST ) <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( ConfirmSubscriptionRenewalView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> @ memoized <EOL> def next_plan_version ( self ) : <EOL> new_edition = self . request . POST . get ( '<STR_LIT>' ) . title ( ) <EOL> plan_version = DefaultProductPlan . get_default_plan_by_domain ( self . domain , new_edition ) <EOL> if plan_version is None : <EOL> log_accounting_error ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % { <EOL> '<STR_LIT>' : self . domain , <EOL> '<STR_LIT>' : self . subscription . pk <EOL> } <EOL> ) <EOL> raise Http404 <EOL> return plan_version <EOL> @ property <EOL> @ memoized <EOL> def confirm_form ( self ) : <EOL> if self . request . method == '<STR_LIT:POST>' and "<STR_LIT>" not in self . request . POST : <EOL> return ConfirmSubscriptionRenewalForm ( <EOL> self . account , self . domain , self . request . couch_user . username , <EOL> self . subscription , self . next_plan_version , <EOL> data = self . request . POST , <EOL> ) <EOL> return ConfirmSubscriptionRenewalForm ( <EOL> self . account , self . domain , self . request . couch_user . username , <EOL> self . subscription , self . next_plan_version , <EOL> ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . subscription , <EOL> '<STR_LIT>' : self . subscription . plan_version . user_facing_description , <EOL> '<STR_LIT>' : self . confirm_form , <EOL> '<STR_LIT>' : self . next_plan_version . user_facing_description , <EOL> } <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if self . async_response is not None : <EOL> return self . async_response <EOL> if self . confirm_form . is_valid ( ) : <EOL> is_saved = self . confirm_form . save ( ) <EOL> if not is_saved : <EOL> messages . error ( <EOL> request , _ ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> ) <EOL> else : <EOL> messages . success ( <EOL> request , _ ( "<STR_LIT>" ) <EOL> ) <EOL> return HttpResponseRedirect ( <EOL> reverse ( DomainSubscriptionView . urlname , args = [ self . domain ] ) <EOL> ) <EOL> return self . get ( request , * args , ** kwargs ) <EOL> class ExchangeSnapshotsView ( BaseAdminProjectSettingsView ) : <EOL> template_name = '<STR_LIT>' <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> @ method_decorator ( domain_admin_required ) <EOL> @ use_bootstrap3 <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( BaseProjectSettingsView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . domain_object , <EOL> '<STR_LIT>' : list ( self . domain_object . snapshots ( ) ) , <EOL> '<STR_LIT>' : self . domain_object . published_snapshot ( ) , <EOL> } <EOL> class CreateNewExchangeSnapshotView ( BaseAdminProjectSettingsView ) : <EOL> template_name = '<STR_LIT>' <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> strict_domain_fetching = True <EOL> @ method_decorator ( domain_admin_required ) <EOL> @ use_bootstrap3 <EOL> @ use_jquery_ui <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( BaseProjectSettingsView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> def parent_pages ( self ) : <EOL> return [ { <EOL> '<STR_LIT:title>' : ExchangeSnapshotsView . page_title , <EOL> '<STR_LIT:url>' : reverse ( ExchangeSnapshotsView . urlname , args = [ self . domain ] ) , <EOL> } ] <EOL> @ property <EOL> def page_context ( self ) : <EOL> context = { <EOL> '<STR_LIT>' : self . snapshot_settings_form , <EOL> '<STR_LIT>' : self . app_forms , <EOL> '<STR_LIT>' : self . fixture_forms , <EOL> '<STR_LIT>' : self . can_publish_as_org , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> } <EOL> if self . published_snapshot : <EOL> context . update ( { <EOL> '<STR_LIT>' : self . published_snapshot . publisher == '<STR_LIT>' , <EOL> '<STR_LIT>' : self . published_snapshot . author , <EOL> } ) <EOL> elif self . request . method == '<STR_LIT:POST>' : <EOL> context . update ( { <EOL> '<STR_LIT>' : self . request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT>' , <EOL> '<STR_LIT>' : self . request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> } ) <EOL> return context <EOL> @ property <EOL> def can_publish_as_org ( self ) : <EOL> return False <EOL> @ property <EOL> @ memoized <EOL> def snapshots ( self ) : <EOL> return list ( self . domain_object . snapshots ( ) ) <EOL> @ property <EOL> @ memoized <EOL> def published_snapshot ( self ) : <EOL> return self . snapshots [ <NUM_LIT:0> ] if self . snapshots else self . domain_object <EOL> @ property <EOL> @ memoized <EOL> def published_apps ( self ) : <EOL> published_apps = { } <EOL> if self . published_snapshot : <EOL> for app in self . published_snapshot . full_applications ( ) : <EOL> base_app_id = app . copy_of if self . domain_object == self . published_snapshot else app . copied_from . copy_of <EOL> if base_app_id : <EOL> published_apps [ base_app_id ] = app <EOL> return published_apps <EOL> @ property <EOL> def app_forms ( self ) : <EOL> app_forms = [ ] <EOL> for app in self . domain_object . applications ( ) : <EOL> if self . request . method == '<STR_LIT:POST>' : <EOL> app_forms . append ( ( app , SnapshotApplicationForm ( self . request . POST , prefix = app . id ) ) ) <EOL> elif self . published_snapshot and app . copy_of in self . published_apps : <EOL> original = self . published_apps [ app . copy_of ] <EOL> app_forms . append ( ( app , SnapshotApplicationForm ( initial = { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:name>' : original . name , <EOL> '<STR_LIT:description>' : original . description , <EOL> '<STR_LIT>' : original . deployment_date , <EOL> '<STR_LIT>' : original . user_type , <EOL> '<STR_LIT>' : original . attribution_notes , <EOL> '<STR_LIT>' : original . phone_model , <EOL> } , prefix = app . id ) ) ) <EOL> else : <EOL> app_forms . append ( ( app , <EOL> SnapshotApplicationForm ( <EOL> initial = { <EOL> '<STR_LIT>' : ( self . published_snapshot is None <EOL> or self . published_snapshot == self . domain_object ) <EOL> } , prefix = app . id ) ) ) <EOL> return app_forms <EOL> @ property <EOL> @ memoized <EOL> def published_fixtures ( self ) : <EOL> return [ f . copy_from for f in FixtureDataType . by_domain ( self . published_snapshot . _id ) ] <EOL> @ property <EOL> def fixture_forms ( self ) : <EOL> fixture_forms = [ ] <EOL> for fixture in FixtureDataType . by_domain ( self . domain_object . name ) : <EOL> fixture . id = fixture . _id <EOL> if self . request . method == '<STR_LIT:POST>' : <EOL> fixture_forms . append ( ( fixture , <EOL> SnapshotFixtureForm ( self . request . POST , prefix = fixture . _id ) ) ) <EOL> else : <EOL> fixture_forms . append ( ( fixture , <EOL> SnapshotFixtureForm ( <EOL> initial = { <EOL> '<STR_LIT>' : ( self . published_snapshot == self . domain_object <EOL> or fixture . _id in self . published_fixtures ) <EOL> } , prefix = fixture . _id ) ) ) <EOL> return fixture_forms <EOL> @ property <EOL> @ memoized <EOL> def snapshot_settings_form ( self ) : <EOL> if self . request . method == '<STR_LIT:POST>' : <EOL> form = SnapshotSettingsForm ( self . request . POST , <EOL> self . request . FILES , <EOL> domain = self . domain_object , <EOL> is_superuser = self . request . user . is_superuser ) <EOL> return form <EOL> proj = self . published_snapshot if self . published_snapshot else self . domain_object <EOL> initial = { <EOL> '<STR_LIT>' : json . dumps ( proj . case_sharing ) , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : self . published_snapshot . multimedia_included if self . published_snapshot else True , <EOL> } <EOL> init_attribs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if self . published_snapshot : <EOL> init_attribs . extend ( [ '<STR_LIT:title>' , '<STR_LIT:description>' , '<STR_LIT>' ] ) <EOL> if self . published_snapshot . yt_id : <EOL> initial [ '<STR_LIT>' ] = '<STR_LIT>' % self . published_snapshot . yt_id <EOL> for attr in init_attribs : <EOL> initial [ attr ] = getattr ( proj , attr ) <EOL> return SnapshotSettingsForm ( initial = initial , <EOL> domain = self . domain_object , <EOL> is_superuser = self . request . user . is_superuser ) <EOL> @ property <EOL> @ memoized <EOL> def has_published_apps ( self ) : <EOL> for app in self . domain_object . applications ( ) : <EOL> if self . request . POST . get ( "<STR_LIT>" % app . id , False ) : <EOL> return True <EOL> messages . error ( self . request , _ ( "<STR_LIT>" ) ) <EOL> return False <EOL> @ property <EOL> def has_signed_eula ( self ) : <EOL> eula_signed = self . request . couch_user . is_eula_signed ( ) <EOL> if not eula_signed : <EOL> messages . error ( self . request , _ ( "<STR_LIT>" ) ) <EOL> return eula_signed <EOL> @ property <EOL> def has_valid_form ( self ) : <EOL> is_valid = self . snapshot_settings_form . is_valid ( ) <EOL> if not is_valid : <EOL> messages . error ( self . request , _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> return is_valid <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if self . has_published_apps and self . has_signed_eula and self . has_valid_form : <EOL> new_license = request . POST [ '<STR_LIT>' ] <EOL> if request . POST . get ( '<STR_LIT>' , False ) : <EOL> app_ids = self . snapshot_settings_form . _get_apps_to_publish ( ) <EOL> media = self . domain_object . all_media ( from_apps = app_ids ) <EOL> for m_file in media : <EOL> if self . domain not in m_file . shared_by : <EOL> m_file . shared_by . append ( self . domain ) <EOL> if not m_file . license : <EOL> m_file . update_or_add_license ( self . domain , type = new_license , should_save = False ) <EOL> m_file . save ( ) <EOL> if not request . POST . get ( '<STR_LIT>' , False ) : <EOL> share_reminders = False <EOL> else : <EOL> share_reminders = True <EOL> copy_by_id = set ( ) <EOL> for k in request . POST . keys ( ) : <EOL> if k . endswith ( "<STR_LIT>" ) : <EOL> copy_by_id . add ( k [ : - len ( "<STR_LIT>" ) ] ) <EOL> old = self . domain_object . published_snapshot ( ) <EOL> new_domain = self . domain_object . save_snapshot ( <EOL> share_reminders = share_reminders , copy_by_id = copy_by_id ) <EOL> new_domain . license = new_license <EOL> new_domain . description = request . POST [ '<STR_LIT:description>' ] <EOL> new_domain . short_description = request . POST [ '<STR_LIT>' ] <EOL> new_domain . project_type = request . POST [ '<STR_LIT>' ] <EOL> new_domain . title = request . POST [ '<STR_LIT:title>' ] <EOL> new_domain . multimedia_included = request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT>' <EOL> new_domain . publisher = request . POST . get ( '<STR_LIT>' , None ) or '<STR_LIT:user>' <EOL> if request . POST . get ( '<STR_LIT>' ) : <EOL> new_domain . yt_id = self . snapshot_settings_form . cleaned_data [ '<STR_LIT>' ] <EOL> new_domain . author = request . POST . get ( '<STR_LIT>' , None ) <EOL> new_domain . is_approved = False <EOL> new_domain . is_starter_app = request . POST . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT>' <EOL> publish_on_submit = request . POST . get ( '<STR_LIT>' , "<STR_LIT>" ) == "<STR_LIT:yes>" <EOL> image = self . snapshot_settings_form . cleaned_data [ '<STR_LIT:image>' ] <EOL> if image : <EOL> new_domain . image_path = image . name <EOL> new_domain . image_type = image . content_type <EOL> elif request . POST . get ( '<STR_LIT>' , False ) : <EOL> new_domain . image_path = old . image_path <EOL> new_domain . image_type = old . image_type <EOL> documentation_file = self . snapshot_settings_form . cleaned_data [ '<STR_LIT>' ] <EOL> if documentation_file : <EOL> new_domain . documentation_file_path = documentation_file . name <EOL> new_domain . documentation_file_type = documentation_file . content_type <EOL> elif request . POST . get ( '<STR_LIT>' , False ) : <EOL> new_domain . documentation_file_path = old . documentation_file_path <EOL> new_domain . documentation_file_type = old . documentation_file_type <EOL> if publish_on_submit : <EOL> new_domain . save ( ) <EOL> _publish_snapshot ( request , self . domain_object , published_snapshot = new_domain ) <EOL> else : <EOL> new_domain . published = False <EOL> new_domain . save ( ) <EOL> if image : <EOL> im = Image . open ( image ) <EOL> out = cStringIO . StringIO ( ) <EOL> im . thumbnail ( ( <NUM_LIT:200> , <NUM_LIT:200> ) , Image . ANTIALIAS ) <EOL> im . save ( out , new_domain . image_type . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] ) <EOL> new_domain . put_attachment ( content = out . getvalue ( ) , name = image . name ) <EOL> elif request . POST . get ( '<STR_LIT>' , False ) : <EOL> new_domain . put_attachment ( content = old . fetch_attachment ( old . image_path ) , name = new_domain . image_path ) <EOL> if documentation_file : <EOL> new_domain . put_attachment ( content = documentation_file , name = documentation_file . name ) <EOL> elif request . POST . get ( '<STR_LIT>' , False ) : <EOL> new_domain . put_attachment ( content = old . fetch_attachment ( old . documentation_file_path ) , <EOL> name = new_domain . documentation_file_path ) <EOL> for application in new_domain . full_applications ( ) : <EOL> original_id = application . copied_from . copy_of if application . copied_from . copy_of else application . copied_from . _id <EOL> name_field = "<STR_LIT>" % original_id <EOL> if name_field not in request . POST : <EOL> continue <EOL> application . name = request . POST [ name_field ] <EOL> application . description = request . POST [ "<STR_LIT>" % original_id ] <EOL> date_picked = request . POST [ "<STR_LIT>" % original_id ] <EOL> try : <EOL> date_picked = dateutil . parser . parse ( date_picked ) <EOL> if date_picked . year > <NUM_LIT> : <EOL> application . deployment_date = date_picked <EOL> except Exception : <EOL> pass <EOL> application . phone_model = request . POST [ "<STR_LIT>" % original_id ] <EOL> application . attribution_notes = request . POST [ "<STR_LIT>" % original_id ] <EOL> application . user_type = request . POST [ "<STR_LIT>" % original_id ] <EOL> if not new_domain . multimedia_included : <EOL> application . multimedia_map = { } <EOL> application . save ( ) <EOL> for fixture in FixtureDataType . by_domain ( new_domain . name ) : <EOL> old_id = FixtureDataType . by_domain_tag ( self . domain_object . name , <EOL> fixture . tag ) . first ( ) . _id <EOL> fixture . description = request . POST [ "<STR_LIT>" % old_id ] <EOL> fixture . save ( ) <EOL> messages . success ( request , ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) if publish_on_submit <EOL> else _ ( "<STR_LIT>" ) ) ) <EOL> return redirect ( ExchangeSnapshotsView . urlname , self . domain ) <EOL> return self . get ( request , * args , ** kwargs ) <EOL> class ManageProjectMediaView ( BaseAdminProjectSettingsView ) : <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> template_name = '<STR_LIT>' <EOL> @ method_decorator ( domain_admin_required ) <EOL> @ use_bootstrap3 <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( BaseProjectSettingsView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> def project_media_data ( self ) : <EOL> return [ { <EOL> '<STR_LIT>' : m . license . type if m . license else '<STR_LIT>' , <EOL> '<STR_LIT>' : self . domain in m . shared_by , <EOL> '<STR_LIT:url>' : m . url ( ) , <EOL> '<STR_LIT>' : m . _id , <EOL> '<STR_LIT>' : m . tags . get ( self . domain , [ ] ) , <EOL> '<STR_LIT:type>' : m . doc_type , <EOL> } for m in self . request . project . all_media ( ) ] <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . project_media_data , <EOL> '<STR_LIT>' : LICENSES . items ( ) , <EOL> } <EOL> @ retry_resource ( <NUM_LIT:3> ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> for m_file in request . project . all_media ( ) : <EOL> if '<STR_LIT>' % m_file . _id in request . POST : <EOL> m_file . tags [ self . domain ] = request . POST . get ( '<STR_LIT>' % m_file . _id , '<STR_LIT>' ) . split ( '<STR_LIT:U+0020>' ) <EOL> if self . domain not in m_file . shared_by and request . POST . get ( '<STR_LIT>' % m_file . _id , False ) : <EOL> m_file . shared_by . append ( self . domain ) <EOL> elif self . domain in m_file . shared_by and not request . POST . get ( '<STR_LIT>' % m_file . _id , False ) : <EOL> m_file . shared_by . remove ( self . domain ) <EOL> if '<STR_LIT>' % m_file . _id in request . POST : <EOL> m_file . update_or_add_license ( self . domain , <EOL> type = request . POST . get ( '<STR_LIT>' % m_file . _id , '<STR_LIT>' ) , <EOL> should_save = True ) <EOL> m_file . save ( ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return self . get ( request , * args , ** kwargs ) <EOL> class CaseSearchConfigView ( BaseAdminProjectSettingsView ) : <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( '<STR_LIT>' ) <EOL> template_name = '<STR_LIT>' <EOL> @ method_decorator ( domain_admin_required ) <EOL> @ use_bootstrap3 <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( CaseSearchConfigView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> def unpack_fuzzies ( query_dict ) : <EOL> """<STR_LIT>""" <EOL> pattern = re . compile ( r'<STR_LIT>' ) <EOL> fuzzy_dict = defaultdict ( dict ) <EOL> for key in query_dict : <EOL> match = pattern . match ( key ) <EOL> if match : <EOL> i = int ( match . group ( '<STR_LIT:index>' ) ) <EOL> attr = match . group ( '<STR_LIT>' ) <EOL> is_list = key . endswith ( '<STR_LIT>' ) <EOL> fuzzy_dict [ i ] [ attr ] = query_dict . getlist ( key ) if is_list else query_dict [ key ] <EOL> if not fuzzy_dict : <EOL> return [ ] <EOL> return [ fuzzy_dict [ i ] for i in range ( max ( fuzzy_dict . keys ( ) ) + <NUM_LIT:1> ) if fuzzy_dict [ i ] ] <EOL> CaseSearchConfig . objects . update_or_create ( domain = self . domain , defaults = { <EOL> '<STR_LIT>' : request . POST [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : CaseSearchConfigJSON ( { '<STR_LIT>' : unpack_fuzzies ( request . POST ) } ) <EOL> } ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return self . get ( request , * args , ** kwargs ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> apps = get_apps_in_domain ( self . domain , include_remote = False ) <EOL> case_types = { t for app in apps for t in app . get_case_types ( ) if t } <EOL> current_values = CaseSearchConfig . objects . get_or_none ( pk = self . domain ) <EOL> return { <EOL> '<STR_LIT>' : sorted ( list ( case_types ) ) , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : current_values . enabled if current_values else False , <EOL> '<STR_LIT>' : current_values . config if current_values else { } <EOL> } <EOL> } <EOL> class RepeaterMixin ( object ) : <EOL> @ property <EOL> def friendly_repeater_names ( self ) : <EOL> return { <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> } <EOL> class DomainForwardingRepeatRecords ( GenericTabularReport ) : <EOL> name = '<STR_LIT>' <EOL> base_template = '<STR_LIT>' <EOL> section_name = '<STR_LIT>' <EOL> slug = '<STR_LIT>' <EOL> dispatcher = DomainReportDispatcher <EOL> ajax_pagination = True <EOL> asynchronous = False <EOL> is_bootstrap3 = True <EOL> sortable = False <EOL> fields = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def _make_view_payload_button ( self , record_id ) : <EOL> return '''<STR_LIT>''' . format ( record_id ) <EOL> def _make_resend_payload_button ( self , record_id ) : <EOL> return '''<STR_LIT>''' . format ( record_id ) <EOL> def _make_state_label ( self , record ) : <EOL> label_cls = '<STR_LIT>' <EOL> label_text = '<STR_LIT>' <EOL> if record . state == RECORD_SUCCESS_STATE : <EOL> label_cls = '<STR_LIT:success>' <EOL> label_text = _ ( '<STR_LIT>' ) <EOL> elif record . state == RECORD_PENDING_STATE : <EOL> label_cls = '<STR_LIT>' <EOL> label_text = _ ( '<STR_LIT>' ) <EOL> elif record . state == RECORD_FAILURE_STATE : <EOL> label_cls = '<STR_LIT>' <EOL> label_text = _ ( '<STR_LIT>' ) <EOL> return '''<STR_LIT>''' . format ( label_cls , label_text ) <EOL> @ property <EOL> def report_context ( self ) : <EOL> context = super ( DomainForwardingRepeatRecords , self ) . report_context <EOL> context . update ( { <EOL> '<STR_LIT>' : ProjectSettingsTab ( <EOL> self . request , <EOL> domain = self . domain , <EOL> couch_user = self . request . couch_user , <EOL> ) <EOL> } ) <EOL> return context <EOL> @ property <EOL> def total_records ( self ) : <EOL> return get_repeat_record_count ( self . domain , self . repeater_id , self . state ) <EOL> @ property <EOL> def shared_pagination_GET_params ( self ) : <EOL> return [ <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:value>' : self . request . GET . get ( '<STR_LIT>' ) } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:value>' : self . request . GET . get ( '<STR_LIT>' ) } , <EOL> ] <EOL> def _format_date ( self , date ) : <EOL> tz_utc_aware_date = pytz . utc . localize ( date ) <EOL> return tz_utc_aware_date . astimezone ( self . timezone ) . strftime ( '<STR_LIT>' ) <EOL> @ property <EOL> def rows ( self ) : <EOL> self . repeater_id = self . request . GET . get ( '<STR_LIT>' , None ) <EOL> self . state = self . request . GET . get ( '<STR_LIT>' , None ) <EOL> records = get_paged_repeat_records ( <EOL> self . domain , <EOL> self . pagination . start , <EOL> self . pagination . count , <EOL> repeater_id = self . repeater_id , <EOL> state = self . state <EOL> ) <EOL> return map ( <EOL> lambda record : [ <EOL> self . _make_state_label ( record ) , <EOL> record . url if record . url else _ ( u'<STR_LIT>' ) , <EOL> self . _format_date ( record . last_checked ) if record . last_checked else None , <EOL> self . _format_date ( record . next_check ) if record . next_check else None , <EOL> record . failure_reason if not record . succeeded else None , <EOL> self . _make_view_payload_button ( record . get_id ) , <EOL> self . _make_resend_payload_button ( record . get_id ) , <EOL> ] , <EOL> records <EOL> ) <EOL> @ property <EOL> def headers ( self ) : <EOL> return DataTablesHeader ( <EOL> DataTablesColumn ( _ ( '<STR_LIT>' ) ) , <EOL> DataTablesColumn ( _ ( '<STR_LIT>' ) ) , <EOL> DataTablesColumn ( _ ( '<STR_LIT>' ) ) , <EOL> DataTablesColumn ( _ ( '<STR_LIT>' ) ) , <EOL> DataTablesColumn ( _ ( '<STR_LIT>' ) ) , <EOL> DataTablesColumn ( _ ( '<STR_LIT>' ) ) , <EOL> DataTablesColumn ( _ ( '<STR_LIT>' ) ) , <EOL> ) <EOL> class DomainForwardingOptionsView ( BaseAdminProjectSettingsView , RepeaterMixin ) : <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> template_name = '<STR_LIT>' <EOL> @ method_decorator ( domain_admin_required ) <EOL> @ use_bootstrap3 <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( BaseProjectSettingsView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> def repeaters ( self ) : <EOL> available_repeaters = [ <EOL> FormRepeater , CaseRepeater , ShortFormRepeater , AppStructureRepeater , <EOL> ] <EOL> return [ ( r . __name__ , r . by_domain ( self . domain ) , self . friendly_repeater_names [ r . __name__ ] ) <EOL> for r in available_repeaters ] <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . repeaters , <EOL> '<STR_LIT>' : RepeatRecord . count ( self . domain ) , <EOL> } <EOL> class AddRepeaterView ( BaseAdminProjectSettingsView , RepeaterMixin ) : <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> template_name = '<STR_LIT>' <EOL> repeater_form_class = GenericRepeaterForm <EOL> @ method_decorator ( domain_admin_required ) <EOL> @ use_bootstrap3 <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( BaseProjectSettingsView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> def page_url ( self ) : <EOL> return reverse ( self . urlname , args = [ self . domain , self . repeater_type ] ) <EOL> @ property <EOL> def parent_pages ( self ) : <EOL> return [ { <EOL> '<STR_LIT:title>' : DomainForwardingOptionsView . page_title , <EOL> '<STR_LIT:url>' : reverse ( DomainForwardingOptionsView . urlname , args = [ self . domain ] ) , <EOL> } ] <EOL> @ property <EOL> def repeater_type ( self ) : <EOL> return self . kwargs [ '<STR_LIT>' ] <EOL> @ property <EOL> def page_name ( self ) : <EOL> return "<STR_LIT>" % self . friendly_repeater_names . get ( self . repeater_type , "<STR_LIT>" ) <EOL> @ property <EOL> @ memoized <EOL> def repeater_class ( self ) : <EOL> try : <EOL> return repeater_types [ self . repeater_type ] <EOL> except KeyError : <EOL> raise Http404 ( ) <EOL> @ property <EOL> @ memoized <EOL> def add_repeater_form ( self ) : <EOL> if self . request . method == '<STR_LIT:POST>' : <EOL> return self . repeater_form_class ( <EOL> self . request . POST , <EOL> domain = self . domain , <EOL> repeater_class = self . repeater_class <EOL> ) <EOL> return self . repeater_form_class ( <EOL> domain = self . domain , <EOL> repeater_class = self . repeater_class <EOL> ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . add_repeater_form , <EOL> '<STR_LIT>' : self . repeater_type , <EOL> } <EOL> def make_repeater ( self ) : <EOL> repeater = self . repeater_class ( <EOL> domain = self . domain , <EOL> url = self . add_repeater_form . cleaned_data [ '<STR_LIT:url>' ] , <EOL> use_basic_auth = self . add_repeater_form . cleaned_data [ '<STR_LIT>' ] , <EOL> username = self . add_repeater_form . cleaned_data [ '<STR_LIT:username>' ] , <EOL> password = self . add_repeater_form . cleaned_data [ '<STR_LIT:password>' ] , <EOL> format = self . add_repeater_form . cleaned_data [ '<STR_LIT>' ] <EOL> ) <EOL> return repeater <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if self . add_repeater_form . is_valid ( ) : <EOL> repeater = self . make_repeater ( ) <EOL> repeater . save ( ) <EOL> messages . success ( request , _ ( "<STR_LIT>" % repeater . url ) ) <EOL> return HttpResponseRedirect ( reverse ( DomainForwardingOptionsView . urlname , args = [ self . domain ] ) ) <EOL> return self . get ( request , * args , ** kwargs ) <EOL> class AddFormRepeaterView ( AddRepeaterView ) : <EOL> urlname = '<STR_LIT>' <EOL> repeater_form_class = FormRepeaterForm <EOL> @ property <EOL> def page_url ( self ) : <EOL> return reverse ( self . urlname , args = [ self . domain ] ) <EOL> def make_repeater ( self ) : <EOL> repeater = super ( AddFormRepeaterView , self ) . make_repeater ( ) <EOL> repeater . include_app_id_param = self . add_repeater_form . cleaned_data [ '<STR_LIT>' ] <EOL> return repeater <EOL> class BaseInternalDomainSettingsView ( BaseProjectSettingsView ) : <EOL> strict_domain_fetching = True <EOL> @ method_decorator ( login_and_domain_required ) <EOL> @ method_decorator ( require_superuser ) <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( BaseInternalDomainSettingsView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> def main_context ( self ) : <EOL> context = super ( BaseInternalDomainSettingsView , self ) . main_context <EOL> context . update ( { <EOL> '<STR_LIT>' : self . domain_object , <EOL> } ) <EOL> return context <EOL> @ property <EOL> def page_name ( self ) : <EOL> return mark_safe ( "<STR_LIT>" % self . page_title ) <EOL> class EditInternalDomainInfoView ( BaseInternalDomainSettingsView ) : <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> template_name = '<STR_LIT>' <EOL> strict_domain_fetching = True <EOL> @ method_decorator ( login_and_domain_required ) <EOL> @ method_decorator ( require_superuser ) <EOL> @ use_bootstrap3 <EOL> @ use_jquery_ui <EOL> @ use_jquery_ui_multiselect <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( BaseInternalDomainSettingsView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> def autocomplete_fields ( self ) : <EOL> return [ '<STR_LIT>' ] <EOL> @ property <EOL> @ memoized <EOL> def internal_settings_form ( self ) : <EOL> can_edit_eula = CAN_EDIT_EULA . enabled ( self . request . couch_user . username ) <EOL> if self . request . method == '<STR_LIT:POST>' : <EOL> return DomainInternalForm ( can_edit_eula , self . request . POST ) <EOL> initial = { <EOL> '<STR_LIT>' : self . domain_object . deployment . date . date <EOL> if self . domain_object . deployment . date else '<STR_LIT>' , <EOL> '<STR_LIT>' : self . domain_object . deployment . countries , <EOL> '<STR_LIT>' : self . domain_object . is_test , <EOL> } <EOL> internal_attrs = [ <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> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> if can_edit_eula : <EOL> internal_attrs += [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> for attr in internal_attrs : <EOL> val = getattr ( self . domain_object . internal , attr ) <EOL> if isinstance ( val , bool ) : <EOL> val = '<STR_LIT:true>' if val else '<STR_LIT:false>' <EOL> initial [ attr ] = val <EOL> return DomainInternalForm ( can_edit_eula , initial = initial ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . domain_object , <EOL> '<STR_LIT>' : self . internal_settings_form , <EOL> '<STR_LIT>' : dict ( [ ( a [ "<STR_LIT:name>" ] , a [ "<STR_LIT>" ] ) for a in settings . INTERNAL_DATA [ "<STR_LIT>" ] ] ) , <EOL> } <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if self . internal_settings_form . is_valid ( ) : <EOL> old_attrs = copy . copy ( self . domain_object . internal ) <EOL> self . internal_settings_form . save ( self . domain_object ) <EOL> eula_props_changed = ( bool ( old_attrs . custom_eula ) != bool ( self . domain_object . internal . custom_eula ) or <EOL> bool ( old_attrs . can_use_data ) != bool ( self . domain_object . internal . can_use_data ) ) <EOL> if eula_props_changed and settings . EULA_CHANGE_EMAIL : <EOL> message = '<STR_LIT:\n>' . join ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] ) . format ( <EOL> user = self . request . couch_user . username , <EOL> domain = self . domain , <EOL> eula_old = old_attrs . custom_eula , <EOL> eula_new = self . domain_object . internal . custom_eula , <EOL> can_use_data_old = old_attrs . can_use_data , <EOL> can_use_data_new = self . domain_object . internal . can_use_data , <EOL> ) <EOL> send_mail_async . delay ( <EOL> '<STR_LIT>' . format ( self . domain ) , <EOL> message , settings . DEFAULT_FROM_EMAIL , [ settings . EULA_CHANGE_EMAIL ] <EOL> ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) <EOL> % self . domain ) <EOL> else : <EOL> messages . error ( request , _ ( <EOL> "<STR_LIT>" ) ) <EOL> return self . get ( request , * args , ** kwargs ) <EOL> class EditInternalCalculationsView ( BaseInternalDomainSettingsView ) : <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> template_name = '<STR_LIT>' <EOL> @ method_decorator ( login_and_domain_required ) <EOL> @ method_decorator ( require_superuser ) <EOL> @ use_bootstrap3 <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( BaseInternalDomainSettingsView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : CALCS , <EOL> '<STR_LIT>' : CALC_ORDER , <EOL> } <EOL> @ login_and_domain_required <EOL> @ require_superuser <EOL> def calculated_properties ( request , domain ) : <EOL> calc_tag = request . GET . get ( "<STR_LIT>" , '<STR_LIT>' ) . split ( '<STR_LIT>' ) <EOL> extra_arg = calc_tag [ <NUM_LIT:1> ] if len ( calc_tag ) > <NUM_LIT:1> else '<STR_LIT>' <EOL> calc_tag = calc_tag [ <NUM_LIT:0> ] <EOL> if not calc_tag or calc_tag not in CALC_FNS . keys ( ) : <EOL> data = { "<STR_LIT:error>" : '<STR_LIT>' } <EOL> else : <EOL> data = { "<STR_LIT:value>" : dom_calc ( calc_tag , domain , extra_arg ) } <EOL> return json_response ( data ) <EOL> def _publish_snapshot ( request , domain , published_snapshot = None ) : <EOL> snapshots = domain . snapshots ( ) <EOL> for snapshot in snapshots : <EOL> if snapshot . published : <EOL> snapshot . published = False <EOL> if not published_snapshot or snapshot . name != published_snapshot . name : <EOL> snapshot . save ( ) <EOL> if published_snapshot : <EOL> if published_snapshot . copied_from . name != domain . name : <EOL> messages . error ( request , "<STR_LIT>" ) <EOL> return False <EOL> published_snapshot . cda . signed = True <EOL> published_snapshot . cda . date = datetime . datetime . utcnow ( ) <EOL> published_snapshot . cda . type = '<STR_LIT>' <EOL> if request . couch_user : <EOL> published_snapshot . cda . user_id = request . couch_user . get_id <EOL> published_snapshot . cda . user_ip = get_ip ( request ) <EOL> published_snapshot . published = True <EOL> published_snapshot . save ( ) <EOL> _notification_email_on_publish ( domain , published_snapshot , request . couch_user ) <EOL> return True <EOL> def _notification_email_on_publish ( domain , snapshot , published_by ) : <EOL> params = { "<STR_LIT>" : domain , "<STR_LIT>" : snapshot , <EOL> "<STR_LIT>" : published_by , "<STR_LIT>" : get_site_domain ( ) } <EOL> text_content = render_to_string ( <EOL> "<STR_LIT>" , params ) <EOL> html_content = render_to_string ( <EOL> "<STR_LIT>" , params ) <EOL> recipients = settings . EXCHANGE_NOTIFICATION_RECIPIENTS <EOL> subject = "<STR_LIT>" % snapshot . title <EOL> try : <EOL> for recipient in recipients : <EOL> send_html_email_async . delay ( subject , recipient , html_content , <EOL> text_content = text_content , <EOL> email_from = settings . DEFAULT_FROM_EMAIL ) <EOL> except Exception : <EOL> logging . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" % text_content ) <EOL> @ domain_admin_required <EOL> def set_published_snapshot ( request , domain , snapshot_name = '<STR_LIT>' ) : <EOL> domain = request . project <EOL> snapshots = domain . snapshots ( ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> if snapshot_name != '<STR_LIT>' : <EOL> published_snapshot = Domain . get_by_name ( snapshot_name ) <EOL> _publish_snapshot ( request , domain , published_snapshot = published_snapshot ) <EOL> else : <EOL> _publish_snapshot ( request , domain ) <EOL> return redirect ( '<STR_LIT>' , domain . name ) <EOL> class ProBonoMixin ( ) : <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> is_submitted = False <EOL> url_name = None <EOL> @ property <EOL> def requesting_domain ( self ) : <EOL> raise NotImplementedError <EOL> @ property <EOL> @ memoized <EOL> def pro_bono_form ( self ) : <EOL> if self . request . method == '<STR_LIT:POST>' : <EOL> return ProBonoForm ( self . use_domain_field , self . request . POST ) <EOL> return ProBonoForm ( self . use_domain_field ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . pro_bono_form , <EOL> '<STR_LIT>' : self . is_submitted , <EOL> } <EOL> @ property <EOL> def page_url ( self ) : <EOL> return self . url_name <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if self . pro_bono_form . is_valid ( ) : <EOL> self . pro_bono_form . process_submission ( domain = self . requesting_domain ) <EOL> self . is_submitted = True <EOL> return self . get ( request , * args , ** kwargs ) <EOL> class ProBonoStaticView ( ProBonoMixin , BasePageView ) : <EOL> template_name = '<STR_LIT>' <EOL> urlname = '<STR_LIT>' <EOL> use_domain_field = True <EOL> @ property <EOL> def requesting_domain ( self ) : <EOL> return self . pro_bono_form . cleaned_data [ '<STR_LIT>' ] <EOL> class ProBonoView ( ProBonoMixin , DomainAccountingSettings ) : <EOL> template_name = '<STR_LIT>' <EOL> urlname = '<STR_LIT>' <EOL> use_domain_field = False <EOL> @ property <EOL> def requesting_domain ( self ) : <EOL> return self . domain <EOL> @ property <EOL> def parent_pages ( self ) : <EOL> return [ <EOL> { <EOL> '<STR_LIT:title>' : DomainSubscriptionView . page_title , <EOL> '<STR_LIT:url>' : reverse ( DomainSubscriptionView . urlname , args = [ self . domain ] ) , <EOL> } <EOL> ] <EOL> @ property <EOL> def section_url ( self ) : <EOL> return self . page_url <EOL> class FeaturePreviewsView ( BaseAdminProjectSettingsView ) : <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> template_name = '<STR_LIT>' <EOL> @ method_decorator ( domain_admin_required ) <EOL> @ use_bootstrap3 <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( BaseProjectSettingsView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ memoized <EOL> def features ( self ) : <EOL> features = [ ] <EOL> for preview_name in dir ( feature_previews ) : <EOL> if not preview_name . startswith ( '<STR_LIT>' ) : <EOL> preview = getattr ( feature_previews , preview_name ) <EOL> if isinstance ( preview , feature_previews . FeaturePreview ) and preview . has_privilege ( self . request ) : <EOL> features . append ( ( preview , preview . enabled ( self . domain ) ) ) <EOL> return sorted ( features , key = lambda feature : feature [ <NUM_LIT:0> ] . label ) <EOL> def get_toggle ( self , slug ) : <EOL> if not slug in [ f . slug for f , _ in self . features ( ) ] : <EOL> raise Http404 ( ) <EOL> try : <EOL> return Toggle . get ( slug ) <EOL> except ResourceNotFound : <EOL> return Toggle ( slug = slug ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . features ( ) , <EOL> } <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> for feature , enabled in self . features ( ) : <EOL> self . update_feature ( feature , enabled , feature . slug in request . POST ) <EOL> return redirect ( '<STR_LIT>' , domain = self . domain ) <EOL> def update_feature ( self , feature , current_state , new_state ) : <EOL> if current_state != new_state : <EOL> toggle_js_domain_cachebuster . clear ( self . domain ) <EOL> feature . set ( self . domain , new_state , NAMESPACE_DOMAIN ) <EOL> if feature . save_fn is not None : <EOL> feature . save_fn ( self . domain , new_state ) <EOL> class FeatureFlagsView ( BaseAdminProjectSettingsView ) : <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> template_name = '<STR_LIT>' <EOL> @ use_bootstrap3 <EOL> @ method_decorator ( require_superuser ) <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( FeatureFlagsView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ memoized <EOL> def enabled_flags ( self ) : <EOL> def _sort_key ( toggle_enabled_tuple ) : <EOL> return ( not toggle_enabled_tuple [ <NUM_LIT:1> ] , not toggle_enabled_tuple [ <NUM_LIT:2> ] , toggle_enabled_tuple [ <NUM_LIT:0> ] . label ) <EOL> return sorted ( <EOL> [ ( toggle , toggle . enabled ( self . domain ) , toggle . enabled ( self . request . couch_user . username ) ) <EOL> for toggle in all_toggles ( ) ] , <EOL> key = _sort_key , <EOL> ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . enabled_flags ( ) , <EOL> } <EOL> class TransferDomainView ( BaseAdminProjectSettingsView ) : <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> template_name = '<STR_LIT>' <EOL> @ property <EOL> @ memoized <EOL> def active_transfer ( self ) : <EOL> return TransferDomainRequest . get_active_transfer ( self . domain , <EOL> self . request . user . username ) <EOL> @ property <EOL> @ memoized <EOL> def transfer_domain_form ( self ) : <EOL> return TransferDomainForm ( self . domain , <EOL> self . request . user . username , <EOL> self . request . POST or None ) <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> if self . active_transfer : <EOL> self . template_name = '<STR_LIT>' <EOL> if request . GET . get ( '<STR_LIT>' , None ) : <EOL> self . active_transfer . send_transfer_request ( ) <EOL> messages . info ( request , <EOL> _ ( u"<STR_LIT>" ) . format ( domain = self . domain ) ) <EOL> return super ( TransferDomainView , self ) . get ( request , * args , ** kwargs ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> form = self . transfer_domain_form <EOL> if form . is_valid ( ) : <EOL> transfer = form . save ( ) <EOL> transfer . send_transfer_request ( ) <EOL> return HttpResponseRedirect ( self . page_url ) <EOL> context = self . get_context_data ( ** kwargs ) <EOL> return self . render_to_response ( context ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> if self . active_transfer : <EOL> return { '<STR_LIT>' : self . active_transfer . as_dict ( ) } <EOL> else : <EOL> return { '<STR_LIT>' : self . transfer_domain_form } <EOL> @ method_decorator ( domain_admin_required ) <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> if not TRANSFER_DOMAIN . enabled ( request . domain ) : <EOL> raise Http404 ( ) <EOL> return super ( TransferDomainView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> class ActivateTransferDomainView ( BasePageView ) : <EOL> urlname = '<STR_LIT>' <EOL> page_title = '<STR_LIT>' <EOL> template_name = '<STR_LIT>' <EOL> @ property <EOL> @ memoized <EOL> def active_transfer ( self ) : <EOL> return TransferDomainRequest . get_by_guid ( self . guid ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> if self . active_transfer : <EOL> return { '<STR_LIT>' : self . active_transfer . as_dict ( ) } <EOL> else : <EOL> return { } <EOL> @ property <EOL> def page_url ( self ) : <EOL> return self . request . get_full_path ( ) <EOL> def get ( self , request , guid , * args , ** kwargs ) : <EOL> self . guid = guid <EOL> if ( self . active_transfer and <EOL> self . active_transfer . to_username != request . user . username and <EOL> not request . user . is_superuser ) : <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" ) ) <EOL> return super ( ActivateTransferDomainView , self ) . get ( request , * args , ** kwargs ) <EOL> def post ( self , request , guid , * args , ** kwargs ) : <EOL> self . guid = guid <EOL> if not self . active_transfer : <EOL> raise Http404 ( ) <EOL> if self . active_transfer . to_username != request . user . username and not request . user . is_superuser : <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" ) ) <EOL> self . active_transfer . transfer_domain ( ip = get_ip ( request ) ) <EOL> messages . success ( request , _ ( u"<STR_LIT>" ) <EOL> . format ( domain = self . active_transfer . domain ) ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = [ self . active_transfer . domain ] ) ) <EOL> @ method_decorator ( login_required ) <EOL> def dispatch ( self , * args , ** kwargs ) : <EOL> return super ( ActivateTransferDomainView , self ) . dispatch ( * args , ** kwargs ) <EOL> class DeactivateTransferDomainView ( View ) : <EOL> def post ( self , request , guid , * args , ** kwargs ) : <EOL> transfer = TransferDomainRequest . get_by_guid ( guid ) <EOL> if not transfer : <EOL> return HttpResponseRedirect ( request . META . get ( '<STR_LIT>' , '<STR_LIT:/>' ) ) <EOL> if ( transfer . to_username != request . user . username and <EOL> transfer . from_username != request . user . username and <EOL> not request . user . is_superuser ) : <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" ) ) <EOL> transfer . active = False <EOL> transfer . save ( ) <EOL> referer = request . META . get ( '<STR_LIT>' , '<STR_LIT:/>' ) <EOL> if referer . endswith ( reverse ( '<STR_LIT>' , args = [ guid ] ) ) : <EOL> messages . info ( request , <EOL> _ ( u"<STR_LIT>" ) . format ( domain = transfer . domain ) ) <EOL> return HttpResponseRedirect ( '<STR_LIT:/>' ) <EOL> else : <EOL> return HttpResponseRedirect ( referer ) <EOL> @ method_decorator ( login_required ) <EOL> def dispatch ( self , * args , ** kwargs ) : <EOL> return super ( DeactivateTransferDomainView , self ) . dispatch ( * args , ** kwargs ) <EOL> from corehq . apps . smsbillables . forms import PublicSMSRateCalculatorForm <EOL> from corehq . apps . smsbillables . async_handlers import PublicSMSRatesAsyncHandler <EOL> class PublicSMSRatesView ( BasePageView , AsyncHandlerMixin ) : <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> template_name = '<STR_LIT>' <EOL> async_handlers = [ PublicSMSRatesAsyncHandler ] <EOL> @ use_bootstrap3 <EOL> @ use_select2 <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( PublicSMSRatesView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> @ property <EOL> def page_url ( self ) : <EOL> return reverse ( self . urlname ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : PublicSMSRateCalculatorForm ( ) <EOL> } <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> return self . async_response or self . get ( request , * args , ** kwargs ) <EOL> class SMSRatesView ( BaseAdminProjectSettingsView , AsyncHandlerMixin ) : <EOL> urlname = '<STR_LIT>' <EOL> page_title = ugettext_lazy ( "<STR_LIT>" ) <EOL> template_name = '<STR_LIT>' <EOL> async_handlers = [ <EOL> SMSRatesAsyncHandler , <EOL> SMSRatesSelect2AsyncHandler , <EOL> ] <EOL> @ property <EOL> @ memoized <EOL> def rate_calc_form ( self ) : <EOL> if self . request . method == '<STR_LIT:POST>' : <EOL> return SMSRateCalculatorForm ( self . domain , self . request . POST ) <EOL> return SMSRateCalculatorForm ( self . domain ) <EOL> @ property <EOL> def page_context ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . rate_calc_form , <EOL> } <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if self . async_response is not None : <EOL> return self . async_response <EOL> return self . get ( request , * args , ** kwargs ) <EOL> class BaseCardView ( DomainAccountingSettings ) : <EOL> @ property <EOL> def payment_method ( self ) : <EOL> payment_method , __ = StripePaymentMethod . objects . get_or_create ( <EOL> web_user = self . request . user . username , <EOL> method_type = PaymentMethodType . STRIPE , <EOL> ) <EOL> return payment_method <EOL> def _generic_error ( self ) : <EOL> error = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return json_response ( { '<STR_LIT:error>' : error } , status_code = <NUM_LIT> ) <EOL> def _stripe_error ( self , e ) : <EOL> body = e . json_body <EOL> err = body [ '<STR_LIT:error>' ] <EOL> return json_response ( { '<STR_LIT:error>' : err [ '<STR_LIT:message>' ] , <EOL> '<STR_LIT>' : self . payment_method . all_cards_serialized ( self . account ) } , <EOL> status_code = <NUM_LIT> ) <EOL> class CardView ( BaseCardView ) : <EOL> """<STR_LIT>""" <EOL> url_name = "<STR_LIT>" <EOL> def post ( self , request , domain , card_token ) : <EOL> try : <EOL> card = self . payment_method . get_card ( card_token ) <EOL> if request . POST . get ( "<STR_LIT>" ) == '<STR_LIT:true>' : <EOL> self . payment_method . set_autopay ( card , self . account , domain ) <EOL> elif request . POST . get ( "<STR_LIT>" ) == '<STR_LIT:false>' : <EOL> self . payment_method . unset_autopay ( card , self . account ) <EOL> except self . payment_method . STRIPE_GENERIC_ERROR as e : <EOL> return self . _stripe_error ( e ) <EOL> except Exception as e : <EOL> return self . _generic_error ( ) <EOL> return json_response ( { '<STR_LIT>' : self . payment_method . all_cards_serialized ( self . account ) } ) <EOL> def delete ( self , request , domain , card_token ) : <EOL> try : <EOL> self . payment_method . remove_card ( card_token ) <EOL> except self . payment_method . STRIPE_GENERIC_ERROR as e : <EOL> return self . _stripe_error ( e ) <EOL> return json_response ( { '<STR_LIT>' : self . payment_method . all_cards_serialized ( self . account ) } ) <EOL> class CardsView ( BaseCardView ) : <EOL> """<STR_LIT>""" <EOL> url_name = "<STR_LIT>" <EOL> def get ( self , request , domain ) : <EOL> return json_response ( { '<STR_LIT>' : self . payment_method . all_cards_serialized ( self . account ) } ) <EOL> def post ( self , request , domain ) : <EOL> stripe_token = request . POST . get ( '<STR_LIT>' ) <EOL> autopay = request . POST . get ( '<STR_LIT>' ) == '<STR_LIT:true>' <EOL> try : <EOL> self . payment_method . create_card ( stripe_token , self . account , domain , autopay ) <EOL> except self . payment_method . STRIPE_GENERIC_ERROR as e : <EOL> return self . _stripe_error ( e ) <EOL> except Exception as e : <EOL> return self . _generic_error ( ) <EOL> return json_response ( { '<STR_LIT>' : self . payment_method . all_cards_serialized ( self . account ) } ) <EOL> class PasswordResetView ( View ) : <EOL> urlname = "<STR_LIT>" <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> extra_context = kwargs . setdefault ( '<STR_LIT>' , { } ) <EOL> extra_context [ '<STR_LIT>' ] = settings . ENABLE_DRACONIAN_SECURITY_FEATURES <EOL> return password_reset_confirm ( request , * args , ** kwargs ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> extra_context = kwargs . setdefault ( '<STR_LIT>' , { } ) <EOL> extra_context [ '<STR_LIT>' ] = settings . ENABLE_DRACONIAN_SECURITY_FEATURES <EOL> response = password_reset_confirm ( request , * args , ** kwargs ) <EOL> uidb64 = kwargs . get ( '<STR_LIT>' ) <EOL> uid = urlsafe_base64_decode ( uidb64 ) <EOL> user = User . objects . get ( pk = uid ) <EOL> couch_user = CouchUser . from_django_user ( user ) <EOL> clear_login_attempts ( couch_user ) <EOL> return response </s>
