Skip to content

Conversation

moumoutte
Copy link
Contributor

@moumoutte moumoutte commented Feb 12, 2017

Use case

When the contextualized schema is already stored in database (First step to reach the versioning directly in formidable) generate the associated django form class.

Used with Contextualized definition

schema = ContextFormSerializer(instance=formidable, context={
    'role': 'jedi'
}).data

form_class = get_dynamic_form_class_from_schema(schema)
form = form_class(data={'first_name': 'Obiwan', 'last_name': 'Kenobi'})
form.is_valid()

Remaining Standard Fields to Handle

  • Radio
  • Radio Button (I'm really asking if we need to handle it in formidable, because it's not a standard input, it's specific needed for peopleask)
  • Checkboxes

Remaining Format Field to handle

  • Help Text
  • Separator Field
  • Title

@moumoutte moumoutte force-pushed the 170_form_class_from_json branch 3 times, most recently from 13e1213 to bff7624 Compare February 12, 2017 21:42
schema = ContextFormSerializer(instance=formidable, context={
'role': 'jedi'
}).data
form = get_dynamic_form_class_from_schema(schema)()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is it here? a form instance or a class?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I understood: get_dynamic_form_class_from_schema(schema) is a class, so get_dynamic_form_class_from_schema(schema)() is an instance.

Copy link
Contributor

@brunobord brunobord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this new tool in our toolbox. although, I'd recommend to rename the "form" variable in the test to reflect its true nature: it's a form_class object, rather than a "ready to use" form instance.
It'll make the tests more readable


def get_dynamic_form_class_from_schema(schema, field_factory=None):
"""
Schema Already contextualized
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of this docstring. maybe it could be a bit more consistent, using a simple code sample, for example? And the first line should read:

Return a dynamically generated and contextualized form class

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"[WIP]"

if role:
# The role is previously "prefetch" in order to avoid database
# hit, we don't use a get() method in queryset.
return self.field.accesses.all()[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens here if the field.accesses.all() is empty?

I'm not sure, but if field.accesses.all() is a queryset, you can use .first() that'll eventually return None if the queryset is empty. (but I don't know if it has side effects)

Copy link
Contributor Author

@moumoutte moumoutte Feb 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The role is previously "prefetch" in order to avoid database
hit, we don't use a get() method in queryset"

Not sur first will not hit the database with prefetch related call before.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for .first()

Copy link
Contributor Author

@moumoutte moumoutte Feb 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using "first" instead of "all()[0]"

======================================================================
FAIL: test_queryset_without_role (tests.test_forms.TestDynamicForm)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/moumoutte/projects/django-formidable/demo/tests/test_forms.py", line 305, in test_queryset_without_role
    form.get_django_form_class(role='jedi')
  File "/home/moumoutte/.virtualenvs/formidable/local/lib/python2.7/site-packages/django_perf_rec/api.py", line 91, in __exit__
    self.save_or_assert()
  File "/home/moumoutte/.virtualenvs/formidable/local/lib/python2.7/site-packages/django_perf_rec/api.py", line 127, in save_or_assert
    assert self.record == orig_record, msg
AssertionError: Performance record did not match for TestDynamicForm.test_queryset_without_role
  db: SELECT ... FROM "formidable_field" WHERE "formidable_field"."form_id" = # ORDER BY "formidable_field"."order" ASC
  db: SELECT ... FROM "formidable_item" WHERE "formidable_item"."field_id" IN (...) ORDER BY "formidable_item"."order" ASC
  db: SELECT ... FROM "formidable_access" WHERE ("formidable_access"."access_id" = # AND "formidable_access"."field_id" IN (...))
  db: SELECT ... FROM "formidable_validation" WHERE "formidable_validation"."field_id" IN (...)
  db: SELECT ... FROM "formidable_default" WHERE "formidable_default"."field_id" IN (...)
+ db: SELECT ... FROM "formidable_access" WHERE "formidable_access"."field_id" = # ORDER BY "formidable_access"."id" ASC LIMIT #
+ db: SELECT ... FROM "formidable_access" WHERE "formidable_access"."field_id" = # ORDER BY "formidable_access"."id" ASC LIMIT #
+ db: SELECT ... FROM "formidable_access" WHERE "formidable_access"."field_id" = # ORDER BY "formidable_access"."id" ASC LIMIT #
+ db: SELECT ... FROM "formidable_access" WHERE "formidable_access"."field_id" = # ORDER BY "formidable_access"."id" ASC LIMIT #
  db: SELECT ... FROM "formidable_preset" WHERE "formidable_preset"."form_id" = #

@moumoutte
Copy link
Contributor Author

it's a form_class

It's not.

@moumoutte
Copy link
Contributor Author

Need #173 in order to continue.

@moumoutte moumoutte force-pushed the 170_form_class_from_json branch 3 times, most recently from 9d21e02 to c4db3b8 Compare February 26, 2017 18:27
@wo0dyn wo0dyn changed the title [WIP] Provide a tools in order to generate django-form class from json contextualized definition Provide a tools in order to generate django-form class from json contextualized definition Mar 6, 2017
@wo0dyn wo0dyn self-assigned this Mar 6, 2017
@wo0dyn wo0dyn force-pushed the 170_form_class_from_json branch 4 times, most recently from 84673c7 to 18f9120 Compare March 24, 2017 15:17
@wo0dyn wo0dyn dismissed brunobord’s stale review March 24, 2017 15:44

Changes requested addressed


class FormFieldFactory(BaseFormFieldFactory):

field_map = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could add a test to ensure that this map has the same keys as field_builder.FormFieldFactory.field_map

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test added in 87d1cac.

@wo0dyn wo0dyn force-pushed the 170_form_class_from_json branch from dc6bc26 to 87d1cac Compare April 11, 2017 03:57
@wo0dyn
Copy link
Contributor

wo0dyn commented Apr 11, 2017

Ready for review once again.

Copy link
Contributor

@mgu mgu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, nice work !

@wo0dyn wo0dyn merged commit 3e7d7aa into master Apr 11, 2017
@wo0dyn wo0dyn deleted the 170_form_class_from_json branch April 11, 2017 08:57
wo0dyn added a commit that referenced this pull request Apr 11, 2017
ChangeLog
---------

* Added Django 1.10 support (#203).
* Dropped Python 3.3 support (#207).
* Fixed the swagger doc generation and rendering (#210).
* Fix wrong field type for Checkbox (#208).
* Don't rely on database ordering in `NestedListSerializer` (#215)
* Provide a tools in order to generate django-form class from json
  contextualized definition (#171)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants