Skip to content

Commit

Permalink
exemplos de formulário
Browse files Browse the repository at this point in the history
  • Loading branch information
kenji-tabata committed Dec 7, 2015
1 parent db8b715 commit bcbb120
Show file tree
Hide file tree
Showing 50 changed files with 884 additions and 0 deletions.
Empty file.
3 changes: 3 additions & 0 deletions formularios/exemplos/checkbox/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
26 changes: 26 additions & 0 deletions formularios/exemplos/checkbox/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
]

operations = [
migrations.CreateModel(
name='Transportes',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('carro', models.CharField(blank=True, null=True, max_length=50)),
('moto', models.CharField(blank=True, null=True, max_length=50)),
('onibus', models.CharField(blank=True, null=True, max_length=50)),
('bicicleta', models.CharField(blank=True, null=True, max_length=50)),
],
options={
},
bases=(models.Model,),
),
]
Empty file.
10 changes: 10 additions & 0 deletions formularios/exemplos/checkbox/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.db import models

class Transportes(models.Model):
carro = models.CharField(max_length=50, blank=True, null=True)
moto = models.CharField(max_length=50, blank=True, null=True)
onibus = models.CharField(max_length=50, blank=True, null=True)
bicicleta = models.CharField(max_length=50, blank=True, null=True)

def __str__(self):
return str(self.id)
25 changes: 25 additions & 0 deletions formularios/exemplos/checkbox/templates/checkbox/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>checkbox</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Inserir item</h1>
<p><a href="{% url 'checkbox:index' %}">Adicionar</a> | <a href="{% url 'checkbox:listar' %}">Listar</a> | </p>

<form action="{% url 'checkbox:enviar' %}" method="POST">
{% csrf_token %}
<input type='checkbox' name='carro' id='carro' value='Sim'/>
<label for='carro'>Carro</label><br/>
<input type='checkbox' name='moto' id='moto' value='Sim'/>
<label for='moto'>Moto</label><br/>
<input type='checkbox' name='onibus' id='onibus' value='Sim'/>
<label for='onibus'>Ônibus</label><br/>
<input type='checkbox' name='bicicleta' id='bicicleta' value='Sim'/>
<label for='bicicleta'>Bicicleta</label><br/>
<input type="submit" id='enviar' value='Enviar'/><br/>
</form>
</body>
</html>
23 changes: 23 additions & 0 deletions formularios/exemplos/checkbox/templates/checkbox/listar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<title>checkbox</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p><a href="{% url 'checkbox:index' %}">Adicionar</a> | <a href="{% url 'checkbox:listar' %}">Listar</a> | </p>
<h1>Listando as alternativas</h1>
{% for transporte in transportes %}
<p><strong><a href="{% url 'checkbox:ver-alternativas' transporte.id %}">ID</a>: </strong>{{transporte.id}} /
<strong>Carro: </strong>{{transporte.carro}} /
<strong>Moto: </strong>{{transporte.moto}} /
<strong>Ônibus: </strong>{{transporte.onibus}} /
<strong>Bicicleta: </strong>{{transporte.bicicleta}}|
<a href="{% url 'checkbox:deletar' transporte.id %}">Remover</a>
</p>
{% empty %}
<p>Nenhuma resposta foi encontrada</p>
{% endfor %}
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p><a href="{% url 'checkbox:index' %}">Adicionar</a> | <a href="{% url 'checkbox:listar' %}">Listar</a> | </p>
{% if transporte %}
<form action="{% url 'checkbox:atualizar' transporte.id %}" method="POST">
{% csrf_token %}
<input type='hidden' name='id' id='{{transporte.id}}' value='{{ transporte.id }}'/>
<input type='checkbox' name='carro' id='carro' value='Sim' {% if transporte.carro == "Sim" %} checked {% endif %}/>
<label for='carro'>Carro</label><br/>
<input type='checkbox' name='moto' id='moto' value='Sim' {% if transporte.moto == "Sim" %} checked {% endif %}/>
<label for='moto'>Moto</label><br/>
<input type='checkbox' name='onibus' id='onibus' value='Sim' {% if transporte.onibus == "Sim" %} checked {% endif %}/>
<label for='onibus'>Ônibus</label><br/>
<input type='checkbox' name='bicicleta' id='bicicleta' value='Sim' {% if transporte.bicicleta == "Sim" %} checked {% endif %}/>
<label for='bicicleta'>Bicicleta</label><br/>
<input type="submit" id='enviar' value='Enviar'/><br/>
</form>

<p><strong>ID: </strong>{{transporte.id}} /
<strong>Carro: </strong>{{transporte.carro}} /
<strong>Moto: </strong>{{transporte.moto}} /
<strong>Ônibus: </strong>{{transporte.onibus}} /
<strong>Bicicleta: </strong>{{transporte.bicicleta}}</p>
{% endif %}
</body>
</html>
46 changes: 46 additions & 0 deletions formularios/exemplos/checkbox/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from django.test import TestCase
from checkbox.models import Transportes

from django.test import Client

class TransportesTest(TestCase):
def setUp(self):
self.transporte1 = {
'id': 1,
'carro': 'Sim',
'moto': 'Não',
'onibus': 'Não',
'bicicleta': 'Sim',
}

def test_inserir_transporte(self):
transporte = Transportes.objects.create(**self.transporte1)
self.assertEqual(transporte.id, 1)


def test_carregar_alternativas(self):
transporte = Transportes.objects.create(**self.transporte1)

get_transporte = Transportes.objects.get(id=1)




def test_enviar_checkbox_selecionados(self):
client = Client()
check1 = client.post('/checkbox/enviar/', {'carro':'Sim','moto':'Não','onibus':'Sim','bicicleta':'Sim'})
check2 = client.post('/checkbox/enviar/', {'carro':'Sim','onibus':'Sim','bicicleta':'Sim'})
check3 = client.post('/checkbox/enviar/', {'moto':'Não','onibus':'Sim'})
check4 = client.post('/checkbox/enviar/', {'onibus':'Sim'})
check5 = client.post('/checkbox/enviar/', {})
check6 = client.post('/checkbox/enviar', {})
check7 = client.get('/checkbox/enviar/')

self.assertEqual(check1.status_code, 200)
self.assertEqual(check2.status_code, 200)
self.assertEqual(check3.status_code, 200)
self.assertEqual(check4.status_code, 200)
self.assertEqual(check5.status_code, 200)
self.assertEqual(check6.status_code, 301)
self.assertEqual(check7.status_code, 200)

12 changes: 12 additions & 0 deletions formularios/exemplos/checkbox/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.conf.urls import patterns, url
from checkbox import views

urlpatterns = patterns('',

url(r'^$', views.index, name='index'),
url(r'^enviar/$', views.enviar, name='enviar'),
url(r'^listar/$', views.listar, name='listar'),
url(r'^ver-alternativas/(?P<pk>[0-9]+)/$', views.carregar, name='ver-alternativas'),
url(r'^atualizar/(?P<pk>[0-9]+)/$', views.atualizar, name='atualizar'),
url(r'^deletar/(?P<pk>[0-9]+)/$', views.deletar, name='deletar'),
)
57 changes: 57 additions & 0 deletions formularios/exemplos/checkbox/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from django.shortcuts import render, get_object_or_404, redirect
from checkbox.models import Transportes

def index(request):
return render(request, 'checkbox/index.html', {})

def enviar(request):
if request.method == 'POST':

p_carro = request.POST.get('carro','Não')
p_moto = request.POST.get('moto','Não')
p_onibus = request.POST.get('onibus','Não')
p_bicicleta = request.POST.get('bicicleta','Não')

transportes = Transportes.objects.create(
carro = p_carro,
moto = p_moto,
onibus = p_onibus,
bicicleta = p_bicicleta,
)

return redirect('checkbox:checkbox.views.listar')


def listar(request):
transportes = Transportes.objects.order_by('-id')
return render(request, 'checkbox/listar.html', {'transportes':transportes})


def carregar(request, pk):
transporte = get_object_or_404(Transportes, pk=pk)
return render(request, 'checkbox/ver-alternativas.html', {'transporte': transporte})


def atualizar(request, pk):
transporte = get_object_or_404(Transportes, pk=pk)

if request.method == 'POST':
p_carro = request.POST.get('carro','Não')
p_moto = request.POST.get('moto','Não')
p_onibus = request.POST.get('onibus','Não')
p_bicicleta = request.POST.get('bicicleta','Não')

transporte.carro = p_carro
transporte.moto = p_moto
transporte.onibus = p_onibus
transporte.bicicleta = p_bicicleta
transporte.save()

return render(request, 'checkbox/ver-alternativas.html', {'transporte': transporte})

def deletar(request, pk):
transporte = get_object_or_404(Transportes, pk=pk)

transporte.delete()

return redirect('checkbox:checkbox.views.listar')
Empty file.
3 changes: 3 additions & 0 deletions formularios/exemplos/checkbox_multi/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
23 changes: 23 additions & 0 deletions formularios/exemplos/checkbox_multi/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
]

operations = [
migrations.CreateModel(
name='Transportes',
fields=[
('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)),
('opcoes_transp', models.CharField(blank=True, null=True, max_length=50)),
],
options={
},
bases=(models.Model,),
),
]
Empty file.
7 changes: 7 additions & 0 deletions formularios/exemplos/checkbox_multi/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.db import models

class Transportes(models.Model):
opcoes_transp = models.CharField(max_length=50, blank=True, null=True)

def __str__(self):
return str(self.id)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>checkbox_multi</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Inserir item</h1>
<p><a href="{% url 'checkbox_multi:index' %}">Adicionar</a> | <a href="{% url 'checkbox_multi:listar' %}">Listar</a> | </p>

<form action="{% url 'checkbox_multi:enviar' %}" method="POST">
{% csrf_token %}
<input type='checkbox' name='opcoes_transp' id='carro' value='Carro'/>
<label for='carro'>Carro</label><br/>
<input type='checkbox' name='opcoes_transp' id='moto' value='Moto'/>
<label for='moto'>Moto</label><br/>
<input type='checkbox' name='opcoes_transp' id='onibus' value='Ônibus'/>
<label for='onibus'>Ônibus</label><br/>
<input type='checkbox' name='opcoes_transp' id='bicicleta' value='Bicicleta'/>
<label for='bicicleta'>Bicicleta</label><br/>
<input type="submit" id='enviar' value='Enviar'/><br/>
</form>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<title>checkbox_multi</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p><a href="{% url 'checkbox_multi:index' %}">Adicionar</a> | <a href="{% url 'checkbox_multi:listar' %}">Listar</a> | </p>
<h1>Listando as alternativas</h1>
{% for transporte in transportes %}
<p><strong><a href="{% url 'checkbox_multi:ver-alternativas' transporte.id %}">ID</a>: </strong>{{transporte.id}} /
<strong>Carro: </strong>{% if 'Carro' in transporte.opcoes_transp %}Sim {% else %}Não {% endif %} /
<strong>Moto: </strong>{% if 'Moto' in transporte.opcoes_transp %}Sim {% else %}Não {% endif %} /
<strong>Ônibus: </strong>{% if 'Ônibus' in transporte.opcoes_transp %}Sim {% else %}Não {% endif %}/
<strong>Bicicleta: </strong>{% if 'Bicicleta' in transporte.opcoes_transp %}Sim {% else %}Não {% endif %}|
<a href="{% url 'checkbox:deletar' transporte.id %}">Remover</a>
</p>
{% empty %}
<p>Nenhuma resposta foi encontrada</p>
{% endfor %}
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p><a href="{% url 'checkbox_multi:index' %}">Adicionar</a> | <a href="{% url 'checkbox_multi:listar' %}">Listar</a> | </p>
{% if transporte %}
<form action="{% url 'checkbox_multi:atualizar' transporte.id %}" method="POST">
{% csrf_token %}
<input type='hidden' name='id' id='{{transporte.id}}' value='{{ transporte.id }}'/>
<input type='checkbox' name='carro' id='carro' value='Sim' {% if 'Carro' in transporte.carro %} checked {% endif %}/>
<label for='carro'>Carro</label><br/>
<input type='checkbox' name='moto' id='moto' value='Sim' {% if 'Moto' in transporte.opcoes_transp %} checked {% endif %}/>
<label for='moto'>Moto</label><br/>
<input type='checkbox' name='onibus' id='onibus' value='Sim' {% if 'Ônibus' in transporte.opcoes_transp %} checked {% endif %}/>
<label for='onibus'>Ônibus</label><br/>
<input type='checkbox' name='bicicleta' id='bicicleta' value='Sim' {% if 'Bicicleta' in transporte.opcoes_transp %} checked {% endif %}/>
<label for='bicicleta'>Bicicleta</label><br/>
<input type="submit" id='enviar' value='Enviar'/><br/>
</form>

<p><strong>ID: </strong>{{transporte.id}} /
<strong>Carro: </strong>{{transporte.carro}} /
<strong>Moto: </strong>{{transporte.moto}} /
<strong>Ônibus: </strong>{{transporte.onibus}} /
<strong>Bicicleta: </strong>{{transporte.bicicleta}}</p>
{% endif %}
</body>
</html>
Loading

0 comments on commit bcbb120

Please sign in to comment.