Skip to content

Commit a10e227

Browse files
committed
增加记录owntracks经纬度信息功能🤓🤓
1 parent d8eb0f2 commit a10e227

File tree

17 files changed

+299
-11
lines changed

17 files changed

+299
-11
lines changed

DjangoBlog/admin_site.py

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from oauth.admin import *
2020
from servermanager.admin import *
2121
from comments.admin import *
22+
from owntracks.admin import *
2223

2324

2425
class DjangoBlogAdminSite(AdminSite):
@@ -57,3 +58,5 @@ def has_permission(self, request):
5758
admin_site.register(Comment, CommentAdmin)
5859

5960
admin_site.register(OAuthUser, OAuthUserAdmin)
61+
62+
admin_site.register(OwnTrackLog, OwnTrackLogsAdmin)

DjangoBlog/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
'comments',
4848
'oauth',
4949
'servermanager',
50+
'owntracks',
5051
'compressor'
5152
]
5253

DjangoBlog/urls.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@
4646
name='django.contrib.sitemaps.views.sitemap'),
4747
url(r'^feed/$', DjangoBlogFeed()),
4848
url(r'^search', include('haystack.urls'), name='search'),
49-
url(r'', include('servermanager.urls', namespace='servermanager'))
49+
url(r'', include('servermanager.urls', namespace='servermanager')),
50+
url(r'', include('owntracks.urls', namespace='owntracks'))
5051
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

accounts/models.py

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
# Create your models here.
99

1010
class BlogUser(AbstractUser):
11-
nickname = models.CharField('昵称', max_length=50, blank=True)
1211
nickname = models.CharField('昵称', max_length=100, blank=True)
1312
mugshot = models.ImageField('头像', upload_to='upload/mugshots', blank=True)
1413
created_time = models.DateTimeField('创建时间', default=now)

owntracks/__init__.py

Whitespace-only changes.

owntracks/admin.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.
4+
from .models import OwnTrackLog
5+
6+
7+
class OwnTrackLogsAdmin(admin.ModelAdmin):
8+
pass

owntracks/apps.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class OwntracksConfig(AppConfig):
5+
name = 'owntracks'

owntracks/migrations/__init__.py

Whitespace-only changes.

owntracks/models.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from django.db import models
2+
from django.utils.timezone import now
3+
4+
5+
# Create your models here.
6+
7+
class OwnTrackLog(models.Model):
8+
tid = models.CharField(max_length=100, null=False, verbose_name='用户')
9+
lat = models.FloatField(verbose_name='纬度')
10+
lon = models.FloatField(verbose_name='经度')
11+
created_time = models.DateTimeField('创建时间', default=now)
12+
13+
def __str__(self):
14+
return self.tid
15+
16+
class Meta:
17+
ordering = ['created_time']
18+
verbose_name = "OwnTrackLogs"
19+
verbose_name_plural = verbose_name
20+
get_latest_by = 'created_time'

owntracks/tests.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from django.test import Client, RequestFactory, TestCase
2+
from .models import OwnTrackLog
3+
from accounts.models import BlogUser
4+
import json
5+
6+
7+
# Create your tests here.
8+
9+
class OwnTrackLogTest(TestCase):
10+
def setUp(self):
11+
self.client = Client()
12+
self.factory = RequestFactory()
13+
14+
def test_own_track_log(self):
15+
user = BlogUser.objects.create_superuser(email="liangliangyy1@gmail.com",
16+
username="liangliangyy1", password="liangliangyy1")
17+
18+
self.client.login(username='liangliangyy1', password='liangliangyy1')
19+
s = OwnTrackLog()
20+
s.tid = 12
21+
s.lon = 123.234
22+
s.lat = 34.234
23+
s.save()
24+
rsp = self.client.get('/owntracks/show_maps')
25+
self.assertEqual(rsp.status_code, 200)
26+
rsp = self.client.get('/owntracks/get_datas')
27+
self.assertEqual(rsp.status_code, 200)

owntracks/urls.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
5+
"""
6+
@version: ??
7+
@author: liangliangyy
8+
@license: MIT Licence
9+
@contact: liangliangyy@gmail.com
10+
@site: https://www.lylinux.net/
11+
@software: PyCharm
12+
@file: urls.py
13+
@time: 2018/2/25 下午3:04
14+
"""
15+
from django.urls import path
16+
from . import views
17+
18+
app_name = "owntracks"
19+
20+
urlpatterns = [
21+
path('owntracks/logtracks', views.manage_owntrack_log),
22+
path('owntracks/show_maps', views.show_maps),
23+
path('owntracks/get_datas', views.get_datas)
24+
]
25+
26+
# http://home.lylinux.net:2213/owntracks/logtracks

owntracks/views.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from django.shortcuts import render
2+
3+
# Create your views here.
4+
import json
5+
from itertools import groupby
6+
from django.http import HttpResponse
7+
from .models import OwnTrackLog
8+
from DjangoBlog.utils import logger
9+
from django.shortcuts import render
10+
from django.http import JsonResponse
11+
from django.contrib.auth.decorators import login_required
12+
from django.views.decorators.csrf import csrf_exempt
13+
14+
15+
@csrf_exempt
16+
def manage_owntrack_log(request):
17+
try:
18+
s = json.loads(request.body)
19+
tid = s['tid']
20+
lat = s['lat']
21+
lon = s['lon']
22+
logger.info('tid:{tid}.lat:{lat}.lon:{lon}'.format(tid=tid, lat=lat, lon=lon))
23+
if tid and lat and lon:
24+
m = OwnTrackLog()
25+
m.tid = tid
26+
m.lat = lat
27+
m.lon = lon
28+
m.save()
29+
return HttpResponse('ok')
30+
else:
31+
return HttpResponse('data error')
32+
except Exception as e:
33+
logger.warn(e)
34+
return HttpResponse('error')
35+
36+
37+
@login_required
38+
def show_maps(request):
39+
return render(request, 'owntracks/show_maps.html')
40+
41+
42+
@login_required
43+
def get_datas(request):
44+
models = OwnTrackLog.objects.all()
45+
result = list()
46+
if models and len(models):
47+
for tid, item in groupby(sorted(models, key=lambda k: k.tid), key=lambda k: k.tid):
48+
49+
d = dict()
50+
d["name"] = tid
51+
paths = list()
52+
for i in item:
53+
path = list()
54+
path.append(i.lon)
55+
path.append(i.lat)
56+
paths.append(path)
57+
d["path"] = paths
58+
result.append(d)
59+
return JsonResponse(result, safe=False)

requirements.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ appdirs==1.4.3
22
bottle==0.12.13
33
certifi==2018.1.18
44
chardet==3.0.4
5-
coverage==4.5
5+
coverage==4.5.1
66
Django==2.0.2
77
django-appconf==1.0.2
88
django-autoslug==1.9.3
99
django-compressor==2.2
1010
django-debug-toolbar==1.9.1
11-
django-haystack==2.6.1
11+
django-haystack==2.7.0
1212
django-ipware==2.0.1
1313
django-pagedown==1.0.4
1414
django-uuslug==1.1.8
1515
idna==2.6
1616
jieba==0.39
17-
jsonpickle==0.9.5
17+
jsonpickle==0.9.6
1818
markdown2==2.3.5
1919
mistune==0.8.3
2020
olefile==0.45.1
@@ -25,7 +25,7 @@ PyMySQL==0.8.0
2525
pyparsing==2.2.0
2626
python-memcached==1.59
2727
python-slugify==1.2.4
28-
pytz==2017.3
28+
pytz==2018.3
2929
rcssmin==1.0.6
3030
requests==2.18.4
3131
rjsmin==1.0.12

servermanager/admin.py

+3
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55

66
class CommandsAdmin(admin.ModelAdmin):
77
list_display = ('title', 'command', 'describe')
8+
9+
10+
admin.site.register(commands, CommandsAdmin)

templates/owntracks/show_maps.html

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<!doctype html>
2+
<head>
3+
<meta charset="utf-8">
4+
<meta http-equiv="X-UA-Compatible" content="chrome=1">
5+
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
6+
<style>
7+
html,
8+
body,
9+
#container {
10+
width: 100%;
11+
height: 100%;
12+
margin: 0px;
13+
}
14+
15+
#loadingTip {
16+
position: absolute;
17+
z-index: 9999;
18+
top: 0;
19+
left: 0;
20+
padding: 3px 10px;
21+
background: red;
22+
color: #fff;
23+
font-size: 14px;
24+
}
25+
</style>
26+
<title>快速入门</title>
27+
</head>
28+
29+
<body>
30+
<div id="container"></div>
31+
<script type="text/javascript" src='//webapi.amap.com/maps?v=1.4.4&key=9c89950bdfbcecd46f814309384655cd'></script>
32+
<!-- UI组件库 1.0 -->
33+
<script src="//webapi.amap.com/ui/1.0/main.js?v=1.0.11"></script>
34+
<script type="text/javascript">
35+
//创建地图
36+
var map = new AMap.Map('container', {
37+
zoom: 4
38+
});
39+
40+
AMapUI.load(['ui/misc/PathSimplifier', 'lib/$'], function (PathSimplifier, $) {
41+
42+
if (!PathSimplifier.supportCanvas) {
43+
alert('当前环境不支持 Canvas!');
44+
return;
45+
}
46+
47+
//just some colors
48+
var colors = [
49+
"#3366cc", "#dc3912", "#ff9900", "#109618", "#990099", "#0099c6", "#dd4477", "#66aa00",
50+
"#b82e2e", "#316395", "#994499", "#22aa99", "#aaaa11", "#6633cc", "#e67300", "#8b0707",
51+
"#651067", "#329262", "#5574a6", "#3b3eac"
52+
];
53+
54+
var pathSimplifierIns = new PathSimplifier({
55+
zIndex: 100,
56+
//autoSetFitView:false,
57+
map: map, //所属的地图实例
58+
59+
getPath: function (pathData, pathIndex) {
60+
61+
return pathData.path;
62+
},
63+
getHoverTitle: function (pathData, pathIndex, pointIndex) {
64+
65+
if (pointIndex >= 0) {
66+
//point
67+
return pathData.name + ',点:' + pointIndex + '/' + pathData.path.length;
68+
}
69+
70+
return pathData.name + ',点数量' + pathData.path.length;
71+
},
72+
renderOptions: {
73+
pathLineStyle: {
74+
dirArrowStyle: true
75+
},
76+
getPathStyle: function (pathItem, zoom) {
77+
78+
var color = colors[pathItem.pathIndex % colors.length],
79+
lineWidth = Math.round(4 * Math.pow(1.1, zoom - 3));
80+
81+
return {
82+
pathLineStyle: {
83+
strokeStyle: color,
84+
lineWidth: lineWidth
85+
},
86+
pathLineSelectedStyle: {
87+
lineWidth: lineWidth + 2
88+
},
89+
pathNavigatorStyle: {
90+
fillStyle: color
91+
}
92+
};
93+
}
94+
}
95+
});
96+
97+
window.pathSimplifierIns = pathSimplifierIns;
98+
99+
$('<div id="loadingTip">加载数据,请稍候...</div>').appendTo(document.body);
100+
101+
$.getJSON('/owntracks/get_datas', function (d) {
102+
103+
if (!d || !d.length) {
104+
$("#loadingTip").text("没有数据...")
105+
return;
106+
}
107+
$('#loadingTip').remove();
108+
pathSimplifierIns.setData(d);
109+
110+
//initRoutesContainer(d);
111+
112+
function onload() {
113+
pathSimplifierIns.renderLater();
114+
}
115+
116+
function onerror(e) {
117+
alert('图片加载失败!');
118+
}
119+
120+
d.forEach(function (item, index) {
121+
var navg1 = pathSimplifierIns.createPathNavigator(index, {
122+
loop: true,
123+
speed: 1000000,
124+
});
125+
126+
navg1.start();
127+
})
128+
129+
});
130+
});
131+
</script>
132+
</body>
133+
134+
135+
</html>

0 commit comments

Comments
 (0)