-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Paul Maigutyak
committed
Jan 30, 2017
1 parent
e880596
commit 4aa7f1b
Showing
18 changed files
with
342 additions
and
0 deletions.
There are no files selected for viewing
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,14 @@ | ||
.idea | ||
*.pyc | ||
*.bak | ||
*.bat | ||
.pydevproject | ||
.project | ||
.settings | ||
.directory | ||
*.fcgi | ||
.DS_Store | ||
storage.db | ||
*.pem | ||
*.crt | ||
media |
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,11 @@ | ||
|
||
from django.apps import AppConfig | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
|
||
class NovaPoshtaAppConfig(AppConfig): | ||
name = 'novaposhta' | ||
verbose_name = _('Nova poshta') | ||
|
||
|
||
default_app_config = 'novaposhta.NovaPoshtaAppConfig' |
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,11 @@ | ||
|
||
from django.contrib import admin | ||
|
||
from novaposhta.models import Warehouse | ||
|
||
|
||
@admin.register(Warehouse) | ||
class WarehouseAdmin(admin.ModelAdmin): | ||
|
||
list_display = ('title', 'address', ) | ||
search_fields = ('title', 'address', ) |
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,49 @@ | ||
|
||
import httplib | ||
import json | ||
|
||
from novaposhta.settings import NOVA_POSHTA_API_KEY | ||
from novaposhta.models import Warehouse | ||
|
||
|
||
def refresh_warehouses(): | ||
|
||
api_domain = 'api.novaposhta.ua' | ||
|
||
method = 'POST' | ||
|
||
url = '/v2.0/json/AddressGeneral/getWarehouses' | ||
|
||
headers = { | ||
'Content-Type': 'application/json' | ||
} | ||
|
||
body = json.dumps({ | ||
'modelName': 'AddressGeneral', | ||
'calledMethod': 'getWarehouses', | ||
'apiKey': NOVA_POSHTA_API_KEY | ||
}) | ||
|
||
try: | ||
conn = httplib.HTTPConnection(api_domain) | ||
|
||
conn.request(method, url, body, headers) | ||
|
||
response_data = json.loads(conn.getresponse().read()) | ||
|
||
if response_data.get('success') is not True: | ||
raise Exception | ||
|
||
Warehouse.objects.all().delete() | ||
|
||
for item in response_data.get('data'): | ||
Warehouse.objects.create( | ||
title_uk=item.get('Description'), | ||
title_ru=item.get('DescriptionRu'), | ||
address_uk=item.get('CityDescription'), | ||
address_ru=item.get('CityDescriptionRu'), | ||
) | ||
|
||
conn.close() | ||
except Exception as e: | ||
print(e) |
Binary file not shown.
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,38 @@ | ||
# SOME DESCRIPTIVE TITLE. | ||
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER | ||
# This file is distributed under the same license as the PACKAGE package. | ||
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. | ||
# | ||
#, fuzzy | ||
msgid "" | ||
msgstr "" | ||
"Project-Id-Version: PACKAGE VERSION\n" | ||
"Report-Msgid-Bugs-To: \n" | ||
"POT-Creation-Date: 2017-01-30 17:22+0200\n" | ||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" | ||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | ||
"Language-Team: LANGUAGE <[email protected]>\n" | ||
"Language: \n" | ||
"MIME-Version: 1.0\n" | ||
"Content-Type: text/plain; charset=UTF-8\n" | ||
"Content-Transfer-Encoding: 8bit\n" | ||
|
||
#: __init__.py:8 | ||
msgid "Nova poshta" | ||
msgstr "" | ||
|
||
#: models.py:8 | ||
msgid "Title" | ||
msgstr "" | ||
|
||
#: models.py:10 | ||
msgid "Address" | ||
msgstr "" | ||
|
||
#: models.py:22 | ||
msgid "Warehouse" | ||
msgstr "" | ||
|
||
#: models.py:23 | ||
msgid "Warehouses" | ||
msgstr "" |
Binary file not shown.
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,40 @@ | ||
# SOME DESCRIPTIVE TITLE. | ||
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER | ||
# This file is distributed under the same license as the PACKAGE package. | ||
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. | ||
# | ||
#, fuzzy | ||
msgid "" | ||
msgstr "" | ||
"Project-Id-Version: PACKAGE VERSION\n" | ||
"Report-Msgid-Bugs-To: \n" | ||
"POT-Creation-Date: 2017-01-30 17:22+0200\n" | ||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" | ||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | ||
"Language-Team: LANGUAGE <[email protected]>\n" | ||
"Language: \n" | ||
"MIME-Version: 1.0\n" | ||
"Content-Type: text/plain; charset=UTF-8\n" | ||
"Content-Transfer-Encoding: 8bit\n" | ||
"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n" | ||
"%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n" | ||
|
||
#: __init__.py:8 | ||
msgid "Nova poshta" | ||
msgstr "" | ||
|
||
#: models.py:8 | ||
msgid "Title" | ||
msgstr "" | ||
|
||
#: models.py:10 | ||
msgid "Address" | ||
msgstr "" | ||
|
||
#: models.py:22 | ||
msgid "Warehouse" | ||
msgstr "" | ||
|
||
#: models.py:23 | ||
msgid "Warehouses" | ||
msgstr "" |
Binary file not shown.
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,40 @@ | ||
# SOME DESCRIPTIVE TITLE. | ||
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER | ||
# This file is distributed under the same license as the PACKAGE package. | ||
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. | ||
# | ||
#, fuzzy | ||
msgid "" | ||
msgstr "" | ||
"Project-Id-Version: PACKAGE VERSION\n" | ||
"Report-Msgid-Bugs-To: \n" | ||
"POT-Creation-Date: 2017-01-30 17:22+0200\n" | ||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" | ||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | ||
"Language-Team: LANGUAGE <[email protected]>\n" | ||
"Language: \n" | ||
"MIME-Version: 1.0\n" | ||
"Content-Type: text/plain; charset=UTF-8\n" | ||
"Content-Transfer-Encoding: 8bit\n" | ||
"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n" | ||
"%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n" | ||
|
||
#: __init__.py:8 | ||
msgid "Nova poshta" | ||
msgstr "Нова пошта" | ||
|
||
#: models.py:8 | ||
msgid "Title" | ||
msgstr "Назва" | ||
|
||
#: models.py:10 | ||
msgid "Address" | ||
msgstr "Адреса" | ||
|
||
#: models.py:22 | ||
msgid "Warehouse" | ||
msgstr "Відділ Нової пошти" | ||
|
||
#: models.py:23 | ||
msgid "Warehouses" | ||
msgstr "Відділи Нової пошти" |
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,28 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.9.6 on 2016-10-26 09:35 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Warehouse', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('title', models.CharField(db_index=True, max_length=255, verbose_name='Title')), | ||
('address', models.CharField(db_index=True, max_length=255, verbose_name='Address')), | ||
], | ||
options={ | ||
'verbose_name': 'Warehouse', | ||
'verbose_name_plural': 'Warehouses', | ||
}, | ||
), | ||
] |
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,38 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.9.6 on 2017-01-16 16:05 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
from django.conf import settings | ||
|
||
|
||
def generate_operations(): | ||
|
||
operations = [] | ||
|
||
for code, name in settings.LANGUAGES: | ||
|
||
operations += [ | ||
|
||
migrations.AddField( | ||
model_name='warehouse', | ||
name='address_%s' % code, | ||
field=models.CharField(db_index=True, max_length=255, null=True, verbose_name='Address'), | ||
), | ||
migrations.AddField( | ||
model_name='warehouse', | ||
name='title_%s' % code, | ||
field=models.CharField(db_index=True, max_length=255, null=True, verbose_name='Title'), | ||
), | ||
|
||
] | ||
|
||
return operations | ||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('nova_poshta', '0001_initial'), | ||
] | ||
|
||
operations = generate_operations() |
Empty file.
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,23 @@ | ||
|
||
from django.db import models | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
|
||
class Warehouse(models.Model): | ||
|
||
title = models.CharField(_('Title'), max_length=255, db_index=True) | ||
|
||
address = models.CharField(_('Address'), max_length=255, db_index=True) | ||
|
||
search_fields = ['title', 'address'] | ||
|
||
@property | ||
def full_name(self): | ||
return '%s, %s' % (self.title, self.address) | ||
|
||
def __unicode__(self): | ||
return self.full_name | ||
|
||
class Meta: | ||
verbose_name = _('Warehouse') | ||
verbose_name_plural = _('Warehouses') |
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,5 @@ | ||
|
||
from django.conf import settings | ||
|
||
|
||
NOVA_POSHTA_API_KEY = getattr(settings, 'NOVA_POSHTA_API_KEY', '') |
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,10 @@ | ||
|
||
from modeltranslation.translator import register, TranslationOptions | ||
|
||
from novaposhta.models import Warehouse | ||
|
||
|
||
@register(Warehouse) | ||
class WarehouseTranslationOptions(TranslationOptions): | ||
|
||
fields = ('title', 'address', ) |
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,11 @@ | ||
|
||
from django.conf.urls import url | ||
|
||
from novaposhta import views | ||
|
||
|
||
urlpatterns = [ | ||
|
||
url(r'^autocomplete', views.autocomplete, name='autocomplete') | ||
|
||
] |
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,24 @@ | ||
|
||
from django.http import JsonResponse | ||
|
||
from mpsite.search import model_search | ||
|
||
from novaposhta.models import Warehouse | ||
|
||
|
||
def autocomplete(request): | ||
|
||
query = request.GET.get('query') | ||
|
||
warehouses = Warehouse.objects.all() | ||
|
||
suggestions = [] | ||
|
||
for warehouse in model_search(query, warehouses, Warehouse.search_fields): | ||
if warehouse.full_name not in suggestions: | ||
suggestions.append(warehouse.full_name) | ||
|
||
return JsonResponse({ | ||
'query': query, | ||
'suggestions': suggestions[:10] | ||
}) |