Commits

Mikhail Korobov committed 0d94990

Закоммитил то, что есть

Comments (0)

Files changed (16)

+syntax: glob
+
+#projects
+.settings/*
+.project
+.pydevproject
+.externalToolBuilders*
+.cache/*
+nbproject/*
+.buildpath
+build.properties
+MANIFEST.MF
+
+#temp files
+*.pyc
+*.pyo
+*.orig
+*~
+stuff/
+
+#os files
+.DS_Store
+Thumbs.db
+Copyright (c) 2009 Mikhail Korobov
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+include photo_albums/locale/en/LC_MESSAGES/*
+include photo_albums/locale/ru/LC_MESSAGES/*
+Простейшее джанго-приложение для работы с яндекс-картами.
+Не умеет почти ничего.
+Лицензия MIT.
+
+Для работы в settings.py нужно добавить переменную YANDEX_MAPS_API_KEY
+со своим ключом API от яндекса. Для работы нужна также библиотека BeautifulSoup.
+#!/usr/bin/env python
+from distutils.core import setup
+
+setup(
+      name='django-yandex-maps',
+      version='0.1',
+      author='Mikhail Korobov',
+      author_email='kmike84@gmail.com',
+      url='http://bitbucket.org/kmike/django-yandex-maps/',
+
+      description = 'Django app for work with Yandex Maps service.',
+      license = 'MIT license',
+      packages=['yandex_maps'],
+
+      package_data={'yandex_maps': ['templates/yandex_maps/*']},
+
+      requires = ['django (>=1.0)', 'BeautifulSoup'],
+
+      classifiers=[
+          'Development Status :: 3 - Alpha',
+          'Environment :: Plugins',
+          'Framework :: Django',
+          'Intended Audience :: Developers',
+          'License :: OSI Approved :: MIT License',
+          'Natural Language :: English',
+          'Natural Language :: Russian',
+          'Programming Language :: Python',
+          'Topic :: Software Development :: Libraries :: Python Modules'
+      ],
+)

yandex_maps/__init__.py

Empty file added.

yandex_maps/admin.py

+from django.contrib import admin
+#from yandex_maps.models import UserAddress
+
+#class YandexAdmin(admin.ModelAdmin):
+
+#admin.site.register(UserAddress)

yandex_maps/fixtures/yandex_maps.json

+[
+    {
+        "pk": 1, 
+        "model": "yandex_maps.mapandaddress", 
+        "fields": {
+            "latitude": 56.834544999999999, 
+            "longtitude": 60.611083999999998, 
+            "address": "\u0420\u043e\u0441\u0441\u0438\u044f, \u0421\u0432\u0435\u0440\u0434\u043b\u043e\u0432\u0441\u043a\u0430\u044f \u043e\u0431\u043b., \u0415\u043a\u0430\u0442\u0435\u0440\u0438\u043d\u0431\u0443\u0440\u0433, \u0443\u043b \u041c\u0430\u043b\u044b\u0448\u0435\u0432\u0430, 43"
+        }
+    }, 
+    {
+        "pk": 2, 
+        "model": "yandex_maps.mapandaddress", 
+        "fields": {
+            "latitude": 55.768718999999997, 
+            "longtitude": 37.597422999999999, 
+            "address": "\u0420\u043e\u0441\u0441\u0438\u044f, \u041c\u043e\u0441\u043a\u0432\u0430 \u0438 \u041c\u043e\u0441\u043a\u043e\u0432\u0441\u043a\u0430\u044f \u043e\u0431\u043b., \u041c\u043e\u0441\u043a\u0432\u0430, \u0422\u0432\u0435\u0440\u0441\u043a\u0430\u044f, \u0434\u043e\u043c 27, \u043a\u0432. 25"
+        }
+    }, 
+    {
+        "pk": 3, 
+        "model": "yandex_maps.mapandaddress", 
+        "fields": {
+            "latitude": 56.85371, 
+            "longtitude": 60.604624999999999, 
+            "address": "\u0420\u043e\u0441\u0441\u0438\u044f, \u0421\u0432\u0435\u0440\u0434\u043b\u043e\u0432\u0441\u043a\u0430\u044f \u043e\u0431\u043b., \u0415\u043a\u0430\u0442\u0435\u0440\u0438\u043d\u0431\u0443\u0440\u0433, \u0421\u0432\u0435\u0440\u0434\u043b\u043e\u0432\u0430, \u0434. 27"
+        }
+    }, 
+    {
+        "pk": 9, 
+        "model": "yandex_maps.mapandaddress", 
+        "fields": {
+            "latitude": 56.854581000000003, 
+            "longtitude": 60.603825999999998, 
+            "address": "\u0420\u043e\u0441\u0441\u0438\u044f, \u0421\u0432\u0435\u0440\u0434\u043b\u043e\u0432\u0441\u043a\u0430\u044f \u043e\u0431\u043b., \u0415\u043a\u0430\u0442\u0435\u0440\u0438\u043d\u0431\u0443\u0440\u0433, \u0421\u0432\u0435\u0440\u0434\u043b\u043e\u0432\u0430, \u0434. 27"
+        }
+    }, 
+    {
+        "pk": 10, 
+        "model": "yandex_maps.mapandaddress", 
+        "fields": {
+            "latitude": 56.832141999999997, 
+            "longtitude": 60.587854, 
+            "address": "\u0420\u043e\u0441\u0441\u0438\u044f, \u0421\u0432\u0435\u0440\u0434\u043b\u043e\u0432\u0441\u043a\u0430\u044f \u043e\u0431\u043b., \u0415\u043a\u0430\u0442\u0435\u0440\u0438\u043d\u0431\u0443\u0440\u0433, \u041c\u0430\u043b\u044b\u0448\u0435\u0432\u0430, 12"
+        }
+    }
+]

yandex_maps/migrations/0001_initial.py

+#coding: utf-8
+
+from south.db import db
+from django.db import models
+from yandex_maps.models import *
+
+class Migration:
+    
+    def forwards(self, orm):
+        
+        # Adding model 'MapAndAddress'
+        db.create_table('yandex_maps_mapandaddress', (
+            ('id', orm['yandex_maps.MapAndAddress:id']),
+            ('address', orm['yandex_maps.MapAndAddress:address']),
+            ('longtitude', orm['yandex_maps.MapAndAddress:longtitude']),
+            ('latitude', orm['yandex_maps.MapAndAddress:latitude']),
+        ))
+        db.send_create_signal('yandex_maps', ['MapAndAddress'])
+        
+    
+    
+    def backwards(self, orm):
+        
+        # Deleting model 'MapAndAddress'
+        db.delete_table('yandex_maps_mapandaddress')
+        
+    
+    
+    models = {
+        'yandex_maps.mapandaddress': {
+            'address': ('django.db.models.fields.CharField', [], {'max_length': '300', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'latitude': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}),
+            'longtitude': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'})
+        }
+    }
+    
+    complete_apps = ['yandex_maps']

yandex_maps/migrations/__init__.py

Empty file added.

yandex_maps/models.py

+#coding: utf-8
+from django.db import models
+from django.conf import settings
+from django.utils.encoding import smart_str
+
+from yandex_maps import utils
+
+YANDEX_KEY = getattr(settings, 'YANDEX_MAPS_API_KEY', None)
+
+class MapAndAddress(models.Model):
+
+    address = models.CharField(u'Адрес', max_length=300, blank=True)
+    longtitude = models.FloatField(u'Долгота', null=True, blank=True)
+    latitude = models.FloatField(u'Широта', null=True, blank=True)
+
+    def get_detail_level(self):
+        return 5
+
+    def get_map_url(self, width=None, height=None, detail_level = 5):
+        w = width if width else settings.YANDEX_MAPS_W
+        h = height if height else settings.YANDEX_MAPS_H
+        if YANDEX_KEY is not None:
+            return utils.get_map_url(YANDEX_KEY, self.longtitude, self.latitude, self.get_detail_level(), w, h)
+        else:
+            return ''
+
+    def fill_geocode_data(self):
+        if YANDEX_KEY is not None:
+            (self.longtitude, self.latitude) = utils.geocode(settings.YANDEX_MAPS_API_KEY, smart_str(self.address))
+
+    def save(self, *args, **kwargs):
+        if self.pk or (self.longtitude is None) or (self.latitude is None): # don't fill geocode data if it is known already
+            self.fill_geocode_data()
+        super(MapAndAddress, self).save(*args, **kwargs)
+
+    def __unicode__(self):
+        return self.address
+

yandex_maps/templates/yandex_maps/map.html

+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+    <head>
+        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+        <title>{{ address }}</title>
+    </head>
+    <body>
+        <div id="YMapsID" style="width:600px;height:600px"></div>
+        <script src="http://api-maps.yandex.ru/1.1/index.xml?key={{ api_key }}" type="text/javascript"></script>
+        <script type="text/javascript">
+            window.onload = function () {
+                var map = new YMaps.Map(document.getElementById("YMapsID"));
+                var map_center = new YMaps.GeoPoint({{ longtitude }}, {{ latitude }});
+                map.setCenter(map_center, {{ detail }});
+                map.openBalloon(map_center, "{{ address }}");
+
+                map.addControl(new YMaps.ToolBar());
+                map.addControl(new YMaps.Zoom());
+                map.addControl(new YMaps.ScaleLine());
+            }
+        </script>
+    </body>
+</html>

yandex_maps/tests.py

+#coding: utf-8
+from generic_utils.test_helpers import ViewTest
+
+class YandexMapTest(ViewTest):
+    
+    fixtures=['yandex_maps']
+
+    def test_views(self):
+        self.check_url('yandex_map', kwargs={'map_id': 1})
+        self.check_url('yandex_map', 404, kwargs={'map_id': 4})
+        

yandex_maps/urls.py

+from django.conf.urls.defaults import *
+
+urlpatterns = patterns('yandex_maps.views',
+                           url(
+                                r'^map/(?P<map_id>\d+)/$',
+                                'yandex_map',
+                                name='yandex_map'
+                           ),
+                       )

yandex_maps/utils.py

+#coding: utf-8
+
+from BeautifulSoup import BeautifulSoup
+import urllib
+from django.utils.http import urlencode
+
+def get_map_url(API_key, longtitude, latitude, zoom, width, height):
+    url = u"http://static-maps.yandex.ru/1.x/?ll=%0.7f,%0.7f&size=%d,%d&z=%d&l=map&pt=%0.7f,%0.7f&key=%s" % \
+          (longtitude, latitude, width, height, zoom, longtitude, latitude, API_key)
+    return url
+
+def geocode(API_key, address):
+    url = u'http://geocode-maps.yandex.ru/1.x/?'
+    params = urlencode({'geocode':address,'key':API_key})
+
+    try:
+        response = urllib.urlopen(url+params).read()
+    except IOError:
+        return (None, None,)
+
+    try:
+        data = BeautifulSoup(response).find('pos').string.split()
+    except AttributeError:
+        data = (None,None,)
+    return data

yandex_maps/views.py

+from django.shortcuts import get_object_or_404
+from django.views.generic.simple import direct_to_template
+
+from django.conf import settings
+from yandex_maps.models import MapAndAddress
+
+def yandex_map(request, map_id):
+    map = get_object_or_404(MapAndAddress, id=map_id)
+    api_key = settings.YANDEX_MAPS_API_KEY
+    return direct_to_template(request, 'yandex_maps/map.html',
+                              {
+                                 'longtitude': map.longtitude,
+                                 'latitude': map.latitude,
+                                 'api_key': api_key,
+                                 'detail': 15,
+                                 'address' : map.address,
+                               })