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}))
def imagesync(request): sync_status='sync_error' print request.POST.keys() image_json = request.POST.get('image_json') log_json = json.loads(request.POST.get('log').value) image_bin = request.POST.get('image_bin') image_json = json.loads(image_json.value) author = Author.get_author(image_json['author']['name']) image = Image.get_image_by_uuid(image_json['uuid']) #does image with this uuid(from json-info) already exist in our db if not image: basedir = '/srv/trackdata/bydate' img_prvw_w='500' img_large_w='990' created=datetime.strptime(log_json['created'], "%Y-%m-%d %H:%M:%S") #we use the timestamp from log_json['created'] for the image-location datepath=created.strftime("%Y-%m-%d") filedir = filetools.createdir(basedir, author.name, datepath) imgdir = filedir+'images/sorted/' filehash = filetools.safe_file(imgdir, image_json['name'], image_bin.value) imagetools.resize(imgdir, imgdir+img_prvw_w+'/', image_json['name'], img_prvw_w) imagetools.resize(imgdir, imgdir+img_large_w+'/',image_json['name'] , img_large_w) #TODO: what happens when a 990px-wide img was uploaded? hash_large=hashlib.sha256(open(imgdir+img_large_w+'/'+image_json['name'], 'rb').read()).hexdigest() #TODO filehash=hashlib.sha256(open(imgdir+'/'+image_json['name'], 'rb').read()).hexdigest() #TODO image = Image( name = image_json['name'], location = imgdir, title = image_json['title'], comment = image_json['comment'], alt = image_json['alt'], aperture = image_json['aperture'], shutter = image_json['shutter'], focal_length = image_json['focal_length'], iso = image_json['iso'], timestamp_original = image_json['timestamp_original'], hash = filehash, hash_large = hash_large, #TODO: we need the real file's hash if 990px was uploaded and not converted author = author.id, trackpoint = None, last_change = timetools.now(), published = timetools.now(), uuid = image_json['uuid'] ) DBSession.add(image) DBSession.flush() sync_status = 'is_synced' else: #TODO: So our image is actually in the db - why has this been found earlier in sync?type=status??? print 'ERROR: Image found in DB, but this should have happened in /sync?type=status' sync_status='sync_error' return Response(json.dumps({'log_id' : log_json['id'], 'type':'image', 'item_uuid':image_json['uuid'], 'sync_status':sync_status})) #Something went very wrong
def itemstatus(request): sync_status='sync_error' payload_type = request.POST.get('payloadtype') print '\n\n\n\n\n\n\n\n\n\n' print payload_type print '\n\n\n\n\n\n\n\n\n\n' log_json = json.loads(request.POST.get('log_json')) if payload_type == 'image': image_json = json.loads(request.POST.get('image_json')) payload_uuid = image_json['uuid'] image = Image.get_image_by_uuid(image_json['uuid']) if image: print 'Image found! '+image.location image_bin = open(image.location+image.name, 'rb') hash=hashlib.sha256(image_bin.read()).hexdigest() if hash == image.hash == image_json['hash']: print 'Image already exists on server!' sync_status='was_synced' else: print 'Imagehash mismatch!' sync_status='sync_error' else: sync_status = 'not_synced' elif payload_type == 'track': track_json = json.loads(request.POST.get('track_json')) track = Track.get_track_by_uuid(track_json['uuid']) payload_uuid = track_json['uuid'] if track: print 'Track already exists on server!' sync_status='was_synced' else: print 'Track not on server' sync_status = 'not_synced' return Response(json.dumps({'log_id':log_json['id'],'type':payload_type, 'item_uuid':payload_uuid, 'sync_status':sync_status}))
def view_view(request): try: action=request.matchdict['action'] except: action='c' try: id=int(request.matchdict['id']) except: id=0 try: page_number=int(request.matchdict['page'].replace('/','')) except: page_number=None if id==0 and page_number==None: q = DBSession.query(Image).order_by(Image.timestamp_original) image_count=q.count() page_fract=float(Fraction(str(image_count)+'/10')) if int(str(page_fract).split('.')[1])==0: page=int(str(page_fract).split('.')[0])-1 else: page=str(page_fract).split('.')[0] elif page_number==None: page=0 else: page=page_number #navstring=countryDetails(model,id) curr_page=int(page) #return { 'bla': log_count} if id==0: ##TODO what was the idea behind "country_id!=None"? ##q = DBSession.query(Trackpoint).filter(Trackpoint.country_id!=None) #q = DBSession.query(Trackpoint) #images=fetch_images_for_trackpoints(q) images=Image.get_images() #print '\n\n\n\n\n' #print images #print '\n\n\n\n\n' elif action=='c': #q = DBSession.query(Trackpoint).filter(and_(Trackpoint.country_id==id)) #images=fetch_images_for_trackpoints(q) images=Image.get_images() elif action=='log': #q = DBSession.query(Trackpoint).filter(and_(Trackpoint.id==id)) #images=fetch_images_for_trackpoints(q) log = DBSession.query(Log).filter(Log.id==id).one() images = DBSession.query(Image).filter(Image.logs.contains(log)).order_by(Image.timestamp_original).all() elif action=='id': images = DBSession.query(Image).filter(Image.id==id).order_by(Image.timestamp_original).all() page_list=list() pages_list=list() i=0 for image in images: page_list.append(image) i=i+1 if i==10: page_list.reverse() pages_list.append(page_list) page_list=list() i=0 if i<10 and i>0: page_list.reverse() pages_list.append(page_list) viewlist=list() #print page_list #print pages_list #print curr_page #print pages_list[curr_page] for image in pages_list[curr_page]: if image.trackpoint: trackpoint_id=image.trackpoint else: trackpoint_id=3572 #TODO prefix='near ' q = DBSession.query(Trackpoint).filter(Trackpoint.id==trackpoint_id) try: trackpointinfo=q.one() #print '\n\n\n\n' #print trackpointinfo.location_ref[0].name except: trackpointinfo = Trackpoint( track_id = None, latitude = None, longitude = None, altitude = None, velocity = None, temperature = None, direction = None, pressure = None, timestamp = None, uuid = None ) #print image.location.replace('/srv','') #print '\n\n\n\n' ##TODO: fix timezone ##q = DBSession.query(Timezone).filter(Timezone.id==trackpointinfo.timezone_id) q = DBSession.query(Timezone).filter(Timezone.id==8) timezone = q.one() localtime = image.timestamp_original+timezone.utcoffset deltaseconds=round(timezone.utcoffset.days*86400+timezone.utcoffset.seconds) #TODO THIS SUCKS! class Viewdetail(object): def __init__(self, image, photoid, name, location, title, comment, alt, aperture, shutter, focal_length, iso, trackpointinfo, localtime, timezone, utcoffset, log, author): self.image = image self.photoid=photoid self.name=name self.location=location self.title=title self.comment=comment self.alt=alt self.aperture= image.aperture self.shutter= image.shutter self.focal_length= image.focal_length self.iso= image.iso #logdate=c.loginfo.created.strftime('%Y-%m-%d') #needed for the imagepath self.trackpointinfo=trackpointinfo self.localtime=localtime self.timezone=timezone #calculate the offset in seconds self.utcoffset=utcoffset self.log = log self.author = image.author_img_ref viewdetail = Viewdetail(image, image.id, image.name, image.location.replace('/srv',''), image.title, image.comment, image.alt, image.aperture, image.shutter, image.focal_length, image.iso, trackpointinfo, localtime.strftime('%Y-%m-%d %H:%M:%S'), timezone, timediff(deltaseconds), image.log, image.author) viewlist.append(viewdetail) return { 'pages_list': pages_list, 'curr_page': int(curr_page), 'viewlist': viewlist, 'request': request, 'action': action, 'id': id, }