Permalink
Browse files

fixs

  • Loading branch information...
1 parent d61c92c commit 268154cdacb16d753e84368e1a075c61c31ab17d @mickymiseck committed Oct 9, 2012
Showing with 61 additions and 64 deletions.
  1. +5 −5 README.md
  2. +8 −6 ubigeo/fields.py
  3. +1 −0 ubigeo/models.py
  4. +28 −28 ubigeo/static/js/ubigeo.js
  5. +19 −25 ubigeo/widgets.py
View
@@ -1,13 +1,13 @@
# Django-Ubigeo-Peru
------------------
-django-ubigeo-peru, es una app que te permitira implementar facilmente
+django-ubigeo-peru, es una app que te permitira implementar facilmente
los ubigeos de Perú, en tus django app.
# Dependencias
-Python
+Python
* south
@@ -21,8 +21,8 @@ Javascript
En tu settings.py
```python
- INSTALLED_APPS = (
- ....
+ INSTALLED_APPS = (
+ ....
'ubigeo',
)
```
@@ -59,7 +59,7 @@ en tu forms.py:
from ubigeo import constant
class MyModelForm(form.ModelForm):
- ubigeo = UbigeoFormFiel(ubigeo=constant.ALL)
+ ubigeo = UbigeoFormField(ubigeo=constant.ONLY_PERU)
class Meta:
model = Ubigeo
View
@@ -9,14 +9,16 @@
class UbigeoFormField(forms.MultiValueField):
def __init__(self, *args, **kwargs):
- regiones = Ubigeo.objects.filter(parent__isnull=True).order_by('name')
+ ubigeos = Ubigeo.objects.filter(parent__isnull=True)
+ regiones = ubigeos.exclude(ubigeo__startswith='9')
+ print ('regiones: ',regiones)
if 'ubigeo' in kwargs:
if kwargs['ubigeo'] == constant.ONLY_INTERNATIONAL:
- regiones = regiones.filter(ubigeo__startswith='9').order_by('name')
- elif kwargs['ubigeo'] == constant.ONLY_PERU:
- regiones = regiones.exclude(ubigeo__startswith='9')
- provincias = Ubigeo.objects.filter(parent=regiones[0]).order_by('name')
- distritos = Ubigeo.objects.filter(parent=provincias[0]).order_by('name')
+ regiones = regiones.filter(ubigeo__startswith='9')
+ elif kwargs['ubigeo'] == constant.ALL:
+ regiones = Ubigeo.objects.filter(parent__isnull=True)
+ provincias = Ubigeo.objects.filter(parent=regiones[0])
+ distritos = Ubigeo.objects.filter(parent=provincias[0])
self.fields = (
ModelChoiceField(queryset=regiones),
ModelChoiceField(queryset=provincias),
View
@@ -11,6 +11,7 @@ class Ubigeo(models.Model):
class Meta:
verbose_name = _(u'Ubigeo')
verbose_name_plural = _(u'Ubigeos')
+ ordering = ['name',]
def __unicode__(self):
return u'%s' % smart_unicode(self.name)
@@ -1,33 +1,33 @@
function getProvincias(id,value_provincia,value_distrito){
- var provincia = $("#id_ubigeo_1");
- provincia.find('option').remove();
- provincia.append("<option value='' selected>---------</option>");
- $.getJSON('/ubigeo/provincia/json/?r='+id, function(data){
- $.each(data, function(key,value){
- if(key == 0){
- if(value_provincia != null)
- getDistritos(value_provincia,value_distrito);
- else
- getDistritos(value.pk,value_distrito);
- }
- if(value_provincia == value.pk)
- provincia.append("<option value='"+value.pk+"' selected>"+value.fields.name+"</option>");
- else
- provincia.append("<option value='"+value.pk+"'>"+value.fields.name+"</option>");
- });
- });
+ var provincia = $("#id_ubigeo_1");
+ provincia.find('option').remove();
+ provincia.append("<option value='' selected>---------</option>");
+ $.getJSON('/ubigeo/provincia/json/?r='+id, function(data){
+ $.each(data, function(key,value){
+ if(key == 0){
+ if(value_provincia != null)
+ getDistritos(value_provincia,value_distrito);
+ else
+ getDistritos(value.pk,value_distrito);
+ }
+ if(value_provincia == value.pk)
+ provincia.append("<option value='"+value.pk+"' selected>"+value.fields.name+"</option>");
+ else
+ provincia.append("<option value='"+value.pk+"'>"+value.fields.name+"</option>");
+ });
+ });
}
function getDistritos(id,value_distrito){
- var distrito = $("#id_ubigeo_2");
- distrito.find('option').remove();
- distrito.append("<option value='' selected>---------</option>");
- $.getJSON('/ubigeo/distrito/json/?d='+id, function(data){
- $.each(data, function(key,value){
- if(value_distrito == value.pk)
- distrito.append("<option value='"+value.pk+"' selected>"+value.fields.name+"</option>");
- else
- distrito.append("<option value='"+value.pk+"'>"+value.fields.name+"</option>");
- });
- });
+ var distrito = $("#id_ubigeo_2");
+ distrito.find('option').remove();
+ distrito.append("<option value='' selected>---------</option>");
+ $.getJSON('/ubigeo/distrito/json/?d='+id, function(data){
+ $.each(data, function(key,value){
+ if(value_distrito == value.pk)
+ distrito.append("<option value='"+value.pk+"' selected>"+value.fields.name+"</option>");
+ else
+ distrito.append("<option value='"+value.pk+"'>"+value.fields.name+"</option>");
+ });
+ });
}
View
@@ -1,21 +1,20 @@
-from django import forms
from django.forms.widgets import Select, MultiWidget
from models import Ubigeo
class UbigeoWidget(MultiWidget):
def __init__(self, regiones, provincias, distritos):
self.regiones = regiones
- self.provincias = provincias
- self.distritos = distritos
- widgets = (
+ self.provincias = provincias
+ self.distritos = distritos
+ widgets = (
Select(
choices = self.regiones,
- attrs = {'onchange' : 'getProvincias(this.value, null, null);'}
+ attrs = {'onchange' : 'getProvincias(this.value, null, null);'}
),
Select(
choices = self.provincias,
- attrs = {'onchange' : 'getDistritos(this.value, null);'}
+ attrs = {'onchange' : 'getDistritos(this.value, null);'}
),
Select(
choices = self.distritos
@@ -25,27 +24,22 @@ def __init__(self, regiones, provincias, distritos):
def decompress(self, value):
if value:
- ubigeo = value if isinstance(value, Ubigeo) else Ubigeo.objects.get(
- ubigeo = value
- )
+ ubigeo = value if isinstance(value, Ubigeo) else Ubigeo.objects.get(
+ ubigeo = value
+ )
self.widgets[1] = Select(
- choices=(
- (u[0], u[1]) for u in self.provincias
- ),
- attrs = {
- 'onchange' : 'getDistritos(this.value);'
- }
- )
+ choices=((u[0], u[1]) for u in self.provincias),
+ attrs = {'onchange' : 'getDistritos(this.value);'}
+ )
self.widgets[2] = Select(
- choices = (
- (u[0], u[1]) for u in self.distritos
- )
- )
- return (ubigeo.parent.parent.ubigeo,ubigeo.parent.ubigeo,ubigeo.ubigeo)
+ choices = ((u[0], u[1]) for u in self.distritos))
+ return (ubigeo.parent.parent.ubigeo,
+ ubigeo.parent.ubigeo,
+ ubigeo.ubigeo)
return (None, None, None)
class Media:
- js=(
- 'js/jquery.js',
- 'js/ubigeo.js',
- )
+ js=(
+ 'js/jquery.js',
+ 'js/ubigeo.js',
+ )

0 comments on commit 268154c

Please sign in to comment.