Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

...
head fork: andreysobol/42test
  • 11 commits
  • 13 files changed
  • 0 commit comments
  • 1 contributor
Commits on Mar 11, 2012
andreysobol many fixes b3899d7
andreysobol Merge branch 't1_show_bio' into t2_request_middleware
Conflicts:
	testsobol/mainapp/tests.py
	testsobol/mainapp/views.py
	testsobol/urls.py
c980321
andreysobol ticket3: pep8, named url, 10 first, test <10 request c52e8b2
andreysobol Merge branch 't2_request_middleware' into t4_settings_in_context
Conflicts:
	testsobol/mainapp/tests.py
bfa1afd
andreysobol ticket4: pep8, fix test context 512e72f
andreysobol Merge branch 't4_settings_in_context' into t5_forms_and_auth
Conflicts:
	testsobol/mainapp/models.py
	testsobol/mainapp/tests.py
	testsobol/mainapp/views.py
	testsobol/templates/index.html
	testsobol/urls.py
3a44bf4
andreysobol ticket5: pep8, test all edit, req 14b87f4
andreysobol Merge branch 't5_forms_and_auth' into t6_ajax
Conflicts:
	testsobol/mainapp/tests.py
	testsobol/mainapp/views.py
31b9007
andreysobol ticket6: pep8, input readonly fef71af
andreysobol Merge branch 't6_ajax' into t7_reversed
Conflicts:
	testsobol/mainapp/tests.py
	testsobol/mainapp/utils.py
79d764d
andreysobol ticket7: pep8, remove render in model 79e15c8
1  requirements.txt
View
@@ -1,2 +1,3 @@
Django==1.3
+pil
wsgiref==0.1.2
2  testsobol/Makefile
View
@@ -0,0 +1,2 @@
+test:
+ python manage.py test mainapp
3  testsobol/mainapp/context_processor.py
View
@@ -1,4 +1,5 @@
from django.conf import settings
+
def settings_context_processor(request):
- return {'settings':settings}
+ return {'settings': settings}
12 testsobol/mainapp/forms.py
View
@@ -5,15 +5,18 @@
from models import Bio
from utils import Render
+
class ImgWidget(ClearableFileInput):
def render(self, name, value, attrs=None):
substitutions = {}
template = u'%(input)s'
- substitutions['input'] = super(ClearableFileInput, self).render(name, value, attrs)
+ substitutions['input'] = super(ClearableFileInput, self)\
+ .render(name, value, attrs)
if value and hasattr(value, "url"):
template = u'%(initial)s<br />%(input)s'
- substitutions['initial'] = (u'<img src="%s"/>' % (escape(value.url)))
+ substitutions['initial'] = (u'<img src="%s"/>' %\
+ (escape(value.url)))
return mark_safe(template % substitutions)
@@ -25,7 +28,10 @@ class Meta:
}
def render(self):
- form_field = dict([(field.name, {'label':field.label,'value':field.as_widget(),'errors':field.errors}) for field in self])
+ form_field = dict([(field.name,
+ {'label':field.label,
+ 'value':field.as_widget(),
+ 'errors':field.errors}) for field in self])
get = lambda item: form_field[item]
3  testsobol/mainapp/middleware.py
View
@@ -2,8 +2,9 @@
from models import Request
+
class RequestMiddleware(object):
-
+
def process_request(self, request):
r = Request()
r.date = datetime.now()
9 testsobol/mainapp/models.py
View
@@ -1,8 +1,7 @@
from django.db import models
-from utils import Render
-class Bio(models.Model, Render):
+class Bio(models.Model):
name = models.CharField(max_length=250, verbose_name='Name')
surname = models.CharField(max_length=250, verbose_name='Last name')
birth = models.DateField(verbose_name='Date of birth')
@@ -13,12 +12,6 @@ class Bio(models.Model, Render):
bio = models.TextField(verbose_name='Bio')
img = models.ImageField(verbose_name='Photo', upload_to='img/')
- def render(self):
- field_types = dict([(field.name, field.verbose_name) for field in self._meta.fields])
-
- get = lambda item: {'value':unicode(getattr(self,item)) if item!='img' else '<img src="%s" />' % getattr(self,item).url,'label':field_types[item]}
-
- return self.get_struct(get, False)
class Request(models.Model):
date = models.DateTimeField()
92 testsobol/mainapp/tests.py
View
@@ -15,14 +15,12 @@
class IndexViewTest(TestCase):
def test(self):
- fixtures = ['initial_data.json']
-
page = self.client.get('')
self.assertEqual(page.status_code, 200)
-
- for key,value in model_to_dict(Bio.objects.get(pk = 1)).items():
- if key!='id':
+
+ for key, value in model_to_dict(Bio.objects.get(pk=1)).items():
+ if key != 'id':
self.assertTrue(page.content.find(unicode(value)) != -1)
@@ -30,7 +28,11 @@ class RequestTest(TestCase):
def test(self):
page = self.client.get('')
- self.assertTrue(bool(Request.objects.filter(date__gte = (datetime.now() - timedelta(minutes=1)))))
+
+ self.assertEqual(page.status_code, 200)
+
+ self.assertTrue(bool(Request.objects.filter(
+ date__gte=(datetime.now() - timedelta(minutes=1)))))
class RequestViewTest(TestCase):
@@ -40,28 +42,42 @@ def test(self):
self.assertEqual(page.status_code, 200)
self.assertTrue(bool(page.context['request']))
+
class SettingsContextTest(TestCase):
-
+
def test(self):
page = self.client.get('')
self.assertEqual(page.status_code, 200)
self.assertTrue(page.context['settings'])
+ self.assertEqual(page.context['settings'].STATIC_URL, '/static/')
class Edit(TestCase):
-
- def test(self):
- fixtures = ['initial_data.json']
- page = self.client.post('/accounts/login/', {'username': 'admin', 'password': 'admin'})
+ def test(self):
+ page = self.client.post('/accounts/login/',
+ {'username': 'admin', 'password': 'admin'})
self.assertEqual(page.status_code, 302)
-
- page = self.client.post('/edit/', {"bio": "Noooooooooooooo", "surname": "Sobol", "name": "Andrey", "other": "pigeon post - white pigeon only", "birth": "1990-09-18", "skype": "andreysobol", "jabber": "pisecs@gmail.com", "email": "asobol@mail.ua"})
+
+ s = {"bio": "Noooooooooooooo",
+ "surname": "Sobol",
+ "name": "Andrey",
+ "other": "pigeon post - white pigeon only",
+ "birth": "1990-09-18",
+ "skype": "andreysobol",
+ "jabber": "pisecs@gmail.com",
+ "email": "asobol@mail.ua"}
+ page = self.client.post('/edit/', s)
self.assertEqual(page.status_code, 200)
self.assertTrue(page.content == 'Okay')
- self.assertTrue(Bio.objects.get(pk = 1).bio == "Noooooooooooooo")
-
- page = self.client.post('/edit/', {"bio": "Noooooooooooooo", "surname": "Sobol", "name": "Andrey", "other": "pigeon post - white pigeon only"})
+ for t in s:
+ self.assertEqual(unicode(getattr(Bio.objects.get(pk=1), t)), s[t])
+
+ page = self.client.post('/edit/',
+ {"bio": "Noooooooooooooo",
+ "surname": "Sobol",
+ "name": "Andrey",
+ "other": "pigeon post - white pigeon only"})
self.assertEqual(page.status_code, 200)
self.assertTrue(page.content.find("error") != -1)
@@ -69,14 +85,13 @@ def test(self):
class AjaxEdit(TestCase):
def test(self):
- fixtures = ['initial_data.json']
-
- page = self.client.post('/accounts/login/', {'username': 'admin', 'password': 'admin'})
+ page = self.client.post('/accounts/login/',
+ {'username': 'admin', 'password': 'admin'})
self.assertEqual(page.status_code, 302)
page = self.client.post('/edit/')
self.assertTrue(page.content.find('<body>') == -1)
-
+
page = self.client.get('/edit/')
self.assertTrue(page.content.find('<body>') != -1)
self.assertTrue(page.content.find('<script') != -1)
@@ -88,14 +103,37 @@ def test(self):
class EditReverse(TestCase):
def test(self):
- fixtures = ['initial_data.json']
-
page = self.client.get('/')
- self.assertTrue(page.content.find('>Name:') < page.content.find('Last name:'))
- self.assertTrue(page.content.find('Bio:') > page.content.find('Other'))
+ self.assertTrue(page.content.find('>Name:')
+ < page.content.find('Last name:'))
+ self.assertTrue(page.content.find('Bio:')
+ > page.content.find('Other'))
- page = self.client.post('/accounts/login/', {'username': 'admin', 'password': 'admin'})
+ page = self.client.post('/accounts/login/',
+ {'username': 'admin', 'password': 'admin'})
page = self.client.get('/edit/')
- self.assertTrue(page.content.find('>Name:') > page.content.find('Last name:'))
- self.assertTrue(page.content.find('Bio:') < page.content.find('Other'))
+ self.assertTrue(page.content.find('>Name:')
+ > page.content.find('Last name:'))
+ self.assertTrue(page.content.find('Bio:')
+ < page.content.find('Other'))
+
+
+class NameUrlTest(TestCase):
+
+ def test(self):
+ page = self.client.get('/')
+ self.assertEqual(page.status_code, 200)
+ self.assertTrue(page.content.find('a href="/http/"') != -1)
+ self.assertTrue(page.content.find('a href="/edit/"') != -1)
+
+
+class ManyRequestTest(TestCase):
+
+ def test(self):
+ for t in range(11):
+ page = self.client.get('/')
+ self.assertEqual(page.status_code, 200)
+
+ page = self.client.get('/http/')
+ self.assertTrue(page.context['request'].count() == 10)
32 testsobol/mainapp/utils.py
View
@@ -1,15 +1,35 @@
-structs = (
- ('name','surname','birth','img'),
- ('email','jabber','skype','other', 'bio'),
+structs = (
+ ('name', 'surname', 'birth', 'img'),
+ ('email', 'jabber', 'skype', 'other', 'bio'),
)
-multiline = ('bio','other', 'img')
+multiline = ('bio', 'other', 'img')
+
class Render(object):
-
+
def get_struct(self, get, reverse):
if reverse:
local_structs = [struct[::-1] for struct in structs]
else:
local_structs = structs
- return [[dict(get(item).items() + [('multiline',(item in multiline))]) for item in struct] for struct in local_structs]
+ return [[dict(get(item).items() + [('multiline', (item in multiline))])
+ for item in struct]
+ for struct in local_structs]
+
+
+class DataRender(Render):
+
+ def __init__(self, model):
+ self.model = model
+
+ def render(self):
+ field_types = dict([(field.name, field.verbose_name) \
+ for field in self.model._meta.fields])
+
+ get = lambda item: {'value':\
+ unicode(getattr(self.model, item)) if item != 'img'\
+ else '<img src="%s" />' % getattr(self.model, item).url,
+ 'label': field_types[item]}
+
+ return self.get_struct(get, False)
28 testsobol/mainapp/views.py
View
@@ -1,6 +1,6 @@
from django.views.generic import View, TemplateView, ListView
from django.forms.models import model_to_dict
-from django.shortcuts import redirect, render_to_response
+from django.shortcuts import render_to_response
from django.template import RequestContext
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
@@ -8,33 +8,37 @@
from models import Bio, Request
from forms import BioForm
+from utils import DataRender
+
class Index(TemplateView):
template_name = 'index.html'
def get_context_data(self, **kwargs):
- context = super(Index, self).get_context_data(**kwargs)
- return {'renders':Bio.objects.get(pk = 1).render()}
+ super(Index, self).get_context_data(**kwargs)
+ d = DataRender(Bio.objects.get(pk=1))
+ return {'renders': d.render()}
class Edit(View):
-
- def render(self,request,form,temp):
- return render_to_response(temp,RequestContext(request,{'renders':form.render(), 'form':True}))
+
+ def render(self, request, form, temp):
+ return render_to_response(temp,
+ RequestContext(request, {'renders': form.render(), 'form': True}))
@method_decorator(login_required())
def dispatch(self, request, *args, **kwargs):
return super(Edit, self).dispatch(request, *args, **kwargs)
def get(self, request):
- form = BioForm(model_to_dict(Bio.objects.get(pk = 1)),{'img':Bio.objects.get(pk = 1).img})
- render=form.render()
- return self.render(request,form,'index.html')
+ form = BioForm(model_to_dict(Bio.objects.get(pk=1)),
+ {'img': Bio.objects.get(pk=1).img})
+ return self.render(request, form, 'index.html')
def post(self, request):
if not request.FILES:
- form = BioForm(request.POST, {'img':Bio.objects.get(pk = 1).img})
+ form = BioForm(request.POST, {'img': Bio.objects.get(pk=1).img})
else:
form = BioForm(request.POST, request.FILES)
if form.is_valid():
@@ -43,7 +47,7 @@ def post(self, request):
model.save()
return HttpResponse("Okay")
else:
- return self.render(request,form,'content.html')
+ return self.render(request, form, 'content.html')
class Http(ListView):
@@ -53,4 +57,4 @@ class Http(ListView):
paginate_by = 10
def get_queryset(self):
- return Request.objects.order_by('-date')
+ return Request.objects.order_by('date')
12 testsobol/settings.py
View
@@ -13,12 +13,12 @@
DATABASES = {
'default': {
- 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
- 'NAME': 'db.sqlite3', # Or path to database file if using sqlite3.
- 'USER': '', # Not used with sqlite3.
- 'PASSWORD': '', # Not used with sqlite3.
- 'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
- 'PORT': '', # Set to empty string for default. Not used with sqlite3.
+ 'ENGINE': 'django.db.backends.sqlite3',
+ 'NAME': 'db.sqlite3',
+ 'USER': '',
+ 'PASSWORD': '',
+ 'HOST': '',
+ 'PORT': '',
}
}
4 testsobol/static/custom.js
View
@@ -20,6 +20,8 @@ $(function(){
url:'/edit/',
type: 'post',
success: function(response){
+ $('input').removeAttr('readonly');
+ $('textarea').removeAttr('readonly');
if(response=="Okay"){
$('.mess').html('Bio update. <a href="/">View.</a>');
$('.error').css('display','none');
@@ -33,6 +35,8 @@ $(function(){
},
beforeSubmit: function(){
$('input[type="submit"]').css('display','none');
+ $('input').attr('readonly','readonly');
+ $('textarea').attr('readonly','readonly');
$('.mess').html('<img src="/static/ajax-loader.gif" style="width:32px;"/>');
}
}
6 testsobol/templates/index.html
View
@@ -31,11 +31,11 @@
{% else %}
<div class="but">
{% if user.is_authenticated %}
- <a href="/edit/">edit</a>
+ <a href="{% url edit %}">edit</a>
{% else %}
- <a href="/edit/">login and edit</a>
+ <a href="{% url edit %}">login and edit</a>
{% endif %}
- <a href="/http/">request</a>
+ <a href="{% url http %}">request</a>
</div>
{% endif %}
{% endblock %}
12 testsobol/urls.py
View
@@ -1,4 +1,4 @@
-from django.conf.urls.defaults import patterns, include, url
+from django.conf.urls.defaults import patterns, url
from django.conf import settings
from testsobol.mainapp.views import Index, Http, Edit
@@ -9,10 +9,12 @@
urlpatterns = patterns('',
# Examples:
url(r'^$', Index.as_view()),
- url(r'^edit/$', Edit.as_view()),
- url(r'^http/$', Http.as_view()),
- (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
- (r'^accounts/login/$', 'django.contrib.auth.views.login', { "template_name": "admin/login.html" } ),
+ url(r'^edit/$', Edit.as_view(), name="edit"),
+ url(r'^http/$', Http.as_view(), name="http"),
+ (r'^media/(?P<path>.*)$', 'django.views.static.serve',
+ {'document_root': settings.MEDIA_ROOT}),
+ (r'^accounts/login/$', 'django.contrib.auth.views.login',
+ {"template_name": "admin/login.html"}),
# url(r'^testsobol/', include('testsobol.foo.urls')),

No commit comments for this range

Something went wrong with that request. Please try again.