# Path: owntracks/models.py
# class OwnTrackLog(models.Model):
#     tid = models.CharField(max_length=100, null=False, verbose_name='用户')
#     lat = models.FloatField(verbose_name='纬度')
#     lon = models.FloatField(verbose_name='经度')
#     created_time = models.DateTimeField('创建时间', default=now)
# 
#     def __str__(self):
#         return self.tid
# 
#     class Meta:
#         ordering = ['created_time']
#         verbose_name = "OwnTrackLogs"
#         verbose_name_plural = verbose_name
#         get_latest_by = 'created_time'


import datetime
import itertools
import json
import logging
import requests
    import django.utils.timezone
from itertools import groupby
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.http import JsonResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from .models import OwnTrackLog
        from django.http import HttpResponseForbidden
    from django.utils.timezone import utc


logger = logging.getLogger(__name__)


@csrf_exempt
def manage_owntrack_log(request):
    try:
        s = json.loads(request.read().decode('utf-8'))
        tid = s['tid']
        lat = s['lat']
        lon = s['lon']

        logger.info(
            'tid:{tid}.lat:{lat}.lon:{lon}'.format(
                tid=tid, lat=lat, lon=lon))
        if tid and lat and lon:
            m = OwnTrackLog()
            m.tid = tid
            m.lat = lat
            m.lon = lon
            m.save()
            return HttpResponse('ok')
        else:
            return HttpResponse('data error')
    except Exception as e:
        logger.error(e)
        return HttpResponse('error')



# Path: owntracks/models.py
# class OwnTrackLog(models.Model):
#     tid = models.CharField(max_length=100, null=False, verbose_name='用户')
#     lat = models.FloatField(verbose_name='纬度')
#     lon = models.FloatField(verbose_name='经度')
#     created_time = models.DateTimeField('创建时间', default=now)
# 
#     def __str__(self):
#         return self.tid
# 
#     class Meta:
#         ordering = ['created_time']
#         verbose_name = "OwnTrackLogs"
#         verbose_name_plural = verbose_name
#         get_latest_by = 'created_time'


# Path: owntracks/views.py
import datetime
import itertools
import json
import logging
import requests
    import django.utils.timezone
from itertools import groupby
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.http import JsonResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from .models import OwnTrackLog
        from django.http import HttpResponseForbidden
    from django.utils.timezone import utc



logger = logging.getLogger(__name__)


@csrf_exempt
def manage_owntrack_log(request):
    try:
        s = json.loads(request.read().decode('utf-8'))
        tid = s['tid']
        lat = s['lat']
        lon = s['lon']

        logger.info(
            'tid:{tid}.lat:{lat}.lon:{lon}'.format(
                tid=tid, lat=lat, lon=lon))
        if tid and lat and lon:
            m = OwnTrackLog()
            m.tid = tid
            m.lat = lat
            m.lon = lon
            m.save()
            return HttpResponse('ok')
        else:
            return HttpResponse('data error')
    except Exception as e:
        logger.error(e)
        return HttpResponse('error')



@login_required