django forms wizard and recaptcha - django

I did a recaptcha integration using the following django snippet
settings.py
RECAPTCHA_PUBLIC_KEY = '<your public key>'
RECAPTCHA_PRIVATE_KEY = '<your private key>'
#widgets.py
from django import forms
from django.utils.safestring import mark_safe
from django.conf import settings
from recaptcha import captcha
class ReCaptcha(forms.widgets.Widget):
recaptcha_challenge_name = 'recaptcha_challenge_field'
recaptcha_response_name = 'recaptcha_response_field'
def render(self, name, value, attrs=None):
return mark_safe(u'%s' % captcha.displayhtml(settings.RECAPTCHA_PUBLIC_KEY))
def value_from_datadict(self, data, files, name):
return [data.get(self.recaptcha_challenge_name, None),
data.get(self.recaptcha_response_name, None)]
#fields.py
from django.conf import settings
from django import forms
from django.utils.encoding import smart_unicode
from django.utils.translation import ugettext_lazy as _
from marcofucci_utils.widgets import ReCaptcha
from recaptcha import captcha
class ReCaptchaField(forms.CharField):
default_error_messages = {
'captcha_invalid': _(u'Invalid captcha')
}
def __init__(self, *args, **kwargs):
self.widget = ReCaptcha
self.required = True
super(ReCaptchaField, self).__init__(*args, **kwargs)
def clean(self, values):
super(ReCaptchaField, self).clean(values[1])
recaptcha_challenge_value = smart_unicode(values[0])
recaptcha_response_value = smart_unicode(values[1])
check_captcha = captcha.submit(recaptcha_challenge_value,
recaptcha_response_value, settings.RECAPTCHA_PRIVATE_KEY, {})
if not check_captcha.is_valid:
raise forms.util.ValidationError(self.error_messages['captcha_invalid'])
return values[0]
#forms.py
class RegistrationForm(forms.Form):
...
recaptcha = marcofucci_fields.ReCaptchaField()
...
But I have the forms defined in the django forms wizard and it calls the clean method on the field twice, even if the captcha is included in the last form.
As in the following:
from registration.forms import RegistrationWizard,RegistrationForm,ProfileForm
url(r'^register/$',
RegistrationWizard([RegistrationForm,ProfileForm]),
name='register_wizard'),
How do I circumvent this situation. What is the need to call the clean on the last form twice?

If a form is bound, it will allways call clean when renderering (even if you don't call form.is_valid).

You might want to consider using this instead:
django snippet 1644

Related

How to set a default handler in django fobi

Did anyone managed to set a default handler in django-fobi? What was the approach? I'd like to set the db_store handler as a default for all of the forms.
I've tried to set defaults in models but with no success.
There's no out-of-box solution for that.
However, you could do as follows:
Solution 1:
Using django signals, watch updates of the FormEntry model and add the db_handler plugin programmatically each time a form is saved (it it doesn't yet have it assigned).
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
#receiver(post_save)
def update_document(sender, **kwargs):
app_label = sender._meta.app_label
model_name = sender._meta.model_name.lower()
instance = kwargs['instance']
if app_label == 'fobi' and model_name == 'formentry':
from fobi.models import FormHandlerEntry
FormHandlerEntry.objects.get_or_create(
plugin_uid='db_store',
form_entry=instance
)
Solution 2:
You could also register a form callback (fobi has callbacks implemented for almost each stage of form submission process).
In your callback you would have to mimic the functionality of the db_store plugin (copy-paste mainly).
import datetime
import simplejson as json
from fobi.base import (
form_callback_registry,
FormCallback,
get_processed_form_data,
)
from fobi.constants import CALLBACK_FORM_VALID
from fobi.contrib.plugins.form_handlers.db_store.models import SavedFormDataEntry
class AutoDbStore(FormCallback):
stage = CALLBACK_FORM_VALID
def callback(self, form_entry, request, form):
form_element_entries = form_entry.formelemententry_set.all()
# Clean up the values, leave our content fields and empty values.
field_name_to_label_map, cleaned_data = get_processed_form_data(
form,
form_element_entries
)
for key, value in cleaned_data.items():
if isinstance(value, (datetime.datetime, datetime.date)):
cleaned_data[key] = value.isoformat() \
if hasattr(value, 'isoformat') \
else value
saved_form_data_entry = SavedFormDataEntry(
form_entry=form_entry,
user=request.user if request.user and request.user.pk else None,
form_data_headers=json.dumps(field_name_to_label_map),
saved_data=json.dumps(cleaned_data)
)
saved_form_data_entry.save()
form_callback_registry.register(AutoDbStore)

Password field for pyforms?

My UI is written in pyforms.
How can I implement a password field? (EG. instead of 'P#ssW0rd' it would display '********').
I have found that I can utilize QLineEdit.EchoMode, but unsure how to implement.
Thanks in advance!
Updated to reflect community guidelines
Pyforms also includes a password box. You can also use
self._password = ControlPassword('Password')
So simply:
import pyforms
from pyforms.basewidget import BaseWidget
from pyforms.controls import ControlText
from pyforms.controls import ControlButton
from pyforms.controls import ControlPassword
class Login(BaseWidget):
def __init__(self):
super(Login,self).__init__('Simple example 1')
#Definition of the forms fields
self._username = ControlText('Username', 'Default value')
self._password = ControlPassword('Password')
self._button = ControlButton('Login')
self._button.value = self.__buttonAction #Define button action
def __buttonAction(self):
"""Button action event"""
username = self._username.value
password = self._password.value
credentials = (username, password)
return credentials
#Execute the application
if __name__ == "__main__":
pyforms.start_app( Login )
You can add the following module as ControlPasswordText.py in your project folder:
from pysettings import conf
from pyforms.Controls import ControlText
from PyQt4.QtGui import QLineEdit
class ControlPasswordText(ControlText):
def __init__(self, *args, **kwargs):
super(ControlPasswordText, self).__init__(*args, **kwargs)
self.form.lineEdit.setEchoMode(QLineEdit.Password)
And here's how you would use it:
import pyforms
from pyforms import BaseWidget
from pyforms.Controls import ControlText
from pyforms.Controls import ControlButton
# Importing the module here
from ControlPasswordText import ControlPasswordText
class SimpleExample1(BaseWidget):
def __init__(self):
super(SimpleExample1,self).__init__('Simple example 1')
#Definition of the forms fields
self._username = ControlText('Username')
# Using the password class
self._password = ControlPasswordText('Password')
#Execute the application
if __name__ == "__main__": pyforms.startApp( SimpleExample1 )
Result:

Django how to call a function in another class from django admin section

hi recently I have implemented a temp django model and I want this to get populate when the user clicks it from django admin
this is the implementation in django admin
from django.contrib import admin
from polls.models import Poll
from polls.models import TempModel
from django.conf.urls import patterns
from django.http import HttpResponse
from test_data import TestData
from django.http import HttpResponse
from django.template import RequestContext, loader
from test_data import TestData
class TempModelAdmin(admin.ModelAdmin):
fields = ('permalink', )
def test(self):
x = TestData.get_test_data()
admin.site.register(Poll)
admin.site.register(TempModel, TempModelAdmin)
and this is the temporary class which I used to populate data
from models import TempModel
class TestData(object):
#classmethod
def get_test_data(self):
print "**********************************************"
print "get test data"
print "**********************************************"
list = []
for x in range(0,50):
str_val = str(x) + "djdj;djfhdfjiosdifj";
list.append(str_val)
temp_model = TempModel()
temp_model.permalink = str_val
temp_model.save()
print "=============================================="
print "Object Count"
print TempModel.objects.count()
print "=============================================="
return list
this is not getting called . Can anyone know the reason for this ?
Thank you in advace
You're making a class method with self, it takes an instance of type. Try changing method signature:
def get_test_data(cls):
I figured it out by doing this approach and it worked
class TempModelAdmin(admin.ModelAdmin):
fields = ('permalink', )
def __init__(self, *args, **kwargs):
super(TempModelAdmin, self).__init__(*args, **kwargs)
self.my_method()
def my_method(self):
print "*************----------------------------"
TestData.get_test_data()

Django: change admin FileField output?

I've made a model with file that is uploaded to custom path (not in MEDIA_ROOT). So it's some kind like protected file.
Now I need to change it's representation in admin details. It shows a path relative to MEDIA_URL. I need to change that, to show a URL to an application view which generates a proper URL.
So, what is the best way to display link, and only in objects details in admin?
Here is the way I did it:
models.py
class SecureFile(models.Model):
upload_storage = FileSystemStorage(
location=settings.ABS_DIR('secure_file/files/'))
secure_file = models.FileField(verbose_name=_(u'file'),
upload_to='images', storage=upload_storage)
widgets.py
from django import forms
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe
class AdminFileWidget(forms.FileInput):
"""A FileField Widget that shows secure file link"""
def __init__(self, attrs={}):
super(AdminFileWidget, self).__init__(attrs)
def render(self, name, value, attrs=None):
output = []
if value and hasattr(value, "url"):
url = reverse('secure_file:get_secure_file',
args=(value.instance.slug, ))
out = u'{}<br />{} '
output.append(out.format(url, _(u'Download'), _(u'Change:')))
output.append(super(AdminFileWidget, self).render(name, value, attrs))
return mark_safe(u''.join(output))
admin.py
class SecureFileAdminForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(SecureFileAdminForm, self).__init__(*args, **kwargs)
self.fields['secure_file'].widget = AdminFileWidget()
class Meta:
model = SecureFile
class SecureFileAdmin(admin.ModelAdmin):
form = SecureFileAdminForm

ImportError for Django Piston handler that isn't tied to a model

I have created a custom handler (CustomHandler) that isn't tied to a model in the ORM and I think it's rigged up correctly, but I'm getting an ImportError: cannot import CustomHandler when trying to import it into my resources.py. Here is my setup:
custom_handlers.py:
from piston.handler import BaseHandler
class CustomHandler(BaseHandler):
allowed_methods = ('GET',)
def read(self, request):
return 'test'
resources.py:
from piston.resource import Resource
from piston.utils import rc
import simplejson as json
from api.authentication import DjangoAuthentication
from api.handlers import CustomHandler # ERROR THROWN HERE
auth = DjangoAuthentication(realm='...')
class JSONResource(Resource):
def determine_emitter(self, request, *args, **kwargs):
"""
Default to the json emitter.
"""
try:
return kwargs['emitter_format']
except KeyError:
pass
if 'format' in request.GET:
return request.GET.get('format')
return 'json'
def form_validation_response(self, e):
"""
Turns the error object into a serializable construct.
"""
resp = rc.BAD_REQUEST
json_errors = json.dumps(
dict(
(k, map(unicode, v))
for (k, v) in e.form.errors.iteritems()
)
)
resp.write(json_errors)
return resp
custom_handler = JSONResource(CustomHandler, authentication=auth)
urls.py:
from django.conf.urls.defaults import patterns, url
from api.resources import custom_handler
urlpatterns = patterns('',
url(r'^things/$', custom_handler),
)
UPDATE: I've tried manually compiling the pys into pycs with no luck. I also read this in Piston's docs:
When you create a handler which is tied to a model, Piston will
automatically register it (via a metaclass.)
But I can't find anything in the docs about creating a handler that isn't tied to a model, specifically how to register it.
Had to add from api.handlers.custom_handlers import CustomHandler to api/handlers/__init__.py