class SimpleAdminRPCWrapper(object): def __init__(self, hostname, admin_username, admin_password, ssl, **kwargs): """ We don't want to cause an unnecessary RPC requests if the object access does not actually require RPC. We defer this by using SimpleLazyObject. """ member = PortalMember(**kwargs) self.rpc = SimpleLazyObject( lambda: user.User( hostname, user=admin_username, password=admin_password, api_type='admin', member=member, ssl=ssl, ) ) def create(self): return self.rpc.create() def delete(self): try: return self.rpc.delete() except user.DoesNotExist as e: raise ObjectDoesNotExist(str(e)) def update(self, **kwargs): return self.rpc.update(**kwargs)
class SimpleReplayRPCWrapper(object): """ A simple collection of methods for interacting with the given user's recording functionality (both control of recording, and lookup of past recordings) """ def __init__(self, portal_hostname, replay_hostname, username, password, ssl, **kwargs): """ As replay recording control and library access happen on two separate hosts, we need to create RPC backends for both the portal and the replay. The Lazy behaviour means we don't actually hit 2 servers unless such methods are called. """ self.library_rpc = SimpleLazyObject( lambda: replay.Replay( replay_hostname, username, password, ssl=ssl ) ) self.rpc = SimpleLazyObject( lambda: user.User(portal_hostname, user=username, password=password) ) def all(self): """ generator of all recordings ordered ascending by date""" return (Recording(x, self.library_rpc) for x in self.library_rpc.all_records) def get(self, id): return Recording(self.library_rpc.open(id), self.library_rpc) def filter(self, **kwargs): return (Recording(x, self.library_rpc) for x in self.library_rpc.filter(**kwargs)) def start_recording(self, quality=-1): """Start recording the currently active user conference (if any). If the user is not in a conference raises LookupError.""" self.rpc.start_recording(quality=quality) def stop_recording(self): """ Stop any current recording for the user. Raises LookupError if no recording is in progress. Return a uuid suitable for later retrieval with ``get`` or `filter(uuid=uuid)`` """ return self.rpc.stop_recording()
def __init__(self, portal_hostname, replay_hostname, username, password, ssl, **kwargs): """ As replay recording control and library access happen on two separate hosts, we need to create RPC backends for both the portal and the replay. The Lazy behaviour means we don't actually hit 2 servers unless such methods are called. """ self.library_rpc = SimpleLazyObject( lambda: replay.Replay( replay_hostname, username, password, ssl=ssl ) ) self.rpc = SimpleLazyObject( lambda: user.User(portal_hostname, user=username, password=password) )
class SimpleUserRPCWrapper(object): def __init__(self, hostname, username, password, ssl, pin): self._pin = pin self.rpc = SimpleLazyObject( lambda: user.User(hostname, user=username, password=password, ssl=ssl) ) def authenticate_endpoint(self, **kwargs): """ Get information necessary to allow an endpoint to authenticate itself. All kwargs are passed to the underlying transport. """ # We modify kwargs kwargs = copy.copy(kwargs) guest = kwargs.pop('guest', True) return self.rpc.endpoint_login_data(guest=guest, **kwargs) def activate_endpoint(self, endpoint_id, **kwargs): """ Given an authenticated endpoint, activate it so that calls can be made. It is an error to attempt to activate an inactive client, but this is not checked. """ return self.rpc.activate_endpoint(endpoint_id, **kwargs) def join_conference(self, conference, **kwargs): """ Bring an activated endpoint into a call. It is an error to call this on an inactive endpoint. """ if not isinstance(conference, (int, )): raise TypeError( "Conferences must be referenced by their integer id, or" "be a subclass of VidyoUserBase" ) return self.rpc.join_room(None, room_id=conference, pin=self._pin, **kwargs) def terminate(self): """ Eject the user's endpoints from all calls, and deauthenticate known endpoints. """ self.rpc.log_out()
class GitTool(Tool): def __init__(self, *args, **kwargs): super(GitTool, self).__init__(*args, **kwargs) self.vcs = SimpleLazyObject(lambda: GitVCS(self.path_to_repo())) def path_to_repo(self): return os.path.join(settings.GITTOOL_REPOS_PATH, os.path.normpath(self.project.path) + '.git') def get_default_ref(self): branches = self.branches() return branches[sorted(self.branches())[0]] # TODO: No refs. Get last? def branches(self): return self.vcs.branches() def get_object(self, sha1): return self.vcs.get_object(sha1) def get_object_by_path(self, ref, path): return self.vcs.get_object_by_path(ref, path)
def user_site_profile(request): # If we access request.user, request.session is accessed, which results in # 'Vary: Cookie' being sent in every request that uses this context # processor, which can easily be every request on a site if # TEMPLATE_CONTEXT_PROCESSORS has this context processor added. This kills # the ability to cache. So, we carefully ensure these attributes are lazy. # We don't use django.utils.functional.lazy() for User, because that # requires knowing the class of the object we want to proxy, which could # break with custom auth backends. LazyObject is a less complete but more # flexible solution that is a good enough wrapper for 'User'. def get_user(): if hasattr(request, "user"): return request.user else: return AnonymousUser() # Get the user... user = SimpleLazyObject(get_user) if isinstance(user, AnonymousUser): # it's an AnonymousUser, so use a temporary instance # which will contain the default settings user_site_profile = UserSiteProfile() else: try: # it's a registered user, so get their profile # using the standard get_profile() method... user_site_profile = user.get_profile() except UserSiteProfile.DoesNotExist: # This shouldn't happen unless for some reason # the UserSiteProfile was manually deleted, but # let's be ready for that eventuality, shall we? # Regenerate the instance user_site_profile = UserSiteProfile() user_site_profile.user = user user_site_profile.save() return {"user_site_profile": user_site_profile}
def __init__(self, hostname, admin_username, admin_password, ssl, **kwargs): """ We don't want to cause an unnecessary RPC requests if the object access does not actually require RPC. We defer this by using SimpleLazyObject. """ member = PortalMember(**kwargs) self.rpc = SimpleLazyObject( lambda: user.User( hostname, user=admin_username, password=admin_password, api_type='admin', member=member, ssl=ssl, ) )
def test_bool(self): x = SimpleLazyObject(lambda: 3) self.assertTrue(x) x = SimpleLazyObject(lambda: 0) self.assertFalse(x)
def request(self, **request): """ The master request method. Compose the environment dictionary and pass to the handler, return the result of the handler. Assume defaults for the query environment, which can be overridden using the arguments to the request. """ environ = self._base_environ(**request) # Curry a data dictionary into an instance of the template renderer # callback function. data = {} on_template_render = partial(store_rendered_templates, data) signal_uid = "template-render-%s" % id(request) signals.template_rendered.connect(on_template_render, dispatch_uid=signal_uid) # Capture exceptions created by the handler. exception_uid = "request-exception-%s" % id(request) got_request_exception.connect(self.store_exc_info, dispatch_uid=exception_uid) try: try: response = self.handler(environ) except TemplateDoesNotExist as e: # If the view raises an exception, Django will attempt to show # the 500.html template. If that template is not available, # we should ignore the error in favor of re-raising the # underlying exception that caused the 500 error. Any other # template found to be missing during view error handling # should be reported as-is. if e.args != ('500.html',): raise # Look for a signalled exception, clear the current context # exception data, then re-raise the signalled exception. # Also make sure that the signalled exception is cleared from # the local cache! if self.exc_info: _, exc_value, _ = self.exc_info self.exc_info = None raise exc_value # Save the client and request that stimulated the response. response.client = self response.request = request # Add any rendered template detail to the response. response.templates = data.get("templates", []) response.context = data.get("context") response.json = partial(self._parse_json, response) # Attach the ResolverMatch instance to the response response.resolver_match = SimpleLazyObject(lambda: resolve(request['PATH_INFO'])) # Flatten a single context. Not really necessary anymore thanks to # the __getattr__ flattening in ContextList, but has some edge-case # backwards-compatibility implications. if response.context and len(response.context) == 1: response.context = response.context[0] # Update persistent cookie data. if response.cookies: self.cookies.update(response.cookies) return response finally: signals.template_rendered.disconnect(dispatch_uid=signal_uid) got_request_exception.disconnect(dispatch_uid=exception_uid)
def test_hash(self): # hash() equality would not be true for many objects, but it should be # for _ComplexObject self.assertEqual(hash(complex_object()), hash(SimpleLazyObject(complex_object)))
def process_request(self, request): request.website = SimpleLazyObject(lambda: get_website(request))
from django.conf import settings from django.forms.utils import flatatt as _flatatt from django.template import Context from django.template.loader import get_template from django.utils.functional import SimpleLazyObject from django.utils.lru_cache import lru_cache from .base import KeepContext from .compatibility import PY2, text_type def get_template_pack(): return getattr(settings, 'CRISPY_TEMPLATE_PACK', 'bootstrap') TEMPLATE_PACK = SimpleLazyObject(get_template_pack) # By caching we avoid loading the template every time render_field # is called without a template @lru_cache() def default_field_template(template_pack=TEMPLATE_PACK): return get_template("%s/field.html" % template_pack) def widget_get_context(name, value, attrs={}): #context = super().get_context(name, value, attrs) context['widget']['name'] = 'test' return context def set_field_html_name(cls, new_name):
app_label = 'gui' verbose_name = _('Permission') verbose_name_plural = _('Permissions') def __unicode__(self): return '%s' % self.alias class SuperAdminPermission(object): """ Dummy special permission for users with is_staff=True. """ is_dummy = True id = pk = None name = 'super_admin' alias = 'SuperAdmin' AdminPermission = SimpleLazyObject(lambda: Permission.objects.get(name='admin')) NetworkAdminPermission = SimpleLazyObject(lambda: Permission.objects.get(name='network_admin')) ImageAdminPermission = SimpleLazyObject(lambda: Permission.objects.get(name='image_admin')) ImageImportAdminPermission = SimpleLazyObject(lambda: Permission.objects.get(name='image_import_admin')) TemplateAdminPermission = SimpleLazyObject(lambda: Permission.objects.get(name='template_admin')) IsoAdminPermission = SimpleLazyObject(lambda: Permission.objects.get(name='iso_admin')) UserAdminPermission = SimpleLazyObject(lambda: Permission.objects.get(name='user_admin')) DnsAdminPermission = SimpleLazyObject(lambda: Permission.objects.get(name='dns_admin')) # Set of strange DC-mixed permission names AnyDcPermissionSet = frozenset(['network_admin', 'image_admin', 'template_admin', 'iso_admin', 'user_admin', 'dns_admin', 'image_import_admin'])
def __call__(self, request): if not hasattr(request, 'jwt_info'): request.user = SimpleLazyObject(lambda: get_user(request)) request.jwt_info = SimpleLazyObject(lambda: get_jwt_info(request)) response = self.get_response(request) return response
def mock_ngram_storage(tmpdir): with mock.patch('capdb.storages.ngram_kv_store._wrapped', SimpleLazyObject(lambda: capdb.storages.NgramRocksDB(path=str(tmpdir)))), \ mock.patch('capdb.storages.ngram_kv_store_ro._wrapped', SimpleLazyObject(lambda: capdb.storages.NgramRocksDB(path=str(tmpdir), read_only=True))): yield None
def test_serialize_lazy_objects(self): pattern = re.compile(r'^foo$', re.UNICODE) lazy_pattern = SimpleLazyObject(lambda: pattern) self.assertEqual(self.serialize_round_trip(lazy_pattern), pattern)
from xdist.scheduler import LoadFileScheduling import mock from moto import mock_s3 from django.core.signals import request_started, request_finished from django.core.cache import cache as django_cache from django.core.management import call_command from django.db import connections import django.apps from django.utils.functional import SimpleLazyObject from rest_framework.test import APIRequestFactory, APIClient # Before importing any of our code, mock capdb.storages redis clients. # Do this here so anything that gets imported later will get the mocked versions. import capdb.storages capdb.storages.redis_client = SimpleLazyObject(lambda: None) capdb.storages.redis_ingest_client = SimpleLazyObject(lambda: None) # our packages import fabfile from .factories import * ### Pytest scheduling ### def pytest_xdist_make_scheduler(config, log): """ pytest-django doesn't currently work with pytest-xdist and multiple databases. This allows us to run some tests in parallel anyway, by naming tests that don't access the database with "__parallel", putting them into a separate bucket and then running "pytest -n 2". """
def test_class(self): # This is important for classes that use __class__ in things like # equality tests. self.assertEqual(_ComplexObject, SimpleLazyObject(complex_object).__class__)
def test_text(self): self.assertEqual("joe", six.text_type(SimpleLazyObject(complex_object)))
def test_bytes(self): self.assertEqual(b"I am _ComplexObject('joe')", bytes(SimpleLazyObject(complex_object)))
thresholds = {'Threads_running': 10} start = time.time() names = thresholds.keys() while True: current = self.get_many(names) higher = [] for name in names: if current[name] > thresholds[name]: higher.append(name) if not higher: return if timeout and time.time() > start + timeout: raise TimeoutError( "Span too long waiting for load to drop: " + ",".join("{} > {}".format(name, thresholds[name]) for name in higher), ) time.sleep(sleep) class SessionStatus(BaseStatus): query = "SHOW SESSION STATUS" global_status = SimpleLazyObject(GlobalStatus) session_status = SimpleLazyObject(SessionStatus)
class GEOSCoordSeq_t(Structure): pass class GEOSContextHandle_t(Structure): pass # Pointers to opaque GEOS geometry structures. GEOM_PTR = POINTER(GEOSGeom_t) PREPGEOM_PTR = POINTER(GEOSPrepGeom_t) CS_PTR = POINTER(GEOSCoordSeq_t) CONTEXT_PTR = POINTER(GEOSContextHandle_t) lgeos = SimpleLazyObject(load_geos) class GEOSFuncFactory: """ Lazy loading of GEOS functions. """ argtypes = None restype = None errcheck = None def __init__(self, func_name, *args, restype=None, errcheck=None, argtypes=None, **kwargs): self.func_name = func_name if restype is not None: self.restype = restype if errcheck is not None:
def process_request(self, request): request.user = SimpleLazyObject(lambda: self.__class__.get_jwt_user(request)) if request.user is none: return HttpResponse("Invalid token", status=401)
def __call__(self, request): request.user = SimpleLazyObject(lambda: self.__class__.get_jwt_user(request)) return self.get_response(request)
def process_request(self, request): request.user = SimpleLazyObject(lambda: get_user(request))
ALLOWED_ATTRIBUTES = { 'a': ['href', 'title', 'class'], 'abbr': ['title'], 'acronym': ['title'], 'table': ['width'], 'td': ['width', 'align'], 'div': ['class'], 'p': ['class'], 'span': ['class', 'title'], # Update doc/user/markdown.rst if you change this! } ALLOWED_PROTOCOLS = ['http', 'https', 'mailto', 'tel'] URL_RE = SimpleLazyObject( lambda: build_url_re(tlds=sorted(tld_set, key=len, reverse=True))) EMAIL_RE = SimpleLazyObject( lambda: build_email_re(tlds=sorted(tld_set, key=len, reverse=True))) def safelink_callback(attrs, new=False): """ Makes sure that all links to a different domain are passed through a redirection handler to ensure there's no passing of referers with secrets inside them. """ url = attrs.get((None, 'href'), '/') if not url_has_allowed_host_and_scheme( url, allowed_hosts=None) and not url.startswith( 'mailto:') and not url.startswith('tel:'): signer = signing.Signer(salt='safe-redirect')
def process_request(self, request): request.regstage = SimpleLazyObject(lambda: get_stage(request))
def process_request(self, request): # Store the current request in the local thread so the template loader has access utils.set_request(request) request.theme = SimpleLazyObject(lambda: get_theme(request))
def __init__(self, hostname, username, password, ssl, pin): self._pin = pin self.rpc = SimpleLazyObject( lambda: user.User(hostname, user=username, password=password, ssl=ssl) )
def __init__(self, *args, **kwargs): super(GitTool, self).__init__(*args, **kwargs) self.vcs = SimpleLazyObject(lambda: GitVCS(self.path_to_repo()))
def middleware(request): request.seo = SimpleLazyObject(lambda: get_url_seo(request)) response = get_response(request) return response
def test_equality(self): self.assertEqual(complex_object(), SimpleLazyObject(complex_object)) self.assertEqual(SimpleLazyObject(complex_object), complex_object())
def lazy_wrap(self, wrapped_object): return SimpleLazyObject(lambda: wrapped_object)
def test_force_text_lazy(self): s = SimpleLazyObject(lambda: 'x') self.assertTrue(type(force_text(s)), str)
# Initialize the class, nothing more happens for now. logger.debug("Initializing plugin: {}".format( PluginClass.__name__)) self.append(PluginClass()) self.apps.add(app) if not was_configured and settings.configured: logger.warn( "Initializing plugin {} caused Django settings to be configured" .format(app)) was_configured = True except ImportError: pass def __initialize(): """ Called once to register hook callbacks. """ registry = Registry() logger.debug("Loading kolibri plugin registry...") was_configured = settings.configured if was_configured: logger.warn( "Django settings already configured when plugin registry initialized" ) registry.register(config.ACTIVE_PLUGINS, was_configured=was_configured) return registry registered_plugins = SimpleLazyObject(__initialize)
def createPaypalPayment(request): ''' This view handles the creation of Paypal Express Checkout Payment objects. All Express Checkout payments must either be associated with a pre-existing Invoice or a registration, or they must have an amount and type passed in the post data (such as gift certificate payment requests). ''' logger.info('Received request for Paypal Express Checkout payment.') invoice_id = request.POST.get('invoice_id') tr_id = request.POST.get('reg_id') amount = request.POST.get('amount') submissionUserId = request.POST.get('user_id') transactionType = request.POST.get('transaction_type') taxable = request.POST.get('taxable', False) # If a specific amount to pay has been passed, then allow payment # of that amount. if amount: try: amount = float(amount) except ValueError: logger.error('Invalid amount passed') return HttpResponseBadRequest() # Parse if a specific submission user is indicated submissionUser = None if submissionUserId: try: submissionUser = User.objects.get(id=int(submissionUserId)) except (ValueError, ObjectDoesNotExist): logger.warning( 'Invalid user passed, submissionUser will not be recorded.') try: # Invoice transactions are usually payment on an existing invoice. if invoice_id: this_invoice = Invoice.objects.get(id=invoice_id) this_description = _('Invoice Payment: %s' % this_invoice.id) if not amount: amount = this_invoice.outstandingBalance # This is typical of payment at the time of registration elif tr_id: tr = TemporaryRegistration.objects.get(id=int(tr_id)) this_invoice = Invoice.get_or_create_from_registration( tr, submissionUser=submissionUser) this_description = _('Registration Payment: #%s' % tr_id) if not amount: amount = this_invoice.outstandingBalance # All other transactions require both a transaction type and an amount to be specified elif not transactionType or not amount: logger.error( 'Insufficient information passed to createPaypalPayment view.') raise ValueError else: # Gift certificates automatically get a nicer invoice description if transactionType == 'Gift Certificate': this_description = _('Gift Certificate Purchase') else: this_description = transactionType this_invoice = Invoice.create_from_item( float(amount), this_description, submissionUser=submissionUser, calculate_taxes=(taxable is not False), transactionType=transactionType, ) except (ValueError, ObjectDoesNotExist) as e: logger.error( 'Invalid registration information passed to createPaypalPayment view: (%s, %s, %s)' % (invoice_id, tr_id, amount)) logger.error(e) return HttpResponseBadRequest() this_currency = getConstant('general__currencyCode') this_total = min(this_invoice.outstandingBalance, amount) if not getConstant('registration__buyerPaysSalesTax'): this_subtotal = this_total - this_invoice.taxes else: this_subtotal = this_total this_transaction = { 'amount': { 'total': this_total, 'currency': this_currency, 'details': { 'subtotal': this_subtotal, 'tax': this_invoice.taxes, }, }, 'description': str(this_description), 'item_list': { 'items': [] } } for item in this_invoice.invoiceitem_set.all(): if not getConstant('registration__buyerPaysSalesTax'): this_item_price = item.grossTotal - item.taxes else: this_item_price = item.grossTotal this_transaction['item_list']['items'].append({ 'name': str(item.name), 'price': this_item_price, 'tax': item.taxes, 'currency': this_currency, 'quantity': 1, }) # Because the Paypal API requires that the subtotal add up to the sum of the item # totals, we must add a negative line item for discounts applied, and a line item # for the remaining balance if there is to be one. if this_invoice.grossTotal != this_invoice.total: this_transaction['item_list']['items'].append({ 'name': str(_('Total Discounts')), 'price': this_invoice.total - this_invoice.grossTotal, 'currency': this_currency, 'quantity': 1, }) if this_invoice.amountPaid > 0: this_transaction['item_list']['items'].append({ 'name': str(_('Previously Paid')), 'price': -1 * this_invoice.amountPaid, 'currency': this_currency, 'quantity': 1, }) if amount != this_invoice.outstandingBalance: this_transaction['item_list']['items'].append({ 'name': str(_('Remaining Balance After Payment')), 'price': amount - this_invoice.outstandingBalance, 'currency': this_currency, 'quantity': 1, }) # Paypal requires the Payment request to include redirect URLs. Since # the plugin can handle actual redirects, we just pass the base URL for # the current site. site = SimpleLazyObject(lambda: get_current_site(request)) protocol = 'https' if request.is_secure() else 'http' base_url = SimpleLazyObject( lambda: "{0}://{1}".format(protocol, site.domain)) payment = Payment({ 'intent': 'sale', 'payer': { 'payment_method': 'paypal' }, 'transactions': [this_transaction], 'redirect_urls': { 'return_url': str(base_url), 'cancel_url': str(base_url), } }) if payment.create(): logger.info('Paypal payment object created.') if this_invoice: this_invoice.status = Invoice.PaymentStatus.authorized this_invoice.save() # We just keep a record of the ID and the status, because the # API can be used to look up everything else. PaypalPaymentRecord.objects.create( paymentId=payment.id, invoice=this_invoice, status=payment.state, ) return JsonResponse(payment.to_dict()) else: logger.error('Paypal payment object not created.') logger.error(payment) logger.error(payment.error) if this_invoice: this_invoice.status = Invoice.PaymentStatus.error this_invoice.save() return HttpResponseBadRequest()
def test_repr(self): # For debugging, it will really confuse things if there is no clue that # SimpleLazyObject is actually a proxy object. So we don't # proxy __repr__ self.assertTrue( "SimpleLazyObject" in repr(SimpleLazyObject(complex_object)))
def process_request(self, request): request.user = SimpleLazyObject(lambda: self.__class__.get_jwt_user(request))
helpers = HelpersNamespace() from wshop.front.template_helpers import general, product, category, urls helpers.general = general helpers.product = product helpers.category = category helpers.urls = urls for namespace in get_provide_objects("front_template_helper_namespace"): if namespace and getattr(namespace, "name", None): if callable(namespace): # If it's a class, instantiate it namespace = namespace() setattr(helpers, namespace.name, namespace) return helpers library.global_function(name="wshop", fn=SimpleLazyObject(_get_helpers)) @lru_cache() def _cached_markdown(str_value): return Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.nl2br', ], output_format="html5").convert(str_value) @library.filter(name="markdown") def markdown(value): return mark_safe(_cached_markdown(force_text(value)))