Skip to content

Commit

Permalink
Initialize app.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Maigutyak committed Jan 30, 2017
1 parent e880596 commit 4aa7f1b
Show file tree
Hide file tree
Showing 18 changed files with 342 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
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
11 changes: 11 additions & 0 deletions novaposhta/__init__.py
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'
11 changes: 11 additions & 0 deletions novaposhta/admin.py
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', )
49 changes: 49 additions & 0 deletions novaposhta/lib.py
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 added novaposhta/locale/en/LC_MESSAGES/django.mo
Binary file not shown.
38 changes: 38 additions & 0 deletions novaposhta/locale/en/LC_MESSAGES/django.po
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 added novaposhta/locale/ru/LC_MESSAGES/django.mo
Binary file not shown.
40 changes: 40 additions & 0 deletions novaposhta/locale/ru/LC_MESSAGES/django.po
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 added novaposhta/locale/uk/LC_MESSAGES/django.mo
Binary file not shown.
40 changes: 40 additions & 0 deletions novaposhta/locale/uk/LC_MESSAGES/django.po
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 "Відділи Нової пошти"
28 changes: 28 additions & 0 deletions novaposhta/migrations/0001_initial.py
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',
},
),
]
38 changes: 38 additions & 0 deletions novaposhta/migrations/0002_model_translation.py
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.
23 changes: 23 additions & 0 deletions novaposhta/models.py
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')
5 changes: 5 additions & 0 deletions novaposhta/settings.py
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', '')
10 changes: 10 additions & 0 deletions novaposhta/translation.py
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', )
11 changes: 11 additions & 0 deletions novaposhta/urls.py
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')

]
24 changes: 24 additions & 0 deletions novaposhta/views.py
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]
})

0 comments on commit 4aa7f1b

Please sign in to comment.