Skip to content

Commit

Permalink
finish practise7
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioPadilla committed Jan 21, 2017
1 parent a73c8aa commit 63fe33b
Show file tree
Hide file tree
Showing 15 changed files with 325 additions and 27 deletions.
Binary file modified Practica7/sitio_web/db.sqlite3
Binary file not shown.
55 changes: 55 additions & 0 deletions Practica7/sitio_web/restaurantes/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
Created at 20/1/17
__author__ = 'Sergio Padilla'
"""
from crispy_forms.bootstrap import FormActions
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Submit, Field
from django import forms
from django.forms import IntegerField, CharField, ChoiceField


class AddForm(forms.Form):
id = IntegerField()
cuisine = CharField(max_length=100, required=False)
borough = ChoiceField(
choices=(
('Manhattan', "Manhattan"),
('Brooklyn', "Brooklyn"),
('Queens', "Queens"),
('Staten Island', "Staten Island"),
('Bronx', "Bronx")
),
widget=forms.RadioSelect,
initial='Manhattan',
)
name = CharField(max_length=100)
postal_code = IntegerField()

helper = FormHelper()
helper.form_class = 'form-horizontal'
helper.label_class = 'col-lg-4'
helper.field_class = 'col-lg-4'
helper.layout = Layout(
Field('id', css_class='input-small'),
Field('cuisine', css_class='input-large'),
'borough',
Field('name', css_class='input-large'),
Field('postal_code', css_class='input-small'),
FormActions(
Submit('save_changes', 'Add restaurant', css_class="btn-primary"),
Submit('cancel', 'Cancel'),
)
)

def clean(self):
self.is_valid()
borough = self.cleaned_data.get('borough')
code = self.cleaned_data.get('postal_code')

if (borough == 'Manhattan' and 10000 < code < 10044) or (borough == 'Brooklyn' and 11200 < code < 11239) or \
(borough == 'Queens' and 11690 < code < 11107) or (borough == 'Staten Island' and 10000 < code < 10044)\
or (borough == 'Bronx' and 10450 < code < 10475):
return self.cleaned_data

raise forms.ValidationError('The postal code doesn\'t correspond with the borough')
1 change: 0 additions & 1 deletion Practica7/sitio_web/restaurantes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from django.db import models

# Create your models here.

class Restaurant(models.Model):
id = models.IntegerField(primary_key=True, auto_created=True, unique=True, editable=False)
Expand Down
40 changes: 40 additions & 0 deletions Practica7/sitio_web/restaurantes/static/css/signin.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #eee;
}

.form-signin {
max-width: 330px;
padding: 15px;
margin: 0 auto;
}
.form-signin .form-signin-heading,
.form-signin .checkbox {
margin-bottom: 10px;
}
.form-signin .checkbox {
font-weight: normal;
}
.form-signin .form-control {
position: relative;
height: auto;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 10px;
font-size: 16px;
}
.form-signin .form-control:focus {
z-index: 2;
}
.form-signin input[type="email"] {
margin-bottom: -1px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}
.form-signin input[type="password"] {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
}
66 changes: 66 additions & 0 deletions Practica7/sitio_web/restaurantes/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{% load i18n %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">

<title>register/login</title>

{% load static %}

<!-- Bootstrap core CSS -->
<link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">

<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link href="{% static 'css/ie10-viewport-bug-workaround.css' %}" rel="stylesheet">

<!-- Custom styles for this template -->
<link href="{% static 'css/signin.css' %}" rel="stylesheet">

<!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
<!--[if lt IE 9]><script src="{% static 'js/ie8-responsive-file-warning.js' %}"></script><![endif]-->
<script src="{% static 'js/ie-emulation-modes-warning.js' %}"></script>

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>

<body>

<div class="container">

{# <div id="header">#}
{# {% block header %}#}
{# <a href="{% url 'index' %}">{% trans "Home" %}</a> |#}

{% if user.is_authenticated %}
{% trans "Logged in" %}: {{ user.username }}
(<a href="{% url 'auth_logout' %}?next=/restaurantes/"></a> |
<a href="{% url 'auth_password_change' %}">{% trans "Change password" %}</a>)
{% else %}
<a href="{% url 'auth_login' %}"></a>
{% endif %}
<hr />
{# {% endblock %}#}
{# </div>#}

{# <div id="content">#}
{% block content %}{% endblock %}
{# </div>#}

</div> <!-- /container -->


<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="{% static 'js/ie10-viewport-bug-workaround.js' %}"></script>
</body>
</html>
37 changes: 37 additions & 0 deletions Practica7/sitio_web/restaurantes/templates/base_old.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{% load i18n %}
<!DOCTYPE html>
<html lang="en">

<head>
<link rel="stylesheet" href="{{ STATIC_URL }}style.css" />
<title>{% block title %}User test{% endblock %}</title>
</head>

<body>
<div id="header">
{% block header %}
<a href="{% url 'index' %}">{% trans "Home" %}</a> |

{% if user.is_authenticated %}
{% trans "Logged in" %}: {{ user.username }}
(<a href="{% url 'auth_logout' %}?next=/restaurantes/"></a> |
<a href="{% url 'auth_password_change' %}">{% trans "Change password" %}</a>)
{% else %}
<a href="{% url 'auth_login' %}">{% trans "Log in" %}</a>
{% endif %}
<hr />
{% endblock %}
</div>

<div id="content">
{% block content %}{% endblock %}
</div>

<div id="footer">
{% block footer %}
<hr />
{% endblock %}
</div>
</body>

</html>
19 changes: 5 additions & 14 deletions Practica7/sitio_web/restaurantes/templates/mongo_example_add.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends "mongo_examples.html" %}
{% load crispy_forms_tags %}

{% block navheader %}
<li><a href="/restaurantes/find">Consultas</a></li>
Expand All @@ -9,20 +10,10 @@
{% block main %}
<h1>Sección de ejemplo de insercción en MongoDB</h1>

<form action="/restaurantes/add" method="post">
<fieldset>
<legend>Añade un nuevo restaurante</legend>
<label>ID</label>
<input type="text" name="id">
<label>CUISINE</label>
<input type="text" name="cuisine">
<label>NAME</label>
<input type="text" name="name">
<label>BOROUGH</label>
<input type="text" name="borough">
<button type="submit" class="btn">INSERTAR</button>
</fieldset>
</form>
{% csrf_token %}

{% crispy form %}

{% if message %}
<h4>{{ message }}</h4>
{% endif %}
Expand Down
15 changes: 15 additions & 0 deletions Practica7/sitio_web/restaurantes/templates/registration/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "base.html" %}

{% block content %}

<form class="form-signin" role="form" method="post" action=".">
{% csrf_token %}

<h2 class="form-signin-heading">Please sign in</h2>
<input class="form-control" placeholder="Username" id="id_username" maxlength="254" name="username" type="text" required autofocus=""/>
<input type="password" class="form-control" placeholder="Password" id="id_password" name="password" required />

<button class="btn btn-lg btn-primary btn-block" type="submit" value="Submit" >Sign in</button>
</form>

{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends "base.html" %}

{% block content %}
<h1>Logged Out</h1>
<p>You are now logged out.</p>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends "base.html" %}

{% block content %}
<h1>Registration Complete</h1>
<p>You are now registered</p>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "base.html" %}

{% block content %}
<h1>Register Here</h1>
<form method="post" action=".">
{% csrf_token %}
{{ form.as_p }}

<input type="submit" value="Submit" />
</form>
{% endblock %}
1 change: 1 addition & 0 deletions Practica7/sitio_web/restaurantes/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
url(r'^mongo_examples/$', views.find, name='mongo_examples'),
url(r'^findpage/$', views.findpage, name='findpage'),
url(r'^find/$', views.find, name='find'),
url(r'^add/$', views.add, name='add'),
url(r'^get_borough_data/$', views.get_borough_data, name='get_borough_data'),
]
61 changes: 53 additions & 8 deletions Practica7/sitio_web/restaurantes/views.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, HttpResponse
from django.template import loader

# Create your views here.
from .forms import AddForm
from .models import Restaurant
from pymongo import MongoClient
from django.conf import settings
import json


@login_required
def index(request):
return render(request, 'home.html', {})


@login_required
def me(request):
return render(request, 'me.html', {})


@login_required
def examples(request):
return render(request, 'examples.html', {})


@login_required
def find(request):
client = MongoClient()
db = client.test
db = settings.MONGOCLIENT.test
nperpage = 200
total = db.restaurants.find().count()
page = int(request.GET.get('page', 1))
Expand All @@ -38,9 +43,48 @@ def find(request):
return render(request, template, context)


@login_required
def add(request):
if request.method == 'POST':
form_post = AddForm(request.POST, request.FILES)
message = ""

if form_post.is_valid():
db = settings.MONGOCLIENT.test
id = form_post.cleaned_data.get('id')
cuisine = form_post.cleaned_data.get('cuisine')
name = form_post.cleaned_data.get('name')
borough = form_post.cleaned_data.get('borough')
result = db.restaurants.insert_one(
{
'restaurant_id': id,
'cuisine': cuisine,
'name': name,
'borough': borough
})

if result:
print result.inserted_id
message = 'El restaurante ha sido insertado con exito'
else:
message = 'Error insertando el restaurante'
else:
print(form_post.errors)

template = 'mongo_example_add.html'
context = {
'message': message,
'form': form_post
}

return render(request, template, context)

return render(request, 'mongo_example_add.html', {'form': AddForm()})


@login_required
def findpage(request):
client = MongoClient()
db = client.test
db = settings.MONGOCLIENT.test
nperpage = 200
page = int(request.GET.get('page', 1))
offset = (page - 1) * nperpage
Expand All @@ -59,13 +103,14 @@ def findpage(request):
return HttpResponse(json.dumps(restaurants))


@login_required
def highcharts(request):
return render(request, 'highcharts.html', {})


def get_borough_data(request):
client = MongoClient()
db = client.test
@login_required
def get_borough_data():
db = settings.MONGOCLIENT.test
boroughs = {}
cursor = db.restaurants.find()
for document in cursor:
Expand Down
Loading

0 comments on commit 63fe33b

Please sign in to comment.