Permalink
Browse files

ticket5: pep8, test all edit, req

  • Loading branch information...
1 parent 3a44bf4 commit 14b87f4f34dbbb29c10f15b6ce0277f65d5954cd @andreysobol committed Mar 11, 2012
View
1 requirements.txt
@@ -1,2 +1,3 @@
Django==1.3
+pil
wsgiref==0.1.2
View
12 testsobol/mainapp/forms.py
@@ -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]
View
9 testsobol/mainapp/models.py
@@ -2,6 +2,7 @@
from utils import Render
+
class Bio(models.Model, Render):
name = models.CharField(max_length=250, verbose_name='Name')
surname = models.CharField(max_length=250, verbose_name='Last name')
@@ -14,9 +15,13 @@ class Bio(models.Model, Render):
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])
+ 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]}
+ 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)
View
37 testsobol/mainapp/tests.py
@@ -18,9 +18,9 @@ def test(self):
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)
@@ -53,18 +53,30 @@ def test(self):
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, 302)
- 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)
@@ -75,6 +87,7 @@ 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):
View
15 testsobol/mainapp/utils.py
@@ -1,11 +1,14 @@
-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):
- return [[dict(get(item).items() + [('multiline',(item in multiline))]) for item in struct] for struct in structs]
+ return [[dict(get(item).items() + [('multiline', (item in multiline))])
+ for item in struct]
+ for struct in structs]
View
19 testsobol/mainapp/views.py
@@ -15,26 +15,27 @@ class Index(TemplateView):
def get_context_data(self, **kwargs):
super(Index, self).get_context_data(**kwargs)
- return {'renders':Bio.objects.get(pk = 1).render()}
+ return {'renders': Bio.objects.get(pk=1).render()}
class Edit(View):
-
- def render(self,request,form):
- return render_to_response('index.html',RequestContext(request,{'renders':form.render(), 'form':True}))
+
+ def render(self, request, form):
+ return render_to_response('index.html',
+ 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)
+ form = BioForm(model_to_dict(Bio.objects.get(pk=1)),
+ {'img': Bio.objects.get(pk=1).img})
+ return self.render(request, form)
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 +44,7 @@ def post(self, request):
model.save()
return redirect('/')
else:
- return self.render(request,form)
+ return self.render(request, form)
class Http(ListView):
View
4 testsobol/templates/index.html
@@ -47,9 +47,9 @@
{% 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="{% url http %}">request</a>
</div>
View
8 testsobol/urls.py
@@ -9,10 +9,12 @@
urlpatterns = patterns('',
# Examples:
url(r'^$', Index.as_view()),
- url(r'^edit/$', Edit.as_view()),
+ 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" } ),
+ (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')),

0 comments on commit 14b87f4

Please sign in to comment.