def wrapper(request, *args, **kwargs): from localground.apps.site.models import Project r = request.GET or request.POST cookies = request.COOKIES project = None user = request.user # inner method to get most recent project if default selection isn't # valid def get_default_project(user): projects = Project.objects.filter( owner=user).order_by('-time_stamp') if len(projects) > 0: return projects[0] return None # 1) if anonymous request, return error: if user.is_anonymous(): return HttpResponse(json.dumps({ 'code': 'failure', 'message': 'User cannot be anonymous' })) # 2) order matters (defer to request param before cookie) # or cookies.get('project_id_' + user.username) project_id = r.get('project_id') if project_id is not None: if project_id in ['all', 'all#', '']: project = None elif project_id in ['add', 'add#']: project_name = r.get('project_name', None) if project_name is None: project = None else: # create a new project from localground.apps.site.models import UserProfile import time profile = UserProfile.objects.get(user=request.user) project = Project() project.name = project_name project.owner = user project.slug = '%s-%s' % (project_name, int(time.time())) project.access_authority = profile.default_view_authority project.save() else: try: project = Project.objects.get(id=int(project_id)) except ValueError: project = get_default_project(user) except Project.DoesNotExist: project = get_default_project(user) # is user authorized? # if project is not None and project.owner != user and not # user.is_superuser: if project.can_view(user) == False: return HttpResponse(json.dumps({ 'code': 'failure', 'message': 'Not authorized to view information for %s' % project.name })) else: # if no project id defined, pick the most recently updated project: project = get_default_project(user) # 3) update kwargs dict to return 'identity' entry to calling function: if kwargs is None: kwargs = {} kwargs.update({'project': project}) return function(request, *args, **kwargs)
def object_list_form( request, object_type_plural, return_message=None, embed=False): context = RequestContext(request) ModelClass = Base.get_model(model_name_plural=object_type_plural) template_name = 'profile/%s.html' % ModelClass.model_name_plural.replace( ' ', '-') r = request.POST or request.GET objects = ModelClass.objects.get_objects( user=request.user, request=request, context=context ) #return HttpResponse(objects.query) per_page = 10 def getModelClassFormSet(**kwargs): # uses Django 1.2 workaround documented here: # https://groups.google.com/forum/?fromgroups=#!topic/django-users/xImbCAbmfuc from django.forms.models import modelformset_factory def create_formfield(f, **kwargs): return f.formfield(**kwargs) return modelformset_factory( ModelClass, max_num=0, formfield_callback=create_formfield, **kwargs ) ModelClassFormSet = getModelClassFormSet( form=ModelClass.inline_form( request.user)) if request.method == "POST": modelformset = ModelClassFormSet( request.POST, queryset=objects) # objects if modelformset.is_valid(): num_updates = 0 for form in modelformset.forms: if form.has_changed(): instance = form.instance instance.last_updated_by = request.user instance.time_stamp = datetime.now() instance.save() num_updates += 1 if num_updates > 0: context.update({'message': '%s %s have been updated' % ( num_updates, ModelClass.model_name_plural)}) else: context.update({'warning_message': '%s %s have been updated' % ( num_updates, ModelClass.model_name_plural)}) else: context.update({ 'error_message': 'There was an error updating the %s' % ModelClass.model_name_plural }) else: start = 0 if r.get('page') is not None: start = (int(r.get('page')) - 1) * per_page # Hack: somehow, calling the ".all()" method slices the queryset (LIMIT BY), # rather than converting the queryset to a list (which we don't want). modelformset = ModelClassFormSet( queryset=objects[ start:start + per_page]) #modelformset = ModelClassFormSet(queryset=objects[start:start+per_page]) context.update({ 'formset': modelformset, 'embed': embed, 'page_title': 'My %s' % ModelClass.model_name_plural.capitalize(), 'username': request.user.username, 'url': '%s?1=1' % ModelClass.listing_url(), 'delete_url': ModelClass.batch_delete_url(), 'create_url': ModelClass.create_url() + 'embed/', 'page_title': 'My %s' % ModelClass.model_name_plural.capitalize(), 'user': request.user, 'object_name_plural': ModelClass.model_name_plural, 'object_type': ModelClass.model_name }) if context.get('filter_fields'): context.update({'url': '%s?query=%s' % (ModelClass.listing_url(), context.get('sql')), }) else: context.update({ 'filter_fields': ModelClass.get_filter_fields(), 'sql': '', 'has_filters': False }) context.update(prep_paginator(request, objects, per_page=per_page)) if request.user.is_superuser: context.update({'users': Project.get_users()}) return render_to_response(template_name, context)