-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(votepeloclima): add get_states and get_city_choices in candidatu…
…re form
- Loading branch information
1 parent
d56d11a
commit dad5a60
Showing
6 changed files
with
10,777 additions
and
4 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
(function ($) { | ||
"use strict"; | ||
|
||
$(function () { | ||
const $stateField = $('[data-address-fields="state"]'); | ||
const $cityField = $('[data-address-fields="city"]'); | ||
console.log("oiee") | ||
|
||
if ($stateField.length) { | ||
$stateField.select2({ dropdownAutoWidth: true, width: 'auto', placeholder: 'Selecione seu estado' }); | ||
$cityField.select2({ allowClear: true, dropdownAutoWidth: true, width: 'auto', placeholder: 'Selecione sua cidade' }); | ||
|
||
var uf; | ||
$stateField.on("change", (evt) => { | ||
uf = evt.target.value; | ||
const url = $stateField.data("address-url"); | ||
|
||
$.get(url + "?state=" + uf, (data) => { | ||
$cityField.empty(); | ||
$cityField.append('<option value="">Selecione sua cidade</option>'); | ||
$.each(data, (index, value) => { | ||
$cityField.append( | ||
'<option value="' + value.code + '">' + value.name + '</option>' | ||
); | ||
}); | ||
}); | ||
}); | ||
} | ||
}); | ||
}(window.jQuery)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from django.conf import settings | ||
import csv | ||
|
||
def get_states(column_label="Nome_UF"): | ||
csv_filename = settings.BASE_DIR / "votepeloclima/candidature/csv/places.csv" | ||
states = set() | ||
with open(csv_filename) as f: | ||
reader = csv.DictReader(f) | ||
reader.fieldnames = [field.strip() for field in reader.fieldnames] | ||
for row in reader: | ||
uf = row["UF"].strip() | ||
state_name = row[column_label].strip() | ||
states.add((uf, state_name)) | ||
return sorted(list(states), key=lambda x: x[1]) | ||
|
||
def get_ufs(): | ||
return get_states(column_label="Nome_UF") | ||
|
||
def get_choices(uf): | ||
csv_filename = settings.BASE_DIR / "votepeloclima/candidature/csv/places.csv" | ||
choices = set() | ||
with open(csv_filename) as f: | ||
reader = csv.DictReader(f) | ||
reader.fieldnames = [field.strip() for field in reader.fieldnames] | ||
for row in reader: | ||
if row["UF"].strip() == uf: | ||
city_code = row["Código Município Completo"].strip() | ||
city_name = row["Nome_Município"].strip() | ||
choices.append((city_code, city_name)) | ||
return sorted(list(choices), key=lambda x: x[1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters