def interlink_image(request):
    image_json = json.loads(request.POST.get('image_json'))
    print image_json['id']
    print image_json['name']
    print image_json['trackpoint']
    image = Image.get_image_by_uuid(image_json['uuid'])
    try:
        trackpoint = Trackpoint.get_trackpoint_by_uuid(image_json['trackpoint']['uuid'])
    except:
        trackpoint = None
    location = None
    if trackpoint:
        print trackpoint
        print image
        image.trackpoint = trackpoint.id
    #Get location for image.trackpoint
        location = Location(name = None, trackpoint_id = None, country_id = None)
        location.name = flickrtools.findplace(image.trackpoint_img_ref.latitude, image.trackpoint_img_ref.longitude, 11, image.author_img_ref)
        location.trackpoint_id = image.trackpoint_img_ref.id,
        location.country_id = flickrtools.get_country_by_lat_lon(image.trackpoint_img_ref.latitude, image.trackpoint_img_ref.longitude, image.author_img_ref).iso_numcode
    if not image.image_flickr_ref:
        print '\n\n\n\n\n\n\n'+str(image.id)
        print '\n\n\n\n\n\n\n'
        farm,server,photoid,secret,originalsecret,originalformat = flickrtools.uploadimage(image, image.author_img_ref, '')
        flickrimage = FlickrImage(image = image.id, farm = farm, server = server, photoid = photoid, secret = secret)
        DBSession.add(flickrimage)
    DBSession.add(image)
    if location: #TODO(Ugly?)
        DBSession.add(location)
    

    return Response(json.dumps({'link_status':'linked', 'item_uuid': image.uuid}))
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}))