From 4d59604bf9cfa5a68f778bbe8d971e39e1320db2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20T=C4=99=C5=BCycki?= Date: Wed, 26 Sep 2018 23:51:43 +0200 Subject: [PATCH 1/2] Demo for issue #5 --- demo/db.sqlite3 | Bin 48128 -> 53248 bytes demo/demo/forms.py | 19 ++++++++- demo/demo/migrations/0002_ap_apreview.py | 36 ++++++++++++++++++ demo/demo/models.py | 19 +++++++++ demo/demo/popups.py | 25 ++++++++++-- demo/demo/templates/demo/APReview_new.html | 17 +++++++++ .../templates/popups/selectionReason.html | 5 +++ demo/demo/urls.py | 3 +- demo/demo/views.py | 10 ++++- django_popup_view_field/fields.py | 12 +++++- django_popup_view_field/widgets.py | 8 +++- 11 files changed, 144 insertions(+), 10 deletions(-) create mode 100644 demo/demo/migrations/0002_ap_apreview.py create mode 100644 demo/demo/templates/demo/APReview_new.html create mode 100644 demo/demo/templates/popups/selectionReason.html diff --git a/demo/db.sqlite3 b/demo/db.sqlite3 index 6161ea94be10e3469d0db622e208e3a50a850d76..076a37ba61c0f9dd05054063270a8ee65fa6f29d 100644 GIT binary patch delta 1482 zcmZuxU1%d!6uviSnn}`V8n?~gWSiS;Lmd0FGf97J?LyNwEV893wSu|}VcMB(N7_k9 zrfL?t?Q}txu7YI{qPqztZF#1VV-T`Nt;ynrTcL;$Gh+tsxbxK?M#;+ zTVAB1+s|~JZ>;V7UFHqnZomy!5j=t);TxLTUD$;RtiaW(k%4ZHn?YTEo8IA~>+|;7 zrkj;>cYwnWO}4Yo>zHv2Izob!cG*N53Uo4<$83YXT-ea`Qtj2iJ`Wr$ne1er z7q)3dbXxdTSgIPm(Ax$WySciI)MeO`u25YD>oQP#bNc24y-0%45iUDYFoB5q!`tuG zj<#QBDUJ|^6DrXVoYef$KOu?I;U}o>M7;>B&4j`& zF-sORQ?pqTZ`w0|P7FU&l5|yDi4#@Rl@+B#=5mWJeiAR!n_AXsV=X9BxuBVk zHaSM0mvnP75ua*!ij$|~svPI}9se*JJ>fVfI93%)d4}n9RH?Q|A${ ztdy5aYS9WQwgRbEAWeZ_8&3=Q#h&rmmcfp)!7E8@@Tw*&Z!{e=FIj2lRXHyY4NKS4 zlF+oBYGn8P+)VbRmKyz+8Yj8A79&~&fBbQbI6pe%3|>iCcUb%NfFmBO0WcgRIHo=E z5iHDO7RACMpD7pC6{1P&idBKU_`(LAWwNST(kcZJY2nu@dWEQEQkLYIv+|I%glfB))^)cE+3urtLZbLZS)0n4eNh5SXh*ac56z>~u9WBei9v5mu z>`vl?wrR_}S*A90W_lq&O%oUC5OUV|+WfPi4TrVwZjXY?0S^%T0uN|4K8J}YN13tY R*?*xalW4@=Jx!r0_&@0Is!ad@ delta 373 zcmXv|F-SsT82o(q^bZxS^rcjkLJp~6ROC`oG&HmZMTb`e~zrzh1?Q$j#RF#M&WHXZFn~TLaQv8fLwR16wP9u_pH#Ct%10|GHc@;cqaZfU< z1%n;Y?ACfDn~A6KNZtQyf9fzHVED~9;uz!`PKe5AKEXQ!V@OfqL1(Ny0zDnKF<#Zn`gHG diff --git a/demo/demo/forms.py b/demo/demo/forms.py index 51e5796..6228faf 100644 --- a/demo/demo/forms.py +++ b/demo/demo/forms.py @@ -3,7 +3,8 @@ from django_popup_view_field.fields import PopupViewField -from .popups import ColorPopupView, CountryPopupView, SexPopupView +from .popups import ColorPopupView, CountryPopupView, SexPopupView, SelectionReasonPopUpViews +from .models import APReview class DemoForm(forms.Form): @@ -25,3 +26,19 @@ class DemoForm(forms.Form): def __init__(self, *args, **kwargs): super(DemoForm, self).__init__(*args, **kwargs) self.helper = FormHelper() + +class APReviewForm(forms.ModelForm): + class Meta: + model=APReview + fields=['rating', 'selectionReason', ] + + def __init__(self, *args, **kwargs): + super(APReviewForm, self).__init__(*args, **kwargs) + self.helper = FormHelper() + self.fields['selectionReason']=PopupViewField( + view_class=SelectionReasonPopUpViews, + popup_dialog_title="Please select a reason for", + callback_data={'my_id':'APid_1', 'other_param': 'xxx'}, + required=True, + + ) diff --git a/demo/demo/migrations/0002_ap_apreview.py b/demo/demo/migrations/0002_ap_apreview.py new file mode 100644 index 0000000..8743a47 --- /dev/null +++ b/demo/demo/migrations/0002_ap_apreview.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11 on 2018-09-26 20:38 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('demo', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='AP', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=200)), + ('APid', models.CharField(max_length=200)), + ('description1', models.CharField(max_length=200)), + ('description2', models.CharField(max_length=200)), + ('description3', models.CharField(max_length=200)), + ], + ), + migrations.CreateModel( + name='APReview', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('rating', models.IntegerField(choices=[(1, 1), (2, 2)])), + ('selectionReason', models.CharField(max_length=200)), + ('atPat', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='demo.AP')), + ], + ), + ] diff --git a/demo/demo/models.py b/demo/demo/models.py index 00008d4..7907bbc 100644 --- a/demo/demo/models.py +++ b/demo/demo/models.py @@ -4,3 +4,22 @@ class Country(models.Model): code = models.CharField(max_length=2, primary_key=True) name = models.CharField(max_length=256) + + +class AP(models.Model): + name=models.CharField(max_length=200) + APid=models.CharField(max_length=200) + description1= models.CharField(max_length=200) + description2= models.CharField(max_length=200) + description3= models.CharField(max_length=200) + + +RATING_CHOICES = ( + (1, 1), + (2, 2) +) + +class APReview(models.Model): + atPat=models.ForeignKey(AP, on_delete=models.CASCADE) + rating = models.IntegerField(choices=RATING_CHOICES) + selectionReason=models.CharField(max_length=200) diff --git a/demo/demo/popups.py b/demo/demo/popups.py index 38aaf72..b413a0a 100644 --- a/demo/demo/popups.py +++ b/demo/demo/popups.py @@ -2,11 +2,11 @@ from django import forms from django.http import HttpResponse -from django.views.generic import FormView, TemplateView - +from django.views.generic import FormView, TemplateView, DetailView +from django.shortcuts import get_object_or_404 from django_popup_view_field.registry import registry_popup_view -from .models import Country +from .models import Country, AP class SexPopupView(TemplateView): @@ -41,7 +41,26 @@ def get_context_data(self, **kwargs): return context +class SelectionReasonPopUpViews(DetailView): + template_name = 'popups/selectionReason.html' + model = AP + context_object_name = 'attackpattern' + + def get(self, request, *args, **kwargs): + return super(SelectionReasonPopUpViews, self).get(request, *args, **kwargs) + + def get_object(self): + #I would like to pass the APiD of AP to myID + #This should just get a specific AP based on the myID value + my_id = self.request.GET.get("my_id") + other_param = self.request.GET.get("other_param") + obj = get_object_or_404(AP, APid=my_id) + return obj + + + # Register popup views registry_popup_view.register(SexPopupView) registry_popup_view.register(ColorPopupView) registry_popup_view.register(CountryPopupView) +registry_popup_view.register(SelectionReasonPopUpViews) diff --git a/demo/demo/templates/demo/APReview_new.html b/demo/demo/templates/demo/APReview_new.html new file mode 100644 index 0000000..d289a0b --- /dev/null +++ b/demo/demo/templates/demo/APReview_new.html @@ -0,0 +1,17 @@ +{% extends "base.html" %} +{% load bootstrap3 %} +{% load crispy_forms_tags %} +{% load django_popup_view_field_tags %} + +{% block content %} +
+ {% for hidden_field in form.hidden_fields %} + {% endfor %} + + {% csrf_token %} + {{ form.management_form }} + {{ form.non_form_errors }} + {{form.as_p}} + +
+{% endblock content %} diff --git a/demo/demo/templates/popups/selectionReason.html b/demo/demo/templates/popups/selectionReason.html new file mode 100644 index 0000000..1883f56 --- /dev/null +++ b/demo/demo/templates/popups/selectionReason.html @@ -0,0 +1,5 @@ +
    +
  • {{object.description1}}
  • +
  • {{object.description2}}
  • +
  • {{object.description3}}
  • +
diff --git a/demo/demo/urls.py b/demo/demo/urls.py index 4853b95..642f6c3 100644 --- a/demo/demo/urls.py +++ b/demo/demo/urls.py @@ -1,11 +1,12 @@ from django.conf.urls import include, url from django.contrib import admin -from .views import Index +from .views import Index, APReviewCreateView urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^$', Index.as_view(), name="index"), + url(r'^ap/$', APReviewCreateView.as_view(), name="ap"), # django-popup-view-field url(r'^django_popup_view_field/', include('django_popup_view_field.urls')), diff --git a/demo/demo/views.py b/demo/demo/views.py index 08b46b7..ca5ed65 100644 --- a/demo/demo/views.py +++ b/demo/demo/views.py @@ -1,7 +1,7 @@ from django.http import HttpResponse -from django.views.generic import TemplateView +from django.views.generic import TemplateView, CreateView -from .forms import DemoForm +from .forms import DemoForm, APReviewForm class Index(TemplateView): @@ -35,3 +35,9 @@ def get_context_data(self, **kwargs): context["form_2"] = self.form_2 context["form_3"] = self.form_3 return context + + +class APReviewCreateView(CreateView): + form_class = APReviewForm + template_name = 'demo/APReview_new.html' + success_url = 'success' diff --git a/django_popup_view_field/fields.py b/django_popup_view_field/fields.py index ac42374..a263fa7 100644 --- a/django_popup_view_field/fields.py +++ b/django_popup_view_field/fields.py @@ -4,6 +4,7 @@ from .exceptions import PopupViewIsNotSubclassView from .widgets import PopupViewWidget +import urllib class PopupViewField(CharField): @@ -21,10 +22,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 diff --git a/django_popup_view_field/widgets.py b/django_popup_view_field/widgets.py index 25deab7..7a44d04 100644 --- a/django_popup_view_field/widgets.py +++ b/django_popup_view_field/widgets.py @@ -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: @@ -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): From 9b4742dd04491589c24f6b2fd7050014e9d29772 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20T=C4=99=C5=BCycki?= Date: Thu, 4 Oct 2018 19:22:00 +0200 Subject: [PATCH 2/2] Close issue #5 --- CHANGELOG.md | 4 ++ README.rst | 43 +++++++++++++++++- demo/db.sqlite3 | Bin 53248 -> 48128 bytes demo/demo/forms.py | 19 +------- demo/demo/migrations/0002_ap_apreview.py | 36 --------------- demo/demo/models.py | 19 -------- demo/demo/popups.py | 25 ++-------- demo/demo/templates/demo/APReview_new.html | 17 ------- .../templates/popups/selectionReason.html | 5 -- demo/demo/urls.py | 3 +- demo/demo/views.py | 10 +--- django_popup_view_field/__init__.py | 2 +- django_popup_view_field/fields.py | 3 +- django_popup_view_field/tests/test_field.py | 35 +++++++++++++- django_popup_view_field/tests/test_views.py | 2 +- django_popup_view_field/views.py | 2 +- setup.py | 3 +- tox.ini | 5 +- 18 files changed, 94 insertions(+), 139 deletions(-) delete mode 100644 demo/demo/migrations/0002_ap_apreview.py delete mode 100644 demo/demo/templates/demo/APReview_new.html delete mode 100644 demo/demo/templates/popups/selectionReason.html diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f6bc93..91d5ee5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.rst b/README.rst index 129fc46..ed406f4 100644 --- a/README.rst +++ b/README.rst @@ -340,10 +340,49 @@ Create typical FormView +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 @@ -371,7 +410,7 @@ PopupView Reverse order -*popups.py* :: +**popups.py* :: from django.views.generic import TemplateView from django_popup_view_field.registry import registry_popup_view diff --git a/demo/db.sqlite3 b/demo/db.sqlite3 index 076a37ba61c0f9dd05054063270a8ee65fa6f29d..6161ea94be10e3469d0db622e208e3a50a850d76 100644 GIT binary patch delta 373 zcmXv|F-SsT82o(q^bZxS^rcjkLJp~6ROC`oG&HmZMTb`e~zrzh1?Q$j#RF#M&WHXZFn~TLaQv8fLwR16wP9u_pH#Ct%10|GHc@;cqaZfU< z1%n;Y?ACfDn~A6KNZtQyf9fzHVED~9;uz!`PKe5AKEXQ!V@OfqL1(Ny0zDnKF<#Zn`gHG delta 1482 zcmZuxU1%d!6uviSnn}`V8n?~gWSiS;Lmd0FGf97J?LyNwEV893wSu|}VcMB(N7_k9 zrfL?t?Q}txu7YI{qPqztZF#1VV-T`Nt;ynrTcL;$Gh+tsxbxK?M#;+ zTVAB1+s|~JZ>;V7UFHqnZomy!5j=t);TxLTUD$;RtiaW(k%4ZHn?YTEo8IA~>+|;7 zrkj;>cYwnWO}4Yo>zHv2Izob!cG*N53Uo4<$83YXT-ea`Qtj2iJ`Wr$ne1er z7q)3dbXxdTSgIPm(Ax$WySciI)MeO`u25YD>oQP#bNc24y-0%45iUDYFoB5q!`tuG zj<#QBDUJ|^6DrXVoYef$KOu?I;U}o>M7;>B&4j`& zF-sORQ?pqTZ`w0|P7FU&l5|yDi4#@Rl@+B#=5mWJeiAR!n_AXsV=X9BxuBVk zHaSM0mvnP75ua*!ij$|~svPI}9se*JJ>fVfI93%)d4}n9RH?Q|A${ ztdy5aYS9WQwgRbEAWeZ_8&3=Q#h&rmmcfp)!7E8@@Tw*&Z!{e=FIj2lRXHyY4NKS4 zlF+oBYGn8P+)VbRmKyz+8Yj8A79&~&fBbQbI6pe%3|>iCcUb%NfFmBO0WcgRIHo=E z5iHDO7RACMpD7pC6{1P&idBKU_`(LAWwNST(kcZJY2nu@dWEQEQkLYIv+|I%glfB))^)cE+3urtLZbLZS)0n4eNh5SXh*ac56z>~u9WBei9v5mu z>`vl?wrR_}S*A90W_lq&O%oUC5OUV|+WfPi4TrVwZjXY?0S^%T0uN|4K8J}YN13tY R*?*xalW4@=Jx!r0_&@0Is!ad@ diff --git a/demo/demo/forms.py b/demo/demo/forms.py index 6228faf..51e5796 100644 --- a/demo/demo/forms.py +++ b/demo/demo/forms.py @@ -3,8 +3,7 @@ from django_popup_view_field.fields import PopupViewField -from .popups import ColorPopupView, CountryPopupView, SexPopupView, SelectionReasonPopUpViews -from .models import APReview +from .popups import ColorPopupView, CountryPopupView, SexPopupView class DemoForm(forms.Form): @@ -26,19 +25,3 @@ class DemoForm(forms.Form): def __init__(self, *args, **kwargs): super(DemoForm, self).__init__(*args, **kwargs) self.helper = FormHelper() - -class APReviewForm(forms.ModelForm): - class Meta: - model=APReview - fields=['rating', 'selectionReason', ] - - def __init__(self, *args, **kwargs): - super(APReviewForm, self).__init__(*args, **kwargs) - self.helper = FormHelper() - self.fields['selectionReason']=PopupViewField( - view_class=SelectionReasonPopUpViews, - popup_dialog_title="Please select a reason for", - callback_data={'my_id':'APid_1', 'other_param': 'xxx'}, - required=True, - - ) diff --git a/demo/demo/migrations/0002_ap_apreview.py b/demo/demo/migrations/0002_ap_apreview.py deleted file mode 100644 index 8743a47..0000000 --- a/demo/demo/migrations/0002_ap_apreview.py +++ /dev/null @@ -1,36 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by Django 1.11 on 2018-09-26 20:38 -from __future__ import unicode_literals - -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - ('demo', '0001_initial'), - ] - - operations = [ - migrations.CreateModel( - name='AP', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('name', models.CharField(max_length=200)), - ('APid', models.CharField(max_length=200)), - ('description1', models.CharField(max_length=200)), - ('description2', models.CharField(max_length=200)), - ('description3', models.CharField(max_length=200)), - ], - ), - migrations.CreateModel( - name='APReview', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('rating', models.IntegerField(choices=[(1, 1), (2, 2)])), - ('selectionReason', models.CharField(max_length=200)), - ('atPat', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='demo.AP')), - ], - ), - ] diff --git a/demo/demo/models.py b/demo/demo/models.py index 7907bbc..00008d4 100644 --- a/demo/demo/models.py +++ b/demo/demo/models.py @@ -4,22 +4,3 @@ class Country(models.Model): code = models.CharField(max_length=2, primary_key=True) name = models.CharField(max_length=256) - - -class AP(models.Model): - name=models.CharField(max_length=200) - APid=models.CharField(max_length=200) - description1= models.CharField(max_length=200) - description2= models.CharField(max_length=200) - description3= models.CharField(max_length=200) - - -RATING_CHOICES = ( - (1, 1), - (2, 2) -) - -class APReview(models.Model): - atPat=models.ForeignKey(AP, on_delete=models.CASCADE) - rating = models.IntegerField(choices=RATING_CHOICES) - selectionReason=models.CharField(max_length=200) diff --git a/demo/demo/popups.py b/demo/demo/popups.py index b413a0a..38aaf72 100644 --- a/demo/demo/popups.py +++ b/demo/demo/popups.py @@ -2,11 +2,11 @@ from django import forms from django.http import HttpResponse -from django.views.generic import FormView, TemplateView, DetailView -from django.shortcuts import get_object_or_404 +from django.views.generic import FormView, TemplateView + from django_popup_view_field.registry import registry_popup_view -from .models import Country, AP +from .models import Country class SexPopupView(TemplateView): @@ -41,26 +41,7 @@ def get_context_data(self, **kwargs): return context -class SelectionReasonPopUpViews(DetailView): - template_name = 'popups/selectionReason.html' - model = AP - context_object_name = 'attackpattern' - - def get(self, request, *args, **kwargs): - return super(SelectionReasonPopUpViews, self).get(request, *args, **kwargs) - - def get_object(self): - #I would like to pass the APiD of AP to myID - #This should just get a specific AP based on the myID value - my_id = self.request.GET.get("my_id") - other_param = self.request.GET.get("other_param") - obj = get_object_or_404(AP, APid=my_id) - return obj - - - # Register popup views registry_popup_view.register(SexPopupView) registry_popup_view.register(ColorPopupView) registry_popup_view.register(CountryPopupView) -registry_popup_view.register(SelectionReasonPopUpViews) diff --git a/demo/demo/templates/demo/APReview_new.html b/demo/demo/templates/demo/APReview_new.html deleted file mode 100644 index d289a0b..0000000 --- a/demo/demo/templates/demo/APReview_new.html +++ /dev/null @@ -1,17 +0,0 @@ -{% extends "base.html" %} -{% load bootstrap3 %} -{% load crispy_forms_tags %} -{% load django_popup_view_field_tags %} - -{% block content %} -
- {% for hidden_field in form.hidden_fields %} - {% endfor %} - - {% csrf_token %} - {{ form.management_form }} - {{ form.non_form_errors }} - {{form.as_p}} - -
-{% endblock content %} diff --git a/demo/demo/templates/popups/selectionReason.html b/demo/demo/templates/popups/selectionReason.html deleted file mode 100644 index 1883f56..0000000 --- a/demo/demo/templates/popups/selectionReason.html +++ /dev/null @@ -1,5 +0,0 @@ -
    -
  • {{object.description1}}
  • -
  • {{object.description2}}
  • -
  • {{object.description3}}
  • -
diff --git a/demo/demo/urls.py b/demo/demo/urls.py index 642f6c3..4853b95 100644 --- a/demo/demo/urls.py +++ b/demo/demo/urls.py @@ -1,12 +1,11 @@ from django.conf.urls import include, url from django.contrib import admin -from .views import Index, APReviewCreateView +from .views import Index urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^$', Index.as_view(), name="index"), - url(r'^ap/$', APReviewCreateView.as_view(), name="ap"), # django-popup-view-field url(r'^django_popup_view_field/', include('django_popup_view_field.urls')), diff --git a/demo/demo/views.py b/demo/demo/views.py index ca5ed65..80427b4 100644 --- a/demo/demo/views.py +++ b/demo/demo/views.py @@ -1,7 +1,7 @@ from django.http import HttpResponse -from django.views.generic import TemplateView, CreateView +from django.views.generic import CreateView, TemplateView -from .forms import DemoForm, APReviewForm +from .forms import DemoForm class Index(TemplateView): @@ -35,9 +35,3 @@ def get_context_data(self, **kwargs): context["form_2"] = self.form_2 context["form_3"] = self.form_3 return context - - -class APReviewCreateView(CreateView): - form_class = APReviewForm - template_name = 'demo/APReview_new.html' - success_url = 'success' diff --git a/django_popup_view_field/__init__.py b/django_popup_view_field/__init__.py index 65a0704..07b714f 100644 --- a/django_popup_view_field/__init__.py +++ b/django_popup_view_field/__init__.py @@ -1,2 +1,2 @@ -VERSION = (0, 3, 0) +VERSION = (0, 4, 0) __version__ = ".".join(str(i) for i in VERSION) diff --git a/django_popup_view_field/fields.py b/django_popup_view_field/fields.py index a263fa7..76e35b6 100644 --- a/django_popup_view_field/fields.py +++ b/django_popup_view_field/fields.py @@ -1,10 +1,11 @@ +import urllib + from django.forms.fields import CharField from django.utils.translation import ugettext_lazy as _ from django.views.generic import View from .exceptions import PopupViewIsNotSubclassView from .widgets import PopupViewWidget -import urllib class PopupViewField(CharField): diff --git a/django_popup_view_field/tests/test_field.py b/django_popup_view_field/tests/test_field.py index 2701302..a73c4ad 100644 --- a/django_popup_view_field/tests/test_field.py +++ b/django_popup_view_field/tests/test_field.py @@ -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 @@ -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( @@ -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('''Test help text''', 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&name=Some+name&utf=%C4%85%C5%9B%C4%87&escape=%26%3F"' + ) in html diff --git a/django_popup_view_field/tests/test_views.py b/django_popup_view_field/tests/test_views.py index 260877f..1469a57 100644 --- a/django_popup_view_field/tests/test_views.py +++ b/django_popup_view_field/tests/test_views.py @@ -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"}) diff --git a/django_popup_view_field/views.py b/django_popup_view_field/views.py index 0574994..689c702 100644 --- a/django_popup_view_field/views.py +++ b/django_popup_view_field/views.py @@ -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) diff --git a/setup.py b/setup.py index 22f23e7..8a19ab6 100644 --- a/setup.py +++ b/setup.py @@ -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 diff --git a/tox.ini b/tox.ini index 1c789be..95a5d85 100644 --- a/tox.ini +++ b/tox.ini @@ -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 = @@ -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 @@ -31,4 +29,3 @@ deps = commands = tests: ./run_test.py flake: flake8 - isort: isort -rc -w80 -m3 -tc -c -df django_popup_view_field run_test.py