def logs_view(request): '''create a list of all update events of all users.''' users = User.objects.all() cruds = [Crud(user, user=True) for user in users] events = [] for crud in cruds: events.extend(crud.events) events = sorted(events, reverse=True) #create placeholder to hold all events and create updates_str crud = Crud(users[0], user=True) crud.events = events nedits = len(events) var = {'cruds': cruds, 'page_name': 'logs', 'updates': crud.updates_str} var.update({'nedits': nedits}) return render(request, 'accounts/logs.html', var)
def log_view(request): '''create a list of all update events performed by a particular user.''' user = User.objects.get(username=request.user.username) crud = Crud(user, user=True) nedits = len(crud.events) var = {'crud': crud, 'page_name': 'logs', 'nedits': nedits} return render(request, 'accounts/log.html', var)
def edit_model(request, name_space, model_name, app_name, instance_id = None, formset_names='', focus='default', view ='complete'): '''edit view generalized over models. assumes a 'add_{{model_name}}.html template and edit_{{model_name}} function and {{model_name}}Form ''' start = time.time() names = formset_names model = apps.get_model(app_name,model_name) modelform = view_util.get_modelform(name_space,model_name+'Form') print('get model and form',delta(start)) instance= model.objects.get(pk=instance_id) if instance_id else None crud = Crud(instance) if instance and model_name != 'Location' else None print('get crud',delta(start)) ffm, form = None, None if request.method == 'POST': focus, button = getfocus(request), getbutton(request) if button in 'delete,cancel,confirm_delete': return delete_model(request,name_space,model_name,app_name,instance_id) copy_instance = copy_complete(instance) if button == 'saveas' and instance else False form = modelform(request.POST, request.FILES, instance=instance) print('made form in post',delta(start)) if form.is_valid() or copy_instance: print('form is valid: ',form.cleaned_data,type(form)) if not button == 'saveas':instance = form.save() else:instance = copy_instance if view == 'complete': ffm = FormsetFactoryManager(name_space,names,request,instance) valid = ffm.save() print('formset factory manager / form making done',delta(start)) if valid: print('validated form',delta(start)) show_messages(request,button, model_name) if button== 'add_another': return HttpResponseRedirect(reverse(app_name+':add_'+model_name.lower())) return HttpResponseRedirect(reverse( app_name+':edit_'+model_name.lower(), kwargs={'pk':instance.pk,'focus':focus})) else: print('ERROR',ffm.errors) else: return HttpResponseRedirect('/utilities/close/') else: print('form invalid:',form.non_field_errors()[0]) show_messages(request,'form_invalid', model_name, form) print('post part done',delta(start)) if not form: form = modelform(instance=instance) if not ffm: ffm = FormsetFactoryManager(name_space,names,instance=instance) print('(after post formset factory manager / form making done',delta(start)) tabs = make_tabs(model_name.lower(), focus_names = focus) print('tabs made',delta(start)) page_name = 'Edit ' +model_name.lower() if instance_id else 'Add ' +model_name.lower() helper = help_util.Helper(model_name=model_name) print('helper made',delta(start)) args = {'form':form,'page_name':page_name,'crud':crud,'model_name':model_name, 'app_name':app_name,'tabs':tabs, 'view':view,'helper':helper.get_dict(), 'instance':instance} args.update(ffm.dict) print('arg made, start rendering',delta(start)) return render(request,app_name+'/add_' + model_name.lower() + '.html',args)
def log_view(request): '''create a list of all update events performed by a particular user.''' user = User.objects.get(username=request.user.username) crud = Crud(user, user=True) var = { 'crud': crud, 'page_name': 'logs' } # {'map_name':map_name,'location_name':location_name} return render(request, 'accounts/log.html', var)
def logs_view(request): '''create a list of all update events of all users.''' users = User.objects.all() cruds = [Crud(user, user=True) for user in users] events = [] for crud in cruds: events.extend(crud.events) events = sorted(events, reverse=True) crud.events = events[:200] updates = crud.updates_str var = {'cruds': cruds, 'page_name': 'logs', 'updates': crud.updates_str} return render(request, 'accounts/logs.html', var)
def edit_comment(request, app_name='', model_name='',entry_pk=None,user_pk=None): if app_name and model_name: model = apps.get_model(app_name,model_name) instance = model.objects.get(pk=entry_pk) crud = Crud(instance) print('commentator',request.user) print('addressee',crud.contributers) app_name, model_name = app_name.lower(), model_name.lower() extra = 0 if user_pk else 1 CommentFormset = modelformset_factory(Comment, CommentForm, fields=('subject','comment','fixed'), extra=extra, can_delete=True) if user_pk: user = get_user_model().objects.get(pk = user_pk) queryset = Comment.objects.filter(user_addressee__icontains = user.username) else: queryset=Comment.objects.filter(app_name=app_name,model_name=model_name,entry_pk=entry_pk) formset =[] if request.method == 'POST': formset = CommentFormset(request.POST,queryset=queryset) if formset.is_valid(): instances = formset.save(commit=False) for x in instances: if user_pk: x.save() else:_handle_comment(x,app_name,model_name,entry_pk,crud.contributers,request.user) for obj in formset.deleted_objects: obj.delete() print('save is a success') if not user_pk: return render(request,'utilities/close.html') else: print('not valid',formset.errors,app_name,model_name,entry_pk) if not formset: formset = CommentFormset(queryset=queryset) if user_pk: page_name = 'Comments addressed to: ' user_comment = True else: page_name = 'Comments about: ' + str(instance) user_comment = False var = {'formset':formset,'page_name':page_name,'model_name':model_name, 'user_comment':user_comment} return render(request, 'utilities/add_comment.html',var)
def edit_model(request, name_space, model_name, app_name, instance_id=None, formset_names='', focus='', view='complete'): '''edit view generalized over models. assumes a 'add_{{model_name}}.html template and edit_{{model_name}} function and {{model_name}}Form ''' names = formset_names model = apps.get_model(app_name, model_name) modelform = view_util.get_modelform(name_space, model_name + 'Form') instance = model.objects.get(pk=instance_id) if instance_id else None crud = Crud(instance) if instance else None ffm, form = None, None if request.method == 'POST': focus, button = getfocus(request), getbutton(request) if button in 'delete,cancel,confirm_delete': return delete_model(request, name_space, model_name, app_name, instance_id) if button == 'saveas' and instance: instance = copy_complete(instance) form = modelform(request.POST, request.FILES, instance=instance) if form.is_valid(): print('form is valid: ', form.cleaned_data, type(form)) instance = form.save() if view == 'complete': ffm = FormsetFactoryManager(name_space, names, request, instance) valid = ffm.save() if valid: show_messages(request, button, model_name) if button == 'add_another': # return HttpResponseRedirect(reverse(app_name + ':add_' + model_name.lower())) return HttpResponseRedirect( reverse(app_name + ':' + model_name.lower() + '-insert')) # return HttpResponseRedirect(reverse( # app_name + ':edit_' + model_name.lower(), # kwargs={'pk': instance.pk, 'focus': focus})) return HttpResponseRedirect( reverse(app_name + ':' + model_name.lower() + '-update', kwargs={ 'pk': instance.pk, 'focus': focus })) else: print('ERROR', ffm.errors) else: return HttpResponseRedirect('/utilities/close/') if not form: form = modelform(instance=instance) if not ffm: ffm = FormsetFactoryManager(name_space, names, instance=instance) tabs = make_tabs(model_name.lower(), focus_names=focus) page_name = 'Edit ' + model_name.lower( ) if instance_id else 'Add ' + model_name.lower() args = { 'form': form, 'page_name': page_name, 'crud': crud, 'tabs': tabs, 'view': view } args.update(ffm.dict) return render(request, app_name + '/add_' + model_name.lower() + '.html', args)
def _add_crud(self): if not hasattr(self,'crud'): from utils.view_util import Crud self.crud = Crud(self)