def fill_default(sender=None, instance=None, created=False, **kwargs): try: from current_user import get_current_user, get_request import datetime request = get_request() user = get_current_user() if 'admin' in request.path and user.is_authenticated() and user.is_superuser: return if hasattr(instance, 'owner'): if not instance.owner: u = get_current_user() if u and u.is_authenticated(): instance.owner = u if hasattr(instance, 'owner_ip'): if not instance.owner_ip: request = get_request() instance.owner_ip = request.META['REMOTE_ADDR'] if hasattr(instance, 'created_on'): if not instance.created_on: instance.created_on = datetime.datetime.now() if hasattr(instance, 'updater'): u = get_current_user() if u and u.is_authenticated(): instance.updater = u if hasattr(instance, 'updated_on'): instance.updated_on = datetime.datetime.now() except: pass
def parse_variable(expression, storage={}): if expression is None: expression = '' request = get_request() c = RequestContext(request, storage); v = Variable(expression).resolve(c); return v
def _remove_guest_fields_if_authen(form): from current_user import get_request request = get_request() if request.user.is_authenticated(): for k,v in form.fields.items(): if k.startswith('guest_'): del form.fields[k]
def render_expression(expression, storage={}): if expression is None: expression = '' try: if expression.startswith('='): rendered = parse_variable(expression[1:], storage) else: request = get_request() c = RequestContext(request, storage) t = Template(expression) rendered = t.render(c) return rendered except Exception, e: return unicode(e)
def Form(form_type, request=None, **kw): from current_user import get_request request = get_request() if isinstance(form_type, HttpRequest): # backward compat (request, form_type) = (form_type, request) class _FormWrapper(form_type): def __init__(self, *args, **kwargs): super(_FormWrapper, self).__init__(*args, **kwargs) _remove_guest_fields_if_authen(self) _remove_unprivilege_fields(self) if request.method=='POST' and not request.GET.get('PARTIAL'): return _FormWrapper(data=request.POST, files=request.FILES, **kw) else: return _FormWrapper(**kw)
def redirect(*args, **kwargs): from current_user import get_request url = None request = get_request() if 'next' in request.GET: url = request.GET.get('next') if 'next' in request.POST: url = request.POST.get('next') if not url: url = uri(*args, **kwargs) else: if 'info' in kwargs: qmap = dict([ (x, kwargs.get(x)) for x in "info ask ask_url".split() if kwargs.get(x) ]) if qmap: url = url + ('&' if '?' in url else '?') url = url + '&'.join([ "%s=%s" % (k, urlencode(v)) for k,v in qmap.items()]) return HttpResponseRedirect(url)
def render_widget(self, entity, context, storage={}): (widget_name, widget_context) = context widget = get_widget(widget_name)(context=widget_context, storage=storage) request = get_request() return widget.render(request)