<commit_before>'''
Created on May 24, 2016

@author: ionut
'''


import datetime
import hashlib
import json
import logging
import os
import pyexiv2
import tornado.web
from PIL import Image as PilImage
from StringIO import StringIO

import cache
import database
import settings
import utils
from settings import STATUS as status


class BaseHandler(tornado.web.RequestHandler):
    
    def initialize(self):
        pass
    
    @tornado.web.asynchronous
    def get(self):
        self.render('base.html')
    
    def post(self):
        self.finish('POST not allowed') 


class MapHandler(BaseHandler):
    

    @tornado.web.asynchronous
    @tornado.gen.coroutine
    def get(self):
        status['busy'] += 1
        images = cache.get_value('geotagged_images')
        if not images:
            query = '''SELECT image.id, ihash, lat, lng, altitude, extract(epoch from moment) as moment, filename, size, make, model, orientation, path, width, height, image.description 
            FROM image LEFT OUTER JOIN camera on image.camera_id = camera.id
            WHERE lat != %s and lng != %s'''
            data = (0, 0)
            images = yield database.raw_query(query, data)
            cache.set_value('geotagged_images', images)

        self.render('map.html', images=images)
        status['busy'] -= 1
    
    
    def post(self):
        self.finish('POST not allowed')


class GeoHandler(BaseHandler):
    

    @tornado.web.asynchronous
    @tornado.gen.coroutine
    def get(self, op):
        
        if op == 'get_image_list':
            status['busy'] += 1
            start_dt = datetime.datetime.strptime(self.get_argument('start_filter', '2012-12-01'), '%Y-%m-%d')
            stop_dt = datetime.datetime.strptime(self.get_argument('stop_filter', '2015-12-01'), '%Y-%m-%d')
            query = '''SELECT image.id, ihash, extract(epoch from moment) as moment, filename, size, make, model, orientation, path, width, height, image.description 
            FROM image LEFT OUTER JOIN camera on image.camera_id = camera.id
            WHERE (lat is %s or lng is %s)
                AND moment between %s AND %s
            ORDER BY moment ASC
            LIMIT 30'''
            data = (None, None, start_dt, stop_dt)
            images = yield database.raw_query(query, data, self.on_images)
            self.finish(json.dumps(images))
            status['busy'] -= 1
            
        else:
            self.render('geotag.html')


    @tornado.web.asynchronous
    @tornado.gen.coroutine
    def post(self, op):
        if op == 'update_location':
            status['busy'] += 1
            ihash = self.get_argument('hash', 'unknown')
            iid = self.get_argument('id', 0)
            lat = self.get_argument('lat')
            lng = self.get_argument('lng')
            try:
                lat = float(lat)
                lng = float(lng)
            except:
                status['busy'] -= 1
                raise tornado.gen.Return(self.finish('lat and/or lng invalid'))
            
            query = '''UPDATE image set lat=%s, lng=%s WHERE id=%s and ihash=%s RETURNING id'''
            data = (lat, lng, iid, ihash)<commit_msg>remove database migration work callback leftover<commit_after>'''
Created on May 24, 2016

@author: ionut
'''


import datetime
import hashlib
import json
import logging
import os
import pyexiv2
import tornado.web
from PIL import Image as PilImage
from StringIO import StringIO

import cache
import database
import settings
import utils
from settings import STATUS as status


class BaseHandler(tornado.web.RequestHandler):
    
    def initialize(self):
        pass
    
    @tornado.web.asynchronous
    def get(self):
        self.render('base.html')
    
    def post(self):
        self.finish('POST not allowed') 


class MapHandler(BaseHandler):
    

    @tornado.web.asynchronous
    @tornado.gen.coroutine
    def get(self):
        status['busy'] += 1
        images = cache.get_value('geotagged_images')
        if not images:
            query = '''SELECT image.id, ihash, lat, lng, altitude, extract(epoch from moment) as moment, filename, size, make, model, orientation, path, width, height, image.description 
            FROM image LEFT OUTER JOIN camera on image.camera_id = camera.id
            WHERE lat != %s and lng != %s'''
            data = (0, 0)
            images = yield database.raw_query(query, data)
            cache.set_value('geotagged_images', images)

        self.render('map.html', images=images)
        status['busy'] -= 1
    
    
    def post(self):
        self.finish('POST not allowed')


class GeoHandler(BaseHandler):
    

    @tornado.web.asynchronous
    @tornado.gen.coroutine
    def get(self, op):
        
        if op == 'get_image_list':
            status['busy'] += 1
            start_dt = datetime.datetime.strptime(self.get_argument('start_filter', '2012-12-01'), '%Y-%m-%d')
            stop_dt = datetime.datetime.strptime(self.get_argument('stop_filter', '2015-12-01'), '%Y-%m-%d')
            query = '''SELECT image.id, ihash, extract(epoch from moment) as moment, filename, size, make, model, orientation, path, width, height, image.description 
            FROM image LEFT OUTER JOIN camera on image.camera_id = camera.id
            WHERE (lat is %s or lng is %s)
                AND moment between %s AND %s
            ORDER BY moment ASC
            LIMIT 30'''
            data = (None, None, start_dt, stop_dt)
            images = yield database.raw_query(query, data)
            self.finish(json.dumps(images))
            status['busy'] -= 1
            
        else:
            self.render('geotag.html')


    @tornado.web.asynchronous
    @tornado.gen.coroutine
    def post(self, op):
        if op == 'update_location':
            status['busy'] += 1
            ihash = self.get_argument('hash', 'unknown')
            iid = self.get_argument('id', 0)
            lat = self.get_argument('lat')
            lng = self.get_argument('lng')
            try:
                lat = float(lat)
                lng = float(lng)
            except:
                status['busy'] -= 1
                raise tornado.gen.Return(self.finish('lat and/or lng invalid'))
            
            query = '''UPDATE image set lat=%s, lng=%s WHERE id=%s and ihash=%s RETURNING id'''
            data = (lat, lng, iid, ihash)
{'fraction_non_alphanumeric': 0.11412447092020693, 'fraction_numerical': 0.010659978052986362, 'mean_word_length': 2.1552917903066273, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4347146', 'n_tokens_mistral': 1977, 'n_tokens_neox': 1877, 'n_words': 521}