Permalink
Browse files

refactored template tags

  • Loading branch information...
1 parent d7a0368 commit d7f439b6048713d0a9459fc557e10d0dc34a8019 @fatiherikli fatiherikli committed Sep 25, 2012
Showing with 57 additions and 49 deletions.
  1. +50 −2 apiplayground/forms.py
  2. +2 −47 apiplayground/templatetags/api_browser_tags.py
  3. +5 −0 apiplayground/utils.py
View
@@ -1,14 +1,62 @@
from django import forms
+from django.utils.datastructures import SortedDict
from apiplayground.models import Feedback
+from apiplayground.utils import tokenize_url_parameters
class FeedbackForm(forms.ModelForm):
-
+ """
+ A form that updates and creates feedbacks.
+ """
title = forms.CharField(widget=forms.TextInput(attrs={
"required": "required"
}))
class Meta:
model = Feedback
- exclude = ("duplicate", "status")
+ exclude = ("duplicate", "status")
+
+TYPE_WIDGET_MAPPING = {
+ "string": forms.TextInput,
+ "boolean": forms.CheckboxInput
+}
+
+def build_data_form(parameters):
+ """
+ Builds a form with given parameters as dynamically.
+ """
+ form_fields = SortedDict()
+ for parameter in parameters:
+ parameter_name = parameter.get("name")
+ parameter_type = parameter.get("type", "string") # default type is "string"
+ is_required = parameter.get("is_required", False)
+ form_widget = TYPE_WIDGET_MAPPING.get(parameter_type)
+
+ assert "name" in parameter, "Parameter name is required"
+ assert form_widget is not None, "Wrong field type."
+
+ widget = form_widget()
+ form_fields[parameter_name] = forms.CharField(
+ label=parameter_name, widget=widget)
+
+ if is_required:
+ widget.attrs["required"] = "required"
+
+ return type("DataParameterForm", (forms.Form,), form_fields)
+
+
+def build_url_form(url):
+ """
+ Builds a url parameter form from the given url.
+ """
+ form_fields = SortedDict()
+ url_parameters = tokenize_url_parameters(url)
+ for token, parameter in url_parameters:
+ form_fields["url-parameter-%s" % parameter] = forms.CharField(
+ label=parameter, widget=forms.TextInput(attrs={
+ "required": "required",
+ "data-token": token,
+ }))
+
+ return type("URLParameterForm", (forms.Form,), form_fields)
@@ -1,53 +1,8 @@
-from django.utils.datastructures import SortedDict
-import re
-
-from django import template, forms
+from django import template
+from apiplayground.forms import build_url_form, build_data_form
register = template.Library()
-TYPE_WIDGET_MAPPING = {
- "string": forms.TextInput,
- "boolean": forms.CheckboxInput
-}
-
-def tokenize_url_parameters(url):
- pattern = re.compile("(\{([a-z-]+)\})")
- return pattern.findall(url) or ()
-
-
-def build_data_form(parameters):
- form_fields = SortedDict()
- for parameter in parameters:
- parameter_name = parameter.get("name")
- parameter_type = parameter.get("type", "string") # default type is "string"
- is_required = parameter.get("is_required", False)
- form_widget = TYPE_WIDGET_MAPPING.get(parameter_type)
-
- assert "name" in parameter, "Parameter name is required"
- assert form_widget is not None, "Wrong field type."
-
- widget = form_widget()
- form_fields[parameter_name] = forms.CharField(
- label=parameter_name, widget=widget)
-
- if is_required:
- widget.attrs["required"] = "required"
-
- return type("DataParameterForm", (forms.Form,), form_fields)
-
-
-def build_url_form(url):
- form_fields = SortedDict()
- url_parameters = tokenize_url_parameters(url)
- for token, parameter in url_parameters:
- form_fields["url-parameter-%s" % parameter] = forms.CharField(
- label=parameter, widget=forms.TextInput(attrs={
- "required": "required",
- "data-token": token,
- }))
-
- return type("URLParameterForm", (forms.Form,), form_fields)
-
@register.assignment_tag()
def get_endpoint_forms(endpoint):
View
@@ -0,0 +1,5 @@
+import re
+
+def tokenize_url_parameters(url):
+ pattern = re.compile("(\{([a-z-]+)\})")
+ return pattern.findall(url) or ()

0 comments on commit d7f439b

Please sign in to comment.