예제 #1
0
def imagesync(request):
    image = Image.get_image_by_id(request.json_body['image']['id'])
    log_json = request.json_body['log']
    log = Log.get_log_by_id(log_json['id'])

    if interlink_only:
        return Response(json.dumps({'log_id':log.id,'type':'image',  'item_uuid':image.uuid, 'sync_status':'was_synced'}))
    else:
        if not image.trackpoint: #find trackpoint for image if there was none yet
            trackpoint=gpxtools.sync_image_trackpoint(image)
            if trackpoint:
                image.trackpoint = trackpoint.id
                DBSession.add(image)
                DBSession.flush()
        headers = {'content-type':'application/json'}
        url = 'http://poab.org:6544/sync?type=status'
        payload = {'payloadtype':'image', 'image_json':json.dumps(image.reprJSON()), 'log_json':json.dumps(log.reprJSON())}
        remote_sync_info = requests.post(url, data=payload)
        print remote_sync_info.text
        sync_status=json.loads(remote_sync_info.text)['sync_status'] #if sync_status is 'was_synced', we already have this image on the server
        print '\n################ IMAGE SYNC STATUS: '+sync_status+str(log.id) + '\n'

        if sync_status == 'not_synced':
            image_bin = open(image.location+image.name, 'rb')
            #image_bin = ''
            url = 'http://poab.org:6544/sync?type=image'
            payload = {'image_json':json.dumps(image.reprJSON_extended()), 'image_bin':image_bin, 'log':json.dumps(log.reprJSON())}
            headers = {'content-type':'application/json'}
            remote_sync_info = requests.post(url, files=payload)

        return Response(remote_sync_info.text)
예제 #2
0
def delete_image(request):
    image_json = request.json_body
    image_id = image_json['id']
    image = Image.get_image_by_id(image_id)
    print image.id
    DBSession.delete(image)
    DBSession.flush()
    return Response(image.name)
예제 #3
0
def delete_log(request):
    log_json = request.json_body
    log_id = log_json['id']
    log = Log.get_log_by_id(log_id)
    print log.id
    DBSession.delete(log)
    DBSession.flush()
    return Response(log.topic)
예제 #4
0
def imageupload(request):
    filelist = request.POST.getall('uploadedFile')
    upload = request.POST.get('upload')
    print request.POST.get('upload')
    print request.POST.keys()
    print filelist
    
    owner = authenticated_userid(request)
    author = Author.get_author(owner)
    images_in_db = Image.get_images()
    today=strftime("%Y-%m-%d")
    
    basedir = '/srv/trackdata/bydate'
    img_large_w='990' #width of images in editor-preview
    img_medium_w='500' #width of images in editor-preview
    img_thumb_w='150' #width of images in editor-preview
    filedir = filetools.createdir(basedir, author.name, today) #TODO: 990-dir is created, img_large_w is ignored
    imgdir = filedir+'images/sorted/'
    images=list()

    for file in filelist:
        print '\n'
        print file.filename
        print '\n'
        filehash = hashlib.sha256(file.value).hexdigest()

        if not filetools.file_exists(images_in_db, filehash): #TODO: Uhm, wouldn't a simple db-query for the hash work too???
            if upload: #only save files when upload-checkbox has been ticked
                filehash = filetools.safe_file_local(imgdir, file)
                imagetools.resize(imgdir, imgdir+img_large_w+'/', file.filename, img_large_w)
                imagetools.resize(imgdir, imgdir+img_medium_w+'/', file.filename, img_medium_w)
                imagetools.resize(imgdir, imgdir+img_thumb_w+'/', file.filename, img_thumb_w)
            image = Image(name=file.filename, location=imgdir, title=None, comment=None, alt=None, \
                        aperture=None, shutter=None, focal_length=None, iso=None, timestamp_original=None, \
                        hash=filehash, hash_large=None, author=author.id, trackpoint=None, last_change=timetools.now(), \
                        published=None, uuid=str(uuid.uuid4()))
            image.aperture, image.shutter, image.focal_length, image.iso, image.timestamp_original = imagetools.get_exif(image)
            image.timestamp_original = image.timestamp_original#-timedelta(seconds=7200) #TODO
            trackpoint=gpxtools.sync_image_trackpoint(image)
            if trackpoint:
                image.trackpoint = trackpoint.id
            DBSession.add(image)
            DBSession.flush()
            image_json = image.reprJSON()
            images.append(image_json)
            print images
        else:
            image = Image.get_image_by_hash(filehash)
            image_json = image.reprJSON()
            images.append(image_json)
    #url = request.route_url('editor')
    #return HTTPFound(location=url)
    return Response(json.dumps({'images':images},cls=ComplexEncoder))
예제 #5
0
def hashfix(request):
    log = DBSession.query(Log).filter(Log.id==52).one()
    images = DBSession.query(Image).filter(Image.logs.contains(log)).order_by(Image.timestamp_original).all()
    n = 0
    for image in images:
        hash = hashlib.sha256(open(image.location+image.name).read()).hexdigest()
        if image.hash != hash:
            print '#', n
            n=n+1
            print image.id, image.location+image.name, image.hash
            print image.id, image.location+image.name, hash, '\n'
            image.hash = hash
    return Response('OK')
예제 #6
0
def image_location(request):
    updated_images=list()
    images = Image.get_images()
    for image in images:
        if not image.trackpoint:
            print image.id
            trackpoint=gpxtools.sync_image_trackpoint(image)
            print trackpoint
            if trackpoint:
                image.trackpoint = trackpoint.id
                DBSession.add(image)
                DBSession.flush()
                updated_images.append(image.id)
    print updated_images
    return Response(str(updated_images))
예제 #7
0
def logsync(request):
    log = Log.get_log_by_id(request.json_body['log']['id'])
    print log.id
    print log.reprJSON_extended()
    if interlink_only:
        return Response(json.dumps({'log_id':log.id, 'type':'log', 'item_uuid':log.uuid, 'sync_status':'was_synced'}))
    else:
        url = 'http://poab.org:6544/sync?type=log'
        payload = {'log_json':json.dumps(log.reprJSON_extended())}
        headers = {'content-type':'application/json'}
        remote_sync_info = requests.post(url, files=payload)
        sync_status=json.loads(remote_sync_info.text)['sync_status'] #if sync_status is True, we already have this image on the server
        if sync_status == 'is_synced':
            log.published = timetools.now()
            DBSession.add(log)
        return Response(remote_sync_info.text)
예제 #8
0
def update_image_metadata(request):
    print request.json_body
    for image_dict in request.json_body:
        try:
            if image_dict['id']:
                image=Image.get_image_by_id(image_dict['id'])
                if image.title != image_dict['title'] or \
                image.alt != image_dict['alt'] or \
                image.comment != image_dict['comment']: #only update if there were changes            
                    image.title = image_dict['title']
                    image.alt = image_dict['alt']
                    image.comment = image_dict['comment']
                    image.last_change = timetools.now()
                    DBSession.add(image)
        except Exception, e:
            print e
            print 'ERROR on updating metadata'
예제 #9
0
def authors_view(request):
    owner = authenticated_userid(request)
    author = Author.get_author(owner)

    authors = DBSession.query(Author).all()
    return {
        'authors': sorted([a.name for a in authors]), 
        'author': author
    }
예제 #10
0
def save_log(request):
    owner = authenticated_userid(request)
    author = Author.get_author(owner)
    log_json = request.json_body
    log_id = log_json['id']
    topic=log_json['topic']
    content=log_json['content']
    images = log_json['images']
    tracks = log_json['tracks']
    etappe = log_json['etappe']

    today=strftime("%Y-%m-%d")
    
    basedir = '/srv/trackdata/bydate'
    filedir = filetools.createdir(basedir, author.name, today) #TODO: 990-dir is created, img_large_w is ignored
    
    start_date = etappe['start_date']
    end_date = datetime.datetime.strptime(etappe['end_date'],'%Y-%m-%d') #unicode to datetime
    end_date = end_date+datetime.timedelta(days=1)-datetime.timedelta(seconds=1) #we need 23:59:59 this day, not 00:00:00
    name=etappe['name']

    if etappe['id']:
        print 'etappe-id:'+ str(etappe['id'])
        etappe = Etappe.get_etappe_by_id(etappe['id'])
        etappe.start_date = start_date
        etappe.end_date = end_date
        etappe.name = name
    else:
        etappe = Etappe(start_date=start_date, end_date=end_date, name=name)
    DBSession.add(etappe)
    DBSession.flush()
 
    if log_id:
        log = Log.get_log_by_id(log_id)
        log.topic = topic
        log.content = content
        log.last_change = timetools.now()
        log.etappe = etappe.id
    else:
        #log_id is None, so this is a new post
        log = Log(topic=topic, content=content, author=author.id, etappe=etappe.id, created=timetools.now(), uuid=str(uuid.uuid4()))
    DBSession.add(log)
    DBSession.flush()
    print 'logid='+str(log.id)
    for image in images:
        try:
            if image['id']:
                print 'imageid:'+ str(image['id'])
                image = Image.get_image_by_id(image['id'])
                log.image.append(image)
        except Exception, e:
            print e
            print 'ERROR while saving log'
예제 #11
0
def create_author_view(request):
    errors = []
    name = password = ''
    if request.method == 'POST':
        name = request.POST.get('name', '')
        password = request.POST.get('password', '')

        v = validate_author(name, password)
        name = v['name']
        password = v['password']
        errors += v['errors']

        if not errors:
            author = Author(name=name, password=password)
            DBSession.add(author)
            url = request.route_url('login')
            return HTTPFound(location=url)

    return {
        'name': name,
        'password': password,
        'errors': errors,
    }
예제 #12
0
def add_trackpoints_to_db(trackpoints, track): 
    for trackpoint in trackpoints:
        trackpoint_in_db = Trackpoint.get_trackpoint_by_lat_lon_time(trackpoint.latitude, \
                                        trackpoint.longitude, trackpoint.timestamp)
        if not trackpoint_in_db:
            trackpoint.track_id = track.id

            try:
                DBSession.add(trackpoint)
                DBSession.flush()
                print trackpoint.timestamp
            except Exception, e:
                print "\n\nTrackpoint could not be added!\n\n"
                print e
                DBSession.rollback()
예제 #13
0
def login_view(request):
    next = request.params.get('next') or request.route_url('overview')
    name = ''
    did_fail = False
    authors = DBSession.query(Author).all()
    if 'submit' in request.POST:
        name = request.POST.get('name', '')
        password = request.POST.get('password', '')
        author = Author.get_author(name)
        if author and author.validate_password(password):
            headers = remember(request, name)
            return HTTPFound(location=next, headers=headers)
        did_fail = True

    return {
        'name': name,
        'next': next,
        'failed_attempt': did_fail,
        'authors': authors,
        'request': request
    }
예제 #14
0
def add_track_to_db(track_details, author):
    track = track_details['track']
    trackpoints = track_details['trackpoints']
    track_in_db = Track.get_track_by_reduced_trackpoints(track.reduced_trackpoints)
    
    if not track_in_db:
        track.author = author.id
        track.uuid = str(uuid.uuid4())
        track.start_time = trackpoints[0].timestamp
        track.end_time = trackpoints[-1].timestamp
        try:
            DBSession.add(track)
            DBSession.flush()
        except Exception, e:
            DBSession.rollback()
            print "\n\nTrack could not be added!\n\n"
            print e
            return None