Beispiel #1
0
def interlink_log(request):
    log_json =  json.loads(request.POST.get('log_json'))
    log = Log.get_log_by_uuid(log_json['uuid'])
    latest_timestamp = datetime.strptime('1970-01-01', '%Y-%m-%d')
    #Link to Tracks
    for track in log_json['tracks']:
        track = Track.get_track_by_uuid(track['uuid'])
        print '############### track.id #########'
        print track.id
        print '################ track.id end #########'
        log.track.append(track)
        #find the latest trackpoint-timestamp related to this log
        #this will be the trackpoint linked to log as infomarker
        if track.trackpoints[0].timestamp > latest_timestamp:
            log.infomarker = track.trackpoints[0].id
            latest_timestamp = track.trackpoints[0].timestamp
    print log_json['tracks']
    if not log_json['tracks']:
        log.infomarker = 3572 #TODO
 
    #Get location for infomarker
    location = Location(name = None, trackpoint_id = None, country_id = None)
    location.name = flickrtools.findplace(log.trackpoint_log_ref.latitude, log.trackpoint_log_ref.longitude, 11, log.author_log_ref)
    location.trackpoint_id = log.trackpoint_log_ref.id,
    location.country_id = flickrtools.get_country_by_lat_lon(log.trackpoint_log_ref.latitude, log.trackpoint_log_ref.longitude, log.author_log_ref).iso_numcode
    #print '\n\n\n\n\n\n'+location.name
    print '\n\n\n\n\n'
    DBSession.add(location)
 
    #Link to Images
    for image in log_json['images']:
        image = Image.get_image_by_uuid(image['uuid'])
        log.image.append(image)
 
    content_with_uuid_tags = log.content
    #print content_with_uuid_tags
    img_uuid_list = re.findall("(\[img_uuid=[0-9A-Za-z-]{1,}\])", content_with_uuid_tags)
    #regex matches A-Z, a-z, 0-9 and "-", e.g. "0eb92a91-3a92-4707-be6e-1907f6c0829"
    print img_uuid_list
    for img_uuid_tag in img_uuid_list:
        img_uuid = re.search("^\[img_uuid=([0-9A-Za-z-]{1,})\]$",img_uuid_tag).group(1)
        image = Image.get_image_by_uuid(img_uuid)
        if image:
            content_with_uuid_tags=content_with_uuid_tags.replace(img_uuid_tag,('[imgid=%s]') % image.id)
    log.content = content_with_uuid_tags
    DBSession.add(log)
    return Response(json.dumps({'link_status':'linked', 'item_uuid': log.uuid}))
Beispiel #2
0
def logsync(request):
    sync_status='sync_error'
    log_json = json.loads(request.POST.get('log_json').value)
    print log_json
    etappe_json = log_json['etappe']
    #TODO: might be better with dates instead of uuid
    etappe = Etappe.get_etappe_by_uuid(etappe_json['uuid']) 
    if not etappe:
        etappe = Etappe(
                start_date = etappe_json['start_date'],
                end_date = etappe_json['end_date'],
                name = etappe_json['name'],
                uuid = etappe_json['uuid']
                )
        DBSession.add(etappe)
        DBSession.flush()

    log = Log.get_log_by_uuid(log_json['uuid'])
    if not log:
        print 'No log found, adding new log.'
        print 'Author: '+log_json['author']
        author = Author.get_author(log_json['author'])
        print author
        log = Log(
                infomarker = None,
                topic=log_json['topic'],
                content=log_json['content'],
                author=author.id,
                etappe=etappe.id,
                created=log_json['created'],
                published=timetools.now(),
                uuid=log_json['uuid']
                )
        DBSession.add(log)
        DBSession.flush()
        sync_status = 'is_synced'; #Item was not synced before we started
    elif log: #TODO: Updating log, needs last_change comparison and stuff
        print 'Log already exists on server'
        sync_status = 'was_synced' #Item was already on the server earlier        
    else:
        sync_status = 'sync_error' #something is wrong here!
    return Response(json.dumps({'log_id':log_json['id'], 'type':'log', 'item_uuid':log_json['uuid'], 'sync_status':sync_status}))