Skip to content

Commit

Permalink
Merge pull request #9 from djk2/issue_5
Browse files Browse the repository at this point in the history
Issue 5
  • Loading branch information
djk2 authored Oct 4, 2018
2 parents 1df331f + 9b4742d commit 5043747
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG for django-popup-view-field

## 0.4.0 (2018-10-04)

* Added `callback_data` attribute to PopupViewField (read more in README)

## 0.3.0 (2017-03-07)

* Support for Django 1.11
Expand Down
43 changes: 41 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,49 @@ Create typical FormView
</form>


callback_data attribute
------------------------
If you want pass extra parameters to your popup view, you should use `callback_data`
attribute for PopupViewField. This argument should be dictionary or OrderedDict.
This dictionary containing yours parameters will be encoded to ASCII text string and
added to url address. In your popup view You can take this parameters from `request.GET`.

*popups.py* ::

from django.views.generic import View
from django_popup_view_field.registry import registry_popup_view

class FooPopupView(View):
def get(self, request):
print(request.GET['extra_param']) # --> will be "Foo Bar"
print(request.GET['my_pk']) # --> will be 666
....

# REGISTER IS IMPORTANT
registry_popup_view.register(FooPopupView)

*forms.py* ::

from django import forms
from django_popup_view_field.fields import PopupViewField

class FooForm(forms.Form):

some_field = PopupViewField(
view_class=FooPopupView,
callback_data={
'extra_param': 'Foo Bar',
'my_pk': 666
}
)



Advanced Example
------------------------

Advanced Example use django-bootstrap3. Dialog is interactive, all links and forms will be send via Ajax and response will be loaded in dialog.
Advanced Example use django-bootstrap3.
Dialog is interactive, all links and forms will be send via Ajax and response will be loaded in dialog.

.. image:: https://raw.githubusercontent.com/djk2/django-popup-view-field/master/doc/static/advanced_example.png
:alt: Advanced Example - screenshot
Expand Down Expand Up @@ -371,7 +410,7 @@ PopupView
Reverse order
</a>

*popups.py* ::
**popups.py* ::

from django.views.generic import TemplateView
from django_popup_view_field.registry import registry_popup_view
Expand Down
2 changes: 1 addition & 1 deletion demo/demo/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.http import HttpResponse
from django.views.generic import TemplateView
from django.views.generic import CreateView, TemplateView

from .forms import DemoForm

Expand Down
2 changes: 1 addition & 1 deletion django_popup_view_field/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
VERSION = (0, 3, 0)
VERSION = (0, 4, 0)
__version__ = ".".join(str(i) for i in VERSION)
13 changes: 12 additions & 1 deletion django_popup_view_field/fields.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import urllib

from django.forms.fields import CharField
from django.utils.translation import ugettext_lazy as _
from django.views.generic import View
Expand All @@ -21,10 +23,19 @@ def __init__(self, view_class, *args, **kwargs):
view_class_name = view_class.__name__
popup_dialog_title = kwargs.pop("popup_dialog_title", _("Popup Dialog: Select value"))

callback_data = kwargs.pop("callback_data", {})
if not isinstance(callback_data, dict):
raise AttributeError("callback_data argument must be a dictionary")
try:
callback_data = urllib.urlencode(callback_data)
except AttributeError:
callback_data = urllib.parse.urlencode(callback_data)

super(PopupViewField, self).__init__(
widget=PopupViewWidget(
view_class_name=view_class_name,
popup_dialog_title=popup_dialog_title
popup_dialog_title=popup_dialog_title,
callback_data=callback_data
),
*args,
**kwargs
Expand Down
35 changes: 34 additions & 1 deletion django_popup_view_field/tests/test_field.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# encoding: utf-8
from collections import OrderedDict

from django import VERSION, forms
from django.forms.fields import CharField
from django.test import TestCase
Expand Down Expand Up @@ -50,7 +52,7 @@ class Form(forms.Form):
assert html.find('''class="input-group-addon btn popup-view-btn-load"''') != -1
assert html.find('''data-target="id_popup_view_field"''') != -1
assert html.find('''data-popup-dialog-title="Popup Dialog: Select value"''') != -1
assert html.find('''data-url = "/django_popup_view_field/PopupView/"''') != -1
assert html.find('''data-url = "/django_popup_view_field/PopupView/?"''') != -1

class Form(forms.Form):
popup_view_field = PopupViewField(
Expand All @@ -63,3 +65,34 @@ class Form(forms.Form):
html = form.as_p()
assert html.find('''data-popup-dialog-title="Foo Bar Title Window"''') != -1
self.assertInHTML('''<span class="helptext">Test help text</span>''', html)

def test_callback_data_type_error(self):

class PopupView(View):
pass

with self.assertRaises(AttributeError):
PopupViewField(view_class=PopupView, callback_data="wrong type")

def test_callback_data(self):

class PopupView(View):
pass

class Form(forms.Form):
popup_view_field = PopupViewField(
view_class=PopupView,
callback_data=OrderedDict([
('pk', 1),
('name', "Some name"),
('utf', "ąść"),
('escape', '&?')
])
)

form = Form()
html = form.as_p()
assert (
'"/django_popup_view_field/PopupView/'
'?pk=1&amp;name=Some+name&amp;utf=%C4%85%C5%9B%C4%87&amp;escape=%26%3F"'
) in html
2 changes: 1 addition & 1 deletion django_popup_view_field/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_get_response(self):
assert html.find('''class="input-group-addon btn popup-view-btn-load"''') != -1
assert html.find('''data-target="id_field"''') != -1
assert html.find('''data-popup-dialog-title="Test PopupView1 Title"''') != -1
assert html.find('''data-url = "/django_popup_view_field/PopupView1/"''') != -1
assert html.find('''data-url = "/django_popup_view_field/PopupView1/?"''') != -1

def test_post_response(self):
response = self.client.post(self.url, {"field": "Test Value"})
Expand Down
2 changes: 1 addition & 1 deletion django_popup_view_field/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ def get(self, request, *args, **kwargs):
raise ValueError("view_class_name must be pass")
else:
view_class = registry_popup_view.get(view_class_name)
return view_class.as_view()(request=request, **kwargs)
return view_class.as_view()(request=request, *args, **kwargs)
8 changes: 6 additions & 2 deletions django_popup_view_field/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ class PopupViewWidget(forms.TextInput):

template_name = 'django_popup_view_field/popup_view_widget.html'

def __init__(self, view_class_name, popup_dialog_title, attrs=None):
def __init__(self, view_class_name, popup_dialog_title, callback_data, attrs=None):
self.view_class_name = view_class_name
self.popup_dialog_title = popup_dialog_title
self.callback_data = callback_data

# compability for Django v1.11
if attrs is not None:
Expand All @@ -31,7 +32,10 @@ def __init__(self, view_class_name, popup_dialog_title, attrs=None):
def get_view_url(self):
"""Return url for ajax to view for render dialog content"""
url = reverse("django_popup_view_field:get_popup_view", args=(self.view_class_name,))
return url
return "{url}?{cd}".format(
url=url,
cd=self.callback_data
)

def get_context(self, name, value, attrs=None):

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# encoding:utf-8
from setuptools import setup, find_packages
from setuptools import find_packages, setup

from django_popup_view_field import __version__ as version


Expand Down
4 changes: 1 addition & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ envlist=
{py27,py34}-dj_1.9-bootstrap_{7.1,8.1,8.2}-crispy_{1.5,1.6}-tests,
{py27,py34}-dj_1.10-bootstrap_{7.1,8.1,8.2}-crispy_{1.5,1.6}-tests,
{py27,py34}-dj_1.11-bootstrap_{8.2}-crispy_{1.6}-tests,
{py27,py34}-flake,
py34-isort
{py27,py34}-flake

[testenv]
basepython =
Expand All @@ -17,7 +16,6 @@ pip_pre = true

deps =
flake8
isort
dj_1.8: Django>=1.8,<1.9
dj_1.9: Django>=1.9,<1.10
dj_1.10: Django>=1.9,<1.11
Expand Down

0 comments on commit 5043747

Please sign in to comment.