1. sammyrulez
  2. uiforms

Commits

sammyrulez  committed b6d4a8b

network is now ok

  • Participants
  • Parent commits a8511ae
  • Branches master

Comments (0)

Files changed (15)

File uiforms.rar

  • Ignore whitespace
Binary file added.

File uiforms/out.txt

View file
  • Ignore whitespace
+Creating test database for alias 'default'...
+is_it_cool?
+How_old_are_U?
+Destroying test database for alias 'default'...

File uiforms/settings.py

View file
  • Ignore whitespace
 
 DEBUG = True
 TEMPLATE_DEBUG = DEBUG
+import os
+SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
+
 
 ADMINS = (
     # ('Your Name', 'your_email@example.com'),
     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
     # Always use forward slashes, even on Windows.
     # Don't forget to use absolute paths, not relative paths.
+	os.path.join(SITE_ROOT, 'templates') , 
 )
 
 INSTALLED_APPS = (
     'django.contrib.admin',
     # Uncomment the next line to enable admin documentation:
     # 'django.contrib.admindocs',
+	'django_coverage',
+	'django_extensions',
 	'tmanager',
 )
 

File uiforms/templates/404.html

  • Ignore whitespace
Empty file added.

File uiforms/templates/admin/tmanager/preview.html

View file
  • Ignore whitespace
+{% extends 'admin/base_site.html' %}
+
+{% block content %}
+	<h2> Previw </h2> 
+	
+	{{ form.as_p }}
+	
+	{% for  field in form %}
+	<p>
+		<strong>  {{ field.label_tag }} : </strong> {{ field.data }}
+	</p>
+	{% endfor %}
+
+{% endblock %}

File uiforms/templates/admin/tmanager/share.html

View file
  • Ignore whitespace
+{% extends 'admin/base_site.html' %}
+
+{% block content %}
+	
+	<form method="POST">
+	{% csrf_token %}
+	{{ form.as_p }}
+	<p>
+		<input type="submit" value="Send" />
+	</p>
+	</form>
+
+{% endblock %}

File uiforms/templates/admin/tmanager/thanks.html

View file
  • Ignore whitespace
+{% extends 'admin/base_site.html' %}
+
+{% block content %}
+	<h1> Thank you </h1> 
+
+{% endblock %}

File uiforms/templates/admin/tmanager/uiform.html

View file
  • Ignore whitespace
+{% extends 'admin/base_site.html' %}

File uiforms/tmanager/admin.py

View file
  • Ignore whitespace
 from django.contrib import admin
 from models import FormTemplate , TemplateField ,TemplateData ,TemplateDataRow
+from django.core import urlresolvers
 
 class TemplateFieldInline(admin.TabularInline):
     model = TemplateField
+	
+	
+def share(obj):
+	return "<a href="%s">Share</a>" % urlresolvers.reverse('share',args=[obj.id])
+	
+share.allow_tags = True
+	
+def preview(obj):
+	return "<a href="%s">Preview</a>" % urlresolvers.reverse('preview',args=[obj.id])
+	
+preview.allow_tags = True
 
 class FormTemplateAdmin(admin.ModelAdmin):
     inlines = [
         TemplateFieldInline,
     ]
+	
+	list_display  = ['name','share','preview']
 
 class TemplateDataRowInline(admin.TabularInline):
     model = TemplateDataRow

File uiforms/tmanager/fixtures/test_data_fixtures.json

View file
  • Ignore whitespace
+[
+ {
+  "pk": 1, 
+  "model": "tmanager.formtemplate", 
+  "fields": {
+   "owner": 1, 
+   "name": "TestForm"
+  }
+ }, 
+ {
+  "pk": 1, 
+  "model": "tmanager.templatefield", 
+  "fields": {
+   "filed_type": "BOOLEAN", 
+   "template": 1, 
+   "label": "is_it_cool?"
+  }
+ }, 
+ {
+  "pk": 2, 
+  "model": "tmanager.templatefield", 
+  "fields": {
+   "filed_type": "INTEGER", 
+   "template": 1, 
+   "label": "How_old_are_U?"
+  }
+ }
+]

File uiforms/tmanager/forms.py

View file
  • Ignore whitespace
 	form_class_name = 	"UIForm%s" % ( str(template),)
 	UIForm = type(form_class_name, (forms.Form , ),fieldz)
 	return UIForm
+	
+class EmailForm(forms.Form):
+	email = fields.EmailField()

File uiforms/tmanager/models.py

View file
  • Ignore whitespace
 from django.db import models
 from django.contrib.auth.models import User
 from datetime import datetime
+from django.db.models import AutoField
+
+def copy_model_instance(obj):
+    initial = dict([(f.name, getattr(obj, f.name))
+                    for f in obj._meta.fields
+                    if not isinstance(f, AutoField) and not f in obj._meta.parents.values()])
+    return obj.__class__(**initial)
 
 class FormTemplate(models.Model):
 	name = models.CharField(max_length=100)

File uiforms/tmanager/tests.py

View file
  • Ignore whitespace
 from models import FormTemplate , TemplateField ,TemplateData
 from django.contrib.auth.models import User
 import forms
+from django.test.client import Client
+from django.core import urlresolvers
 
 
 
 		
 class TemplateDataTest(TestCase):
 	
-	fixtures = ['user_fixtures.json']
+	fixtures = ['user_fixtures.json' , 'test_data_fixtures.json']
 
 	def test_last_m(self):
 		td = TemplateData()
-		ft = FormTemplate()
-		ft.name = "test"
-		ft.owner = User.objects.get(pk=2)
-		ft.save()
+		ft = FormTemplate.objects.get(pk=1)
 		td.template_ref = ft
 		td.submitted_by = User.objects.get(pk=2)
 		td.save()
 		
 class FormBuilderTest(TestCase):
 	
-	fixtures = ['user_fixtures.json']
+	fixtures = ['user_fixtures.json', 'test_data_fixtures.json']
 	
 	def test_class_creation(self):
-		ft = FormTemplate()
-		ft.name = "Test"
-		ft.owner = User.objects.get(pk=2)
-		ft.save()
-		tf = TemplateField()
-		tf.label = "BooleanTestField"
-		tf.template = ft
-		tf.filed_type = 'BOOLEAN'
-		tf.save()
-		tf = TemplateField()
-		tf.label = "IntegerTestField"
-		tf.template = ft
-		tf.filed_type = 'INTEGER'
-		tf.save()
+		ft =  FormTemplate.objects.get(pk=1)
 		UIForm = forms.build_UIForm(ft)
 		instance = UIForm()
 		self.assertTrue(instance.as_p())
 		
+class CloneTest(TestCase):
+	fixtures = ['user_fixtures.json', 'test_data_fixtures.json']
 	
+	def test_clone_obj(self):
+		pass #TODO
+		
+class ViewTest(TestCase):
+
+	fixtures = ['user_fixtures.json', 'test_data_fixtures.json']
+	
+	def setUp(self):
+		self.client = Client()
+		self.client.login(username='admin', password='admin')
+		
+	def params(self,ft):
+		ok_params = {}
+		for f in ft.templatefield_set.all():
+			if f.filed_type == 'BOOLEAN':
+				ok_params[f.label] = 'True'
+			else:
+				ok_params[f.label] = 1
+		return ok_params
+		
+	def check_fieds_in_output(self, ft, content):
+		for f in ft.templatefield_set.all():
+			self.assertTrue(content.count(f.label) > 0 )
+
+		
+	def test_uiform(self):
+		ft =  FormTemplate.objects.get(pk=1)
+		original_data = ft.templatedata_set.count()
+		response = self.client.get(urlresolvers.reverse('uiform',args=[1]))
+		self.assertEquals(200,response.status_code)
+		self.check_fieds_in_output(ft,response.content)
+		
+		ok_params = self.params(ft)
+		post_response = self.client.post(urlresolvers.reverse('uiform',args=[1]),ok_params)
+		self.assertEquals(200,post_response.status_code)
+		#check output
+		self.assertTrue(post_response.content.count('Thank you') > 0 )
+		#check db
+		ft_after =  FormTemplate.objects.get(pk=1)
+		self.assertTrue( ft.templatedata_set.count() > original_data)
+		
+	def test_uiform_post_previw(self):
+		ft =  FormTemplate.objects.get(pk=1)
+		ok_params = self.params(ft)
+		response = self.client.post(urlresolvers.reverse('preview',args=[1]),ok_params)
+		self.assertEquals(200,response.status_code)
+		self.check_fieds_in_output(ft,response.content)
+		
+		
+	def test_uiform_get_previw(self):
+		ft =  FormTemplate.objects.get(pk=1)
+		response = self.client.get(urlresolvers.reverse('preview',args=[1]))
+		self.assertEquals(200,response.status_code)
+		#check output
+		self.check_fieds_in_output(ft,response.content)
+		
+	def test_clone(self):
+		original_data = FormTemplate.objects.count()
+		response = self.client.get(urlresolvers.reverse('clone',args=[1]))
+		self.assertEquals(404,response.status_code)
+		post_response = self.client.post(urlresolvers.reverse('clone',args=[1]), { 'honeypot':'dammy'})
+		self.assertEquals(302,post_response.status_code) #check redirect
+		self.assertTrue(FormTemplate.objects.count() > original_data) #check db
+		
+	def test_share(self):
+		response = self.client.get(urlresolvers.reverse('share',args=[1]))
+		self.assertEquals(200,response.status_code)
+		self.assertTrue(response.content.count("email") > 0 )
+		post_response = self.client.post(urlresolvers.reverse('share',args=[1]), { 'email':'dammy'})
+		self.assertEquals(200,response.status_code)
+		self.assertTrue(response.content.count("error") > 0 )
+		post_response = self.client.post(urlresolvers.reverse('share',args=[1]), { 'email':'your_email@example.com'})
+		self.assertEquals(302,post_response.status_code) #check redirect
+		
+			
+		
+		
 	

File uiforms/tmanager/views.py

View file
  • Ignore whitespace
-# Create your views here.
+from models import FormTemplate , TemplateField ,TemplateData ,TemplateDataRow ,copy_model_instance
+from django.contrib.auth.models import User
+import forms
+from django.shortcuts import get_object_or_404 ,redirect ,render_to_response
+from django.http import Http404
+import random
+
+
+
+def uiform(request, template_id):
+
+	template = get_object_or_404(FormTemplate ,pk = template_id)
+	UIForm = forms.build_UIForm(template)
+	if request.POST:
+		form_instance = UIForm(request.POST)
+		if form_instance.is_valid():
+			data = TemplateData()
+			data.template_ref = template
+			data.submitted_by = request.user
+			data.save()
+			for f in template.templatefield_set.all():
+				data_row =  TemplateDataRow()
+				data_row.template_data = data
+				data_row.template_field = f
+				data_row.payload = form_instance.cleaned_data[f.label]
+				data_row.save()
+			return render_to_response('admin/tmanager/thanks.html',{'form':form_instance, 'template_obj':template}) 
+			
+	else:
+		form_instance = UIForm()
+		
+	return render_to_response('admin/tmanager/uiform.html',{'form':form_instance, 'template_obj':template})
+	
+def uiform_preview(request, template_id):
+	template = get_object_or_404(FormTemplate ,pk = template_id)
+	UIForm = forms.build_UIForm(template)
+	if request.POST:
+		form_instance = UIForm(request.POST)
+	else:
+		idata = {}
+		for f in template.templatefield_set.all():
+			if f.filed_type == 'BOOLEAN':
+				idata[f.label] = random.choice([True, False])
+			else:
+				idata[f.label] = random.randrange(0,10)
+		
+		form_instance = UIForm(idata)
+		
+	return render_to_response('admin/tmanager/preview.html',{'form':form_instance, 'template_obj':template}) 
+	
+def clone_template(request, template_id):
+	if request.POST:
+		template = get_object_or_404(FormTemplate ,pk = template_id)
+		new_template = copy_model_instance(template)
+		new_template.owner = request.user
+		new_template.save()
+		for f in template.templatefield_set.all():
+			new_f = copy_model_instance(f)
+			new_f.template = new_template
+			new_f.save()
+		return redirect('admin:tmanager_formtemplate_changelist')
+	else:
+		raise Http404
+		
+
+def share_template(request, template_id):
+	if request.POST:
+		email_form = forms.EmailForm(request.POST)
+		if email_form.is_valid():
+			#send mail
+			return redirect('admin:tmanager_formtemplate_changelist')
+	else:
+		email_form = forms.EmailForm()
+		
+	return render_to_response('admin/tmanager/share.html',{'form':email_form}) 
+	
+	
+	
+
+	

File uiforms/urls.py

View file
  • Ignore whitespace
 
     # Uncomment the next line to enable the admin:
     url(r'^admin/', include(admin.site.urls)),
+	#uiform -get mostra post salva
+	url(r'^admin/uiform/(?P<template_id>\d+)/$','uiforms.tmanager.views.uiform',name='uiform'),
+	url(r'^admin/uiform_preview/(?P<template_id>\d+)/$','uiforms.tmanager.views.uiform_preview',name='preview'),
+	url(r'^admin/uiform_clone/(?P<template_id>\d+)/$','uiforms.tmanager.views.clone_template',name='clone'),
+	url(r'^admin/uiform_share/(?P<template_id>\d+)/$','uiforms.tmanager.views.share_template',name='share'),
+	#uiform-preview - get mostra post clona 
 )