def triadmin(request, app_name, model_name, pk, command, all_models, list_model, create_object, edit_object): # def check_kwargs(kw): # for app_name, model_names in kw.items(): # assert app_name in apps.all_models # for model_name in model_names: # assert model_name in apps.all_models[app_name] # # check_kwargs(model) if app_name is None and model_name is None: result = all_models(request=request) elif command is None: assert pk is Nones result = list_model(request=request, app_name=app_name, model_name=model_name) elif command == 'create': assert pk is None result = create_object( request=request, model=apps.all_models[app_name][model_name], ) elif command == 'edit': assert pk result = edit_object( request=request, instance=apps.all_models[app_name][model_name].objects.get(pk=pk), ) else: assert False, 'unknown command %s' % command return result
def view(request, model, pk, title): return edit_object( request=request, instance=get_object_or_404(model, pk=pk), form__editable=False, form__links=[], render__context__title=evaluate(title, model=model), )
def tri_form_example3(request, pk): def on_save(instance, form, **_): if request.user.is_staff and form.fields_by_name.audit_complete.value: instance.last_audit = datetime.now() instance.auditor = request.user instance.save() return edit_object( request=request, instance=Room.objects.get(pk=pk), on_save=on_save, form__exclude=['auditor', 'last_audit'], form__extra_fields=[ Field.boolean( name='audit_complete', attr=None, # don't write "audit_complete" to the Room object show=request.user.is_staff, ), ], form__field__auditor_notes__show=request.user.is_staff, )
def tri_form_example4(request, pk): def on_save(instance, form, **_): if request.user.contact.is_auditor and form.fields_by_name.audit_complete.value: instance.last_audit = datetime.now() instance.auditor = request.user instance.save() return edit_object( request=request, instance=Room.objects.get(pk=pk), on_save=on_save, form__extra_fields=[ Field.boolean( name='audit_complete', attr=None, # don't write "audit_complete" to the Room object show=request.user.contact.is_auditor, ), Field.heading( name='audit', after='description', show=request.user.contact.is_auditor or request.user.is_staff, ), ], form__field=dict( auditor_notes__show=request.user.is_staff or request.user.contact.is_auditor, auditor_notes__editable=request.user.contact.is_auditor, auditor__editable=False, auditor__show=request.user.is_staff, last_audit__editable=False, last_audit__show=request.user.is_staff, last_audit__container__attrs__class__audit=True, auditor__container__attrs__class__audit=True, auditor_notes__container__attrs__class__audit=True, ), )
def test_create_or_edit_object(): # 1. View create form request = Struct(method='GET', META={}, GET={}, user=Struct(is_authenticated=lambda: True)) response = create_object( request=request, model=CreateOrEditObjectTest, form__field__f_int__initial=1, form__field__f_float__initial=lambda form, field: 2, render_context={'foo': 'FOO'}, render=lambda **kwargs: kwargs) assert response['context_instance']['object_name'] == 'create or edit object test' assert response['context_instance']['is_create'] is True form = response['context_instance']['form'] assert response['context_instance']['foo'] == 'FOO' assert form.mode is INITIALS_FROM_GET assert form.fields_by_name['f_int'].initial == 1 assert form.fields_by_name['f_int'].errors == set() assert form.fields_by_name['f_int'].value == 1 assert form.fields_by_name['f_float'].value == 2 assert form.fields_by_name['f_bool'].value is None assert set(form.fields_by_name.keys()) == {'f_int', 'f_float', 'f_bool'} # 2. Create request.method = 'POST' request.POST = { 'f_int': '3', 'f_float': '5.1', 'f_bool': 'True', '-': '-', } create_object( request=request, model=CreateOrEditObjectTest, render=lambda **kwargs: kwargs) assert get_saved_something() is not None assert get_saved_something().f_int == 3 assert get_saved_something().f_float == 5.1 assert get_saved_something().f_bool is True # 3. View edit form request.method = 'GET' del request.POST response = edit_object( request=request, instance=get_saved_something(), render=lambda **kwargs: kwargs) form = response['context_instance']['form'] assert form.get_errors() == {} assert form.fields_by_name['f_int'].value == 3 assert form.fields_by_name['f_float'].value == 5.1 assert form.fields_by_name['f_bool'].value is True # 4. Edit request.method = 'POST' request.POST = { 'f_int': '7', 'f_float': '11.2', '-': '-', # Not sending a parameter in a POST is the same thing as false } response = edit_object( request=request, instance=get_saved_something(), redirect=lambda form, **_: {'context_instance': {'form': form}}, render=lambda **kwargs: kwargs) form = response['context_instance']['form'] assert form.get_errors() == {} assert form.is_valid() assert get_saved_something() is not None assert get_saved_something().f_int == 7 assert get_saved_something().f_float == 11.2 assert not get_saved_something().f_bool
def test_create_or_edit_object(): # 1. View create form request = Struct(method='GET', META={}, GET={}, user=Struct(is_authenticated=lambda: True)) response = create_object( request=request, model=CreateOrEditObjectTest, form__field__f_int__initial=1, form__field__f_float__initial=lambda form, field: 2, render__context={'foo': 'FOO'}, render=lambda **kwargs: kwargs) assert response['context_instance'][ 'object_name'] == 'create or edit object test' assert response['context_instance']['is_create'] is True form = response['context_instance']['form'] assert response['context_instance']['foo'] == 'FOO' assert form.mode is INITIALS_FROM_GET assert form.fields_by_name['f_int'].initial == 1 assert form.fields_by_name['f_int'].errors == set() assert form.fields_by_name['f_int'].value == 1 assert form.fields_by_name['f_float'].value == 2 assert form.fields_by_name['f_bool'].value is None assert set(form.fields_by_name.keys()) == {'f_int', 'f_float', 'f_bool'} # 2. Create request.method = 'POST' request.POST = { 'f_int': '3', 'f_float': '5.1', 'f_bool': 'True', '-': '-', } create_object(request=request, model=CreateOrEditObjectTest, render=lambda **kwargs: kwargs) assert get_saved_something() is not None assert get_saved_something().f_int == 3 assert get_saved_something().f_float == 5.1 assert get_saved_something().f_bool is True # 3. View edit form request.method = 'GET' del request.POST response = edit_object(request=request, instance=get_saved_something(), render=lambda **kwargs: kwargs) form = response['context_instance']['form'] assert form.get_errors() == {} assert form.fields_by_name['f_int'].value == 3 assert form.fields_by_name['f_float'].value == 5.1 assert form.fields_by_name['f_bool'].value is True # 4. Edit request.method = 'POST' request.POST = { 'f_int': '7', 'f_float': '11.2', '-': '-', # Not sending a parameter in a POST is the same thing as false } response = edit_object( request=request, instance=get_saved_something(), redirect=lambda form, **_: {'context_instance': { 'form': form }}, render=lambda **kwargs: kwargs) form = response['context_instance']['form'] assert form.get_errors() == {} assert form.is_valid() assert get_saved_something() is not None assert get_saved_something().f_int == 7 assert get_saved_something().f_float == 11.2 assert not get_saved_something().f_bool
def example_3(request): return edit_object(request, instance=Foo.objects.all().first())
def test_create_or_edit_object(): # 1. View create form request = Struct(method='GET', META={}, user=Struct(is_authenticated=lambda: True)) response = create_object( request=request, model=CreateOrEditObjectTest, form__f_int__initial=1, form__f_float__initial=lambda form, field: 2, render=lambda **kwargs: kwargs) assert response['context_instance']['object_name'] == 'create or edit object test' assert response['context_instance']['is_create'] == True form = response['context_instance']['form'] assert not form.should_parse assert form.fields_by_name['f_int'].initial == 1 assert form.fields_by_name['f_int'].errors == set() assert form.fields_by_name['f_int'].value == 1 assert form.fields_by_name['f_float'].value == 2 assert form.fields_by_name['f_bool'].value is None # 2. Create request.method = 'POST' request.POST = { 'f_int': '3', 'f_float': '5.1', 'f_bool': 'True', } create_object( request=request, model=CreateOrEditObjectTest, render=lambda **kwargs: kwargs) assert get_saved_something() is not None assert get_saved_something().f_int == 3 assert get_saved_something().f_float == 5.1 assert get_saved_something().f_bool is True # 3. View edit form request.method = 'GET' del request.POST response = edit_object( request=request, instance=get_saved_something(), render=lambda **kwargs: kwargs) form = response['context_instance']['form'] assert form.fields_by_name['f_int'].value == 3 assert form.fields_by_name['f_float'].value == 5.1 assert form.fields_by_name['f_bool'].value is True # 4. Edit request.method = 'POST' request.POST = { 'f_int': '7', 'f_float': '11.2', # Not sending a parameter in a POST is the same thing as false } edit_object( request=request, instance=get_saved_something(), render=lambda **kwargs: kwargs) assert get_saved_something() is not None assert get_saved_something().f_int == 7 assert get_saved_something().f_float == 11.2 assert not get_saved_something().f_bool
def update(request, model, pk, title): return edit_object( request=request, instance=get_object_or_404(model, pk=pk), render__context__title=evaluate(title, model=model), )