def parse_line(line, account_first=False, create_organizations=False): """ Parse an (organization, account, amount) triplet. """ if account_first: look = re.match(r'\s+(?P<account>(\w+:)+)(?P<organization>\w+)'\ r'(\s+(?P<amount>.+))?', line) else: look = re.match(r'\s+(?P<organization>\w+)(?P<account>(:(\w+))+)'\ r'(\s+(?P<amount>.+))?', line) if look: organization_slug = look.group('organization') account = look.group('account') if account.startswith(':'): account = account[1:] if account.endswith(':'): account = account[:-1] amount = look.group('amount') if amount and amount.startswith('$'): locale.setlocale(locale.LC_ALL, 'en_US') amount = long(locale.atof(amount[1:]) * 100) try: if create_organizations: organization, _ = Organization.objects.get_or_create( slug=organization_slug) else: organization = Organization.objects.get(slug=organization_slug) if account_first: organization = get_current_provider() return (organization, account, amount) except Organization.DoesNotExist: print "Cannot find Organization '%s'" % organization_slug return (None, None, None)
def _read_agreement_file(slug, context=None): import markdown if not context: context = { 'organization': get_current_provider()} source, _ = loader.find_template('saas/agreements/legal_%s.md' % slug) return markdown.markdown(source.render(Context(context)))
def get_initial(self): """ Populates place order forms with the organization address whenever possible. """ self.customer = self.get_organization() kwargs = super(CardFormMixin, self).get_initial() provider = get_current_provider() if self.customer.country: country = self.customer.country else: country = provider.country if self.customer.region: region = self.customer.region else: region = provider.region kwargs.update({ 'card_name': self.customer.full_name, 'card_city': self.customer.locality, 'card_address_line1': self.customer.street_address, 'country': country, 'region': region, 'card_address_zip': self.customer.postal_code }) return kwargs
def get_organization(self): if self.organization_url_kwarg in self.kwargs: queryset = Organization.objects.filter( slug=self.kwargs.get(self.organization_url_kwarg)) if queryset.exists(): return queryset.get() return get_current_provider()
def is_current_provider(organization): # We do a string compare here because both ``Organization`` might come # from a different db. That is if the organization parameter is not # a unicode string itself. if isinstance(organization, basestring): slug = organization else: slug = organization.slug return slug == get_current_provider().slug
def is_current_provider(organization): # We do a string compare here because both ``Organization`` might come # from a different db. That is if the organization parameter is not # a unicode string itself. slug = '' if isinstance(organization, basestring): slug = organization elif organization: slug = organization.slug return slug == get_current_provider().slug
def to_representation(self, obj): ret = super(TransactionSerializer, self).to_representation(obj) is_debit = self._is_debit(obj) if is_debit: amount = as_money(obj.orig_amount, '-%s' % obj.orig_unit) else: amount = as_money(obj.dest_amount, obj.dest_unit) ret.update({ 'description': as_html_description( obj, provider=get_current_provider()), 'is_debit': is_debit, 'amount': amount}) return ret
def _wrapped_view(request, *args, **kwargs): organization = None if kwargs.has_key('charge'): charge = get_object_or_404( Charge, processor_id=kwargs.get('charge')) organization = charge.customer elif kwargs.has_key('organization'): organization = get_object_or_404(Organization, slug=kwargs.get('organization')) else: organization = get_current_provider() if organization and _contributor_readonly(request, [organization]): return view_func(request, *args, **kwargs) raise PermissionDenied
def to_representation(self, obj): ret = super(TransactionSerializer, self).to_representation(obj) is_debit = self._is_debit(obj) if is_debit: amount = as_money(obj.orig_amount, '-%s' % obj.orig_unit) else: amount = as_money(obj.dest_amount, obj.dest_unit) ret.update({ 'description': as_html_description(obj, provider=get_current_provider()), 'is_debit': is_debit, 'amount': amount }) return ret
def pass_direct(request, charge=None, organization=None, strength=NORMAL): """ Returns True if the request authenticated ``User`` is a direct contributor (or manager) for the ``Organization`` associated to the request. Managers can issue all types of requests (GET, POST, etc.) while contributors are restricted to GET requests. .. image:: perms-contrib.* """ if charge: charge = get_object_or_404(Charge, processor_id=charge) organization = charge.customer elif organization and not isinstance(organization, Organization): organization = get_object_or_404(Organization, slug=organization) else: organization = get_current_provider() return organization and _has_valid_access(request, [organization], strength)
def pass_provider_only(request, charge=None, organization=None, strength=NORMAL): """ Returns True if the request authenticated ``User`` is a contributor (or manager) for a provider to the ``Organization`` associated to the request. When *strength* is NORMAL, managers can issue all types of requests (GET, POST, etc.) while contributors are restricted to GET requests. .. image:: perms-contrib-provider-only.* """ if charge: charge = get_object_or_404(Charge, processor_key=charge) organization = charge.customer elif organization: organization = get_object_or_404(Organization, slug=organization) if organization: return _has_valid_access(request, list(Organization.objects.providers_to(organization)), strength) return _has_valid_access(request, [get_current_provider()])
def get_initial(self): """ Populates place order forms with the organization address whenever possible. """ self.customer = self.get_organization() kwargs = super(CardFormMixin, self).get_initial() provider = get_current_provider() if self.customer.country: country = self.customer.country else: country = provider.country if self.customer.region: region = self.customer.region else: region = provider.region kwargs.update({'card_name': self.customer.full_name, 'card_city': self.customer.locality, 'card_address_line1': self.customer.street_address, 'country': country, 'region': region, 'card_address_zip': self.customer.postal_code}) return kwargs
def pass_provider_only(request, charge=None, organization=None, strength=NORMAL): """ Returns True if the request authenticated ``User`` is a contributor (or manager) for a provider to the ``Organization`` associated to the request. When *strength* is NORMAL, managers can issue all types of requests (GET, POST, etc.) while contributors are restricted to GET requests. .. image:: perms-contrib-provider-only.* """ if charge: charge = get_object_or_404(Charge, processor_key=charge) organization = charge.customer elif organization: organization = get_object_or_404(Organization, slug=organization) if organization: return _has_valid_access( request, list(Organization.objects.providers_to(organization)), strength) return _has_valid_access(request, [get_current_provider()])
def get(self, request, *args, **kwargs): provider = get_current_provider() if pass_direct(request, organization=provider): kwargs.update({self.slug_url_kwarg: provider}) return RedirectView.get(self, request, *args, **kwargs) return super(ProviderRedirectView, self).get(request, *args, **kwargs)
def get_provider(): return get_current_provider()
def describe(transaction): return mark_safe(as_html_description( transaction, provider=get_current_provider()))
def is_direct(request, organization=None): if organization is None: organization = get_current_provider() return pass_direct(request, organization=organization)
def is_current_provider(organization): # XXX Use slug because both organizations might come from a different db. return organization.slug == get_current_provider().slug
def _read_agreement_file(slug, context=None): import markdown if not context: context = {'organization': get_current_provider()} source, _ = loader.find_template('saas/agreements/legal_%s.md' % slug) return markdown.markdown(source.render(Context(context)))