def default(self): """ Default action fetches all the stats and renders a template using them """ stats = {} # get all the thumbnails stats["Database stats"] = [ ("Number of marked files", Photo.get_num_marked_photos(), "raw"), ("Number of DB files", Photo.get_count_by_date(), "raw") ] stats["File System stats"] = [] for s in [Photo.SMALL_THUMB_SIZE, Photo.MEDIUM_THUMB_SIZE]: stats["File System stats"].extend(self._thumb_info(s)) num_images = 0 total_size = 0 for root, dirs, files in os.walk(S.BASE_FS_PATH): for f in files: if util.is_image_file(f): num_images += 1 total_size += os.path.getsize(os.path.join(root, f)) stats["File System stats"].extend([ ("Number of source images", num_images, "raw"), ("Disk space", total_size, "bytes") ]) return self.construct_response(Template.render("stats.html", {"stats": stats}))
def get_photos_from_date(self): """ Fetches a list of photos which apply to the given filter This function should be able to handle listing directories (for instance, month directories in the year or days in each month) as well as actually rendering photos. """ year, month, day = self._get_id_from_path("").split(os.sep) offset = self._get_from_query("page", 1) - 1 limit = self._get_from_query("limit", S.DEFAULT_PER_PAGE) num_photos = Photo.get_count_by_date(year=year, month=month, day=day) start_index = (offset * limit) + 1 end_index = min(((offset + 1) * limit), num_photos) photos = Photo.get_by_date(year=year, month=month, day=day, limit=limit, offset=(offset * limit)) tokens = { "photos": photos, "offset": offset, "limit": limit, "start_index": start_index, "end_index": end_index, "num_photos": num_photos } return self.construct_response(Template.render("photos/list.html", tokens))
def get_photos_from_date(self): """ Fetches a list of photos which apply to the given filter This function should be able to handle listing directories (for instance, month directories in the year or days in each month) as well as actually rendering photos. """ year, month, day = self._get_id_from_path("").split(os.sep) offset = self._get_from_query("page", 1) - 1 limit = self._get_from_query("limit", S.DEFAULT_PER_PAGE) num_photos = Photo.get_count_by_date(year=year, month=month, day=day) start_index = (offset * limit) + 1 end_index = min(((offset + 1) * limit), num_photos) photos = Photo.get_by_date(year=year, month=month, day=day, limit=limit, offset=(offset * limit)) tokens = { "photos": photos, "offset": offset, "limit": limit, "start_index": start_index, "end_index": end_index, "num_photos": num_photos } return self.construct_response( Template.render("photos/list.html", tokens))
def sync_photo(id, flickr, check_dirty=False): print id db_photo = session.query(Photo).filter(Photo.flickr_id == id).first() if db_photo and not check_dirty: print 'Photo is already local.' return db_photo photo = simplejson.loads(flickr.photos_getInfo(photo_id=id, nojsoncallback=1)) p = photo['photo'] (id, title) = (int(p['id']), p['title']['_content']) url = url_for_photo(p) page_url = p['urls']['url'][0]['_content'] description = """%s\n %s Taken: %s in %s Flickr: %s""" % (p['title']['_content'], p['description']['_content'], p['dates']['taken'], loc_to_string(p), page_url) if db_photo: print "Photo %s already exists" % id if db_photo.title == title and db_photo.description == description: return db_photo db_photo.dirty = True db_photo.title = title db_photo.description = description else: url = url_for_photo(p) db_photo = Photo(title= title, description=description, flickr_id=id, dirty=False, url=url) if not p['visibility']['ispublic']: db_photo.private = True session.add(db_photo) sync_tags(db_photo, p) session.commit() return db_photo
def _get_image(self, size, action): """ Fetches the large image for lightboxing for the given photo id. Returns the image raw data. """ id = self._get_id_from_path(action) try: id = int(id) p = Photo.get_by_id(id) except Exception as e: p = None if p == None: fc = util.FileContainer(os.path.join(S.IMPORT_DIR, id), S.IMPORT_DIR) fc.time = util.get_time(fc)["time"] p = Photo.from_file_container(fc) if p == None: Logger.warning("could not find photo for %s" % id) image_path = S.BROKEN_IMG_PATH else: rel_thumb_path = p.get_or_create_thumb(size) image_path = os.path.join(S.THUMBNAIL_DIR, rel_thumb_path) f = open(image_path) raw_image = f.read() f.close() return self.construct_response(raw_image, self._route_types.JPEG_CONTENT_TYPE)
def default(self): """ Default action fetches all the stats and renders a template using them """ stats = {} # get all the thumbnails stats["Database stats"] = [ ("Number of marked files", Photo.get_num_marked_photos(), "raw"), ("Number of DB files", Photo.get_count_by_date(), "raw") ] stats["File System stats"] = [] for s in [Photo.SMALL_THUMB_SIZE, Photo.MEDIUM_THUMB_SIZE]: stats["File System stats"].extend(self._thumb_info(s)) num_images = 0 total_size = 0 for root, dirs, files in os.walk(S.BASE_FS_PATH): for f in files: if util.is_image_file(f): num_images += 1 total_size += os.path.getsize(os.path.join(root, f)) stats["File System stats"].extend([ ("Number of source images", num_images, "raw"), ("Disk space", total_size, "bytes") ]) return self.construct_response( Template.render("stats.html", {"stats": stats}))
def _upload_photos(self, users, comps): d = os.getcwd() d = os.path.join(d, 'test', '*.jpg') photos = glob(d) titles = ('Mars', 'Finnish Flag', 'Hospital in the distance', '', '', '') comp1 = comps[0] # collect Photo instances here p = [] all_data = zip(product(users, comps), photos, titles) for (user, comp), photo_path, title in all_data: file_name = files.blobstore.create(mime_type='image/jpeg') with files.open(file_name, 'a') as f: f.write(open(photo_path, 'rb').read()) files.finalize(file_name) blob_key = files.blobstore.get_blob_key(file_name) photo = Photo( user=user.key, competition=comp.key, blob=blob_key, title=title ) photo.put() p.append(photo) user_comp = UserComp(user=user.key, comp=comp.key) if comp == comp1: user_comp.submitted_scores = True user_comp.put() return p
def post(self): try: action = self.request.get("action") if action == "new": if "image" in self.request.arguments(): img_req = db.Blob(self.request.get("image")) content_id = db.Key(self.request.get("content")) image = Photo(content=content_id, image=img_req, tumb_img=images.resize(img_req, 300, 300)) image.put() else: content = Content(menu=self.request.get("menu"), title=self.request.get("title"), text=self.request.get("text"),) content.put() if action == "edit": if "content" in self.request.arguments(): content_id = db.Key(self.request.get("content")) content = Content.get(content_id) content.menu = self.request.get("menu") content.title = self.request.get("title") content.text = self.request.get("text") content.put() if action == "del": if "id" in self.request.arguments(): id = db.Key(self.request.get("id")) photo = Photo.get(id) photo.delete() if "content" in self.request.arguments(): content_id = db.Key(self.request.get("content")) content = Content.get(content_id) content.delete() self.redirect("/admin/") except Exception: self.error(404)
def test_shouldCreatePhotoWithoutCode(self): test_photo = Photo().from_json( json.loads(''' {"code": "", "dimensions": {"width": 1080, "height": 1349}, "comments_disabled": false, "owner": {"id": "11111111"}, "comments": {"count": 134}, "caption": "#sunday#black#girl#selfie", "likes": {"count": 12}, "date": 1476637330, "thumbnail_src": "https://scontent-waw1-1.cdninstagram.com/whatever/whatever", "is_video": false, "id": "3333333333333333333", "display_src": "https://scontent-waw1-1.cdninstagram.com/whatever/whatever"}''' )) self.assertIsNotNone(test_photo) self.assertTrue(test_photo.width == 1080) self.assertTrue(test_photo.height == 1349) self.assertTrue(test_photo.owner_id == '11111111') self.assertTrue(test_photo.caption == '#sunday#black#girl#selfie') self.assertTrue(test_photo.likes_count == 12) self.assertFalse(test_photo.is_video) self.assertTrue(test_photo.id == '3333333333333333333') self.assertTrue(test_photo.code == '') self.assertTrue( test_photo.display_src == 'https://scontent-waw1-1.cdninstagram.com/whatever/whatever') test_photo = Photo().from_json( json.loads(''' { "dimensions": {"width": 1080, "height": 1349}, "comments_disabled": false, "owner": {"id": "11111111"}, "comments": {"count": 134}, "caption": "#sunday#black#girl#selfie", "likes": {"count": 12}, "date": 1476637330, "thumbnail_src": "https://scontent-waw1-1.cdninstagram.com/whatever/whatever", "is_video": false, "id": "3333333333333333333", "display_src": "https://scontent-waw1-1.cdninstagram.com/whatever/whatever"}''' )) self.assertIsNotNone(test_photo) self.assertTrue(test_photo.width == 1080) self.assertTrue(test_photo.height == 1349) self.assertTrue(test_photo.owner_id == '11111111') self.assertTrue(test_photo.caption == '#sunday#black#girl#selfie') self.assertTrue(test_photo.likes_count == 12) self.assertFalse(test_photo.is_video) self.assertTrue(test_photo.id == '3333333333333333333') self.assertTrue(test_photo.code == '') self.assertTrue( test_photo.display_src == 'https://scontent-waw1-1.cdninstagram.com/whatever/whatever')
def response(): if config.memcache.db: clearmem() action = cgi_get("action", choices=["post", "videopost", "comment", "photo", "photoset", "md"]) if action == "md": for dn, dz, fz in os.walk("md"): succeed([f[:-3] for f in fz]) user = cgi_get("user") if action == "comment": ent = Comment(user=user, post=cgi_get("post"), body=cgi_get("body")) elif action == "photo": pkey = cgi_get("key", required=False) if pkey: ent = Photo.query(Photo.key == pkey).get() else: ent = Photo() capt = cgi_get("caption", required=False) # imgs uploaded separately if capt: ent.caption = capt else: blurb = cgi_get("blurb", required=False) pkey = cgi_get("key", required=False) tags = cgi_get("tags", required=False) pmod = db.get_model(action) if pkey: ent = pmod.query(pmod.key == pkey).get() else: ent = pmod(user=user) ent.title = cgi_get("title") ent.live = cgi_get("live") if blurb: ent.blurb = blurb if tags: ent.tags = tags if action == "post": ent.body = cgi_get("body") ent.put() if action == "photo": # get photoset psk = cgi_get("photoset", required=False) if psk: ps = db.get(psk) # these are hacky -- fix list ops in ct if cgi_get("remove", default=False): # ps.photos.remove(ent.key) ps.photos = [p for p in ps.photos if p != ent.key] else: # ps.photos.append(ent.key) ps.photos = ps.photos + [ent.key] ps.put() succeed(ent.id())
def get_one(self): """ Fetches and returns raw data for a single photo """ self._get_id_from_path("single") a = Photo.get_by_id(id) return self.construct_response()
def get(self): """Show the competitions page.""" user_id, user = self.get_user() comps = [] for c in Competition.all(): month = c.month month_word = MONTHS[month] user_photo = False if user: user_photo = Photo.competition_user(c, user) is not None comps.append( ( # month, c.key.id(), month_word, c.year, c.title, c.description, c.get_status(), user_photo, ) ) data = {"page_title": "Competitions", "user": user, "comps": comps, "months": MONTHS} self.render("competitions.html", **data)
def post(self, comp_id=0): """A user is submitting scores.""" user_id, user = self.get_user() comp_id = int(comp_id) comp = Competition.get_by_id(comp_id) if not user or not comp: # stop some unauthorised post submissions. self.redirect("/competitions") return results = self.parse_scores(self.request.POST) for photo_id, score in results.iteritems(): photo = Photo.get_by_id(photo_id) # photo = self.get_photo(photo_id) new_score = Scores(photo=photo.key, user_from=user.key, score=score) new_score.put() # record that user has submitted scores for this comp usercomp = self.get_usercomp(user, comp) usercomp.submitted_scores = True usercomp.put() self.redirect("/competition/%d" % (comp_id))
def _calculate_scores(self, comp): """Calculate the scores for a completed competition.""" all_scores = UserComp.all_scores_submitted(comp) if not all_scores: return False results = [] for photo in Photo.competition_photos(comp): total_score = Scores.photo_score(photo) results.append((total_score, photo)) results.sort(reverse=True) # calculate positions position = 1 prev_score = 1000000 # full_results = [] for i, (score, photo) in enumerate(results, start=1): if score != prev_score: position = i # full_results.append((position, score, photo)) photo.position = position photo.total_score = score photo.put() prev_score = score return True
def view_open(self, user, comp_id, comp, data): """Create the competition page when its status is Open.""" # for p in Photo.competition_photos(comp): photo_count = len(list(Photo.competition_photos(comp))) data.update({"photo_count": photo_count}) self.render("competition-open.html", **data)
def load_photos(photo_filename): """Load movies from file into database.""" print("Photos") for i, row in enumerate(open(photo_filename)): row = row.rstrip() # unpack file into variables img_id, url, lat, lon, city_name = row.split("|") # convert geolocation from string to float format lat = float(lat) lon = float(lon) img_id = int(img_id) photo = Photo(img_id=img_id, url=url, lat=lat, lon=lon, city_name=city_name) db.session.add(photo) db.session.commit()
def random_images(self, number=3): photo_keys = list(Photo.query().fetch(keys_only=True)) shuffle(photo_keys) photos = [] for key in photo_keys: photo = key.get() if photo is None: # on the dev server, after a photo delete, the key is still in # the database when the homepage loads. So need this check to # make sure this is not the recently deleted photo. # TODO: fix this... continue if photo.competition and photo.competition.get().status == COMPLETED: # only view photos belonging to completed competition - title = photo.title if not title: title = 'Untitled' user = photo.user.get().username photos.append((key.id(), photo.url(size=800), title, user)) if len(photos) == number: # Once we have the required number of photos, we can quit the # loop break #logging.info('random photos: %s', photos) return photos
def add_photo(filename, pp_id): """ adds photo to database""" # need trip id trip_id = session["current_trip_id"] # need file_path file_path = "/static/photos/" + filename # check if the photo is already in database in_db = Photo.query.filter_by(trip_id=trip_id, pp_id=pp_id, file_path=file_path, file_name=filename).first() if not in_db: # add row to photos table new_photo = Photo(file_path=file_path, file_name=filename, trip_id=trip_id, pp_id=pp_id) db.session.add(new_photo) db.session.commit() print("added photo to database") else: flash("photo already loaded") return
def _update_mark(self, marked): """ Handles the AJAX calls from the app mark actions """ post_args = parse_qs(self._env["wsgi.input"].read()) if "id" not in post_args: Logger.warning("not in post args: %s" % str(post_args)) return self.construct_response(json.dumps({ "success": False, "error": "missing args", "id": None }), self._route_types.JSON_CONTENT_TYPE) post_ids = post_args["id"] _, id = post_ids[0].split("_") p = Photo.get_by_id(id) if p == None: Logger.warning("no photo retrieved") return self.construct_response(json.dumps({ "success": False, "error": "invalid_id", "id": id }), self._route_types.JSON_CONTENT_TYPE) p.marked = marked p.store() a = self.construct_response(json.dumps({ "success": True, "details": { "marked": p.marked, "id": id } }), self._route_types.JSON_CONTENT_TYPE) return a
def get_instagram_photos(rest_id, location): """""" os.system( 'instagram-scraper --location ' + location + ' --maximum 6 --media-metadata --media-types none --destination ig_photos' ) # try with subprocess: # string = 'instagram-scraper --location ' + location + ' --maximum 4 --media-metadata --media-types none --destination ig_photos' # p = subprocess.Popen(string, stdout=subprocess.PIPE, shell=True) # TAKE OUT shell=True # p.terminate() json_file = 'ig_photos/' + location + '.json' with open(json_file) as json_data: results = json.load(json_data) for result in results: url = result['urls'][0] if url[-3:] == 'jpg': photo = Photo(rest_id=rest_id, url=url) db.session.add(photo) db.session.commit() os.system('rm ig_photos/' + location + '.json') return 'success'
def _data(self, comp, user, **kwds): """Create the data dictionary for the renderer.""" users = [] photos = [] status = comp.status for uc in comp.users(): user1 = uc.user.get() users.append((user1, "Yes" if uc.submitted_scores else "No")) if status == OPEN: photo = Photo.competition_user(comp, user1) photos.append(photo.key.id()) data = { "page_title": "Modify Competition", "title": comp.title, "description": comp.description, "year": comp.year, "month": MONTHS[comp.month], "status": comp.get_status(), "user": user, "users": users, "photos": photos, "comp_id": comp.key.id(), "status_values": ((0, "Open"), (1, "Scoring"), (2, "Completed")), } data.update(kwds) # logging.info('kwds %s' % kwds) return data
def load_photos(): """Load photos from seed data into database""" with open("seed_data/photos.txt") as photos: for row in photos: photo = row.rstrip().split("|") hidden = True if photo[6] == "True" else False kwargs = dict( photo_id = photo[0], user_id = photo[1], play_id = photo[2], order = photo[3], film_id = photo[4], photo = photo[5], hidden = hidden ) keys_to_remove = [] for key in kwargs.keys(): if kwargs[key] == "": keys_to_remove.append(key) for key in keys_to_remove: del kwargs[key] photo = Photo(**kwargs) db.session.add(photo) db.session.commit()
def get_dirs_from_date(self): """ Renders a list of all the year "folders" in the system. As we're not rendering any photos, we can assume this to be a separate function from the photo fetching and rendering; this is just reporting certain dates. """ path = self._env.get('PATH_INFO', '').lstrip('/') path = os.path.relpath(path, "photos") Logger.debug(path) path_parts = path.split(os.sep) if len(path_parts) == 1 and path_parts[0] == ".": path_parts = [] year = None if len(path_parts) < 1 else path_parts[0] month = None if len(path_parts) < 2 else path_parts[1] list = Photo.get_all_dates(year=year, month=month) list = [("0%d" % f if f < 10 else str(f)) for f in list] list.sort() tokens = { "dirs": list, "year": year, "month": month } return self.construct_response(Template.render("photos/dirs.html", tokens))
def create_photo(user, entry, photo_url, city): photo = Photo(user=user, entry=entry, photo_url=photo_url, city=city) db.session.add(photo) db.session.commit() return photo
def get(self, user_view_id): # logged in user user_id, user = self.get_user() # user's page user_view_id = int(user_view_id) user_view = User.get_by_id(user_view_id) my_page = user == user_view if user else False if not user_view: data = { 'user': user, 'page_title': 'Error', 'error_msg': 'Cannot find User', } self.render('error.html', **data) return if my_page: photos = Photo.user_photos(user) need_scores = list(user.scoring_competitions()) need_photos = self._competitions_need_photos(user) else: photos = Photo.user_photos_complete(user_view) need_scores = [] need_photos = [] extra_photos = Photo.extra_photos(user_view) data = { 'page_title': 'User', 'page_subtitle': user_view.username, 'user': user, 'user_view': user_view, 'my_page': my_page, 'need_scores': need_scores, 'need_photos': need_photos, 'photos': photos, 'extra_photos': extra_photos, 'upload_extra': (user.extra_photo_count < MAX_EXTRA_PHOTO if user else False), 'max_extra_photos': MAX_EXTRA_PHOTO, 'months': MONTHS, 'upload_url': blobstore.create_upload_url('/upload'), 'upload_extra_url': blobstore.create_upload_url('/upload'), } self.render('user-view.html', **data)
def get(self): if self.request.get("id"): photo = Photo.get(self.request.get("id")) if photo: self.response.headers['Content-Type'] = 'image/jpeg' self.response.out.write(photo.tumb_img) return self.error(404)
def preview(self): """ Presents a preview of the files to be imported, giving the user an opportunity to view and change dates for images, highlighting images which may already be in the system, and the like. """ rel_import_dir = os.path.relpath( self._env.get("PATH_INFO", "").lstrip("/"), "import/preview") import_dir = os.path.realpath( os.path.join(S.IMPORT_DIR, rel_import_dir)) file_listing = [] import_identifier = hashlib.sha1() hashes = [] session_file_struct = {} for base_dir, _, files in os.walk(import_dir): for f in files: if not util.is_image_file(f): continue fc = util.FileContainer(os.path.join(import_dir, f), S.IMPORT_DIR) ts = util.get_time(fc, allow_date_from_path=False) if ts["time"] != None: fc.time = time.strftime("%Y-%m-%d %H:%M:%S", ts["time"]) hashes.append(fc.hash) import_identifier.update(fc.hash) file_listing.append(fc) session_file_struct[fc.hash] = { "file_data": fc.__dict__(), "conflicts": None } break file_listing = sorted(file_listing, key=itemgetter('name')) conflicts = Photo.get_by_hash(hashes) for conflict_hash in conflicts.keys(): conflicts_for_json = [c.id for c in conflicts[conflict_hash]] session_file_struct[conflict_hash][ "conflicts"] = conflicts_for_json session_file_struct[conflict_hash]["file_data"]["marked"] = True Logger.debug(session_file_struct) session_id = import_identifier.hexdigest() session_data = { "file_listing": session_file_struct, "rel_dir": rel_import_dir, "session_id": session_id } with open(os.path.join("/tmp", "%s.txt" % session_id), "w+") as f: f.write(json.dumps(session_data)) return self.construct_response( Template.render( "import/preview.html", { "files": file_listing, "import_id": session_id, "import_dir": rel_import_dir, "conflicts": conflicts }), self._route_types.HTML_CONTENT_TYPE)
def preview(self): """ Presents a preview of the files to be imported, giving the user an opportunity to view and change dates for images, highlighting images which may already be in the system, and the like. """ rel_import_dir = os.path.relpath(self._env.get("PATH_INFO", "").lstrip("/"), "import/preview") import_dir = os.path.realpath(os.path.join(S.IMPORT_DIR, rel_import_dir)) file_listing = [] import_identifier = hashlib.sha1() hashes = [] session_file_struct = {} for base_dir, _, files in os.walk(import_dir): for f in files: if not util.is_image_file(f): continue fc = util.FileContainer(os.path.join(import_dir, f), S.IMPORT_DIR) ts = util.get_time(fc, allow_date_from_path=False) if ts["time"] != None: fc.time = time.strftime("%Y-%m-%d %H:%M:%S", ts["time"]) hashes.append(fc.hash) import_identifier.update(fc.hash) file_listing.append(fc) session_file_struct[fc.hash] = { "file_data": fc.__dict__(), "conflicts": None } break file_listing = sorted(file_listing, key=itemgetter('name')) conflicts = Photo.get_by_hash(hashes) for conflict_hash in conflicts.keys(): conflicts_for_json = [c.id for c in conflicts[conflict_hash]] session_file_struct[conflict_hash]["conflicts"] = conflicts_for_json session_file_struct[conflict_hash]["file_data"]["marked"] = True Logger.debug(session_file_struct) session_id = import_identifier.hexdigest() session_data = { "file_listing": session_file_struct, "rel_dir": rel_import_dir, "session_id": session_id } with open(os.path.join("/tmp", "%s.txt" % session_id), "w+") as f: f.write(json.dumps(session_data)) return self.construct_response( Template.render( "import/preview.html", { "files": file_listing, "import_id": session_id, "import_dir": rel_import_dir, "conflicts": conflicts } ), self._route_types.HTML_CONTENT_TYPE )
def create_photo(img_path, location, description, gps_url, popular_url): """ create and return a photo""" photo = Photo(img_path=img_path, location=location, description=description, gps_url=gps_url, popular_url=popular_url) db.session.add(photo) db.session.commit() return photo
def _delete_all(self): for base in (Competition, UserComp, Scores, Comment): for item in base.query(): item.key.delete() for photo in Photo.query(): delete_blob(photo.blob) photo.key.delete() for user in User.gql('WHERE username != :1', 'test'): user.key.delete()
def create_photo(title, desc, price, img_url): """Create and return a new photo.""" photo = Photo(title=title, desc=desc, price=price, img_url=img_url) db.session.add(photo) db.session.commit() return photo
def add_photo_to_trip(): '''adds a photo to the trip board for that location''' trip_id = request.form.get('trip') img_id = request.form.get('img_id') url = request.form.get('url') lat = request.form.get('lat') lon = request.form.get('lon') city_name = request.form.get('city_name') trip = Trip.get_trip(trip_id) # checks if authorized user is accessing this page if session.get('login') == trip.user_id: # if photo is not in the database, add it to the database if not Photo.get_photo(img_id): photo = Photo(img_id=int(img_id), url=url, lon=float(lon), lat=float(lat), city_name=city_name) db.session.add(photo) db.session.commit() # check if photo already exists in current users trip already_exists = TripPhotoRelationship.get_trip_photo(trip_id, img_id) if already_exists: return 'This photo is already in your trip board!' # photo is not in current trip board, add relationship to the database else: trip_photo = TripPhotoRelationship(trip_id=trip_id, photo_id=img_id) db.session.add(trip_photo) db.session.commit() return 'Photo Added' # unauthorized user, redirect to homepage else: flash('You do not have permission to access this feature') return 'Unauthorized User: Photo Not Added'
def load_seed_photos(): """Load sample photo urls from seed_photos.csv""" for row in open("seed_data/seed_photos.csv"): row = row.rstrip() user_id, event_id, url = row.split(",") row_photo = Photo(user_id=user_id, event_id=event_id, url=url) db.session.add(row_photo) db.session.commit() print("Successfuly seeded into the photos table!")
def get(self, name): a_obj = Photo.get_by_key_name(name) if a_obj and a_obj.content: self.header['Content-Type'] = "image/png" self.header['Cache-Control'] = "max-age=172800, public, must-revalidate" self.header['Expires'] = avatar_expires() self.write(a_obj.content) else: self.error(404)
def uploadfile(): if request.method == 'POST': file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) photo_location = "uploads/" + filename photo_file_path = os.path.join(app.config['UPLOAD_PHOTO_FOLDER'], filename) file.save(photo_file_path) thumbnail_file_path = os.path.splitext( photo_file_path)[0] + ".thumbnail" create_thumbnail(filename, photo_file_path, thumbnail_file_path) thumbnail_location = "uploads/" + os.path.splitext( filename)[0] + ".thumbnail" image = Image.open(photo_file_path) exif_data = get_exif_data(image) latlon = get_lat_lon(exif_data) l = str(latlon) latitude = lat(l) longitude = lon(l) timestamp = get_time(exif_data) if timestamp != None: timestamp = datetime.strptime(str(timestamp), "%Y:%m:%d %H:%M:%S") caption = request.form['caption'] p = Photo(file_location=photo_location, caption=caption, latitude=latitude, longitude=longitude, timestamp=timestamp, user_id=g.user_id, thumbnail=thumbnail_location) db_session.add(p) db_session.commit() db_session.refresh(p) if latitude == None: # photo_id is a key and p.id is a value and session is a dict print "SESSION" session['photo_id'] = p.id return redirect(url_for('addlocation', photo_id=p.id)) user = db_session.query(User).filter_by(id=g.user_id).one() # create a template that shows the view of an uploaded photo and then the user's other photos return redirect(url_for('userpage')) return render_template("upload.html")
def _last_positions(self, data): '''Update UserStat records for all users who have a photo that was last in a competition.''' for comp in Competition.get_by_status(COMPLETED): photos = list(Photo.competition_photos(comp)) last_position = max(photos, key=lambda x: x.position).position #logging.info('%s: last: %d' % (comp, last_position)) for photo in filter(lambda x: x.position == last_position, photos): user_stat = data[photo.user.id()] user_stat.last_place += 1 user_stat.medals += 1
def create_photo_object(pet_entry, photo_entry): """Take photo attributes out of pet mini-dictionary and instantiate a Photo object.""" # Get pet ID out of database and store as foreign key. new_photo = Photo(pet_id=pet_entry['id'], pf_id=photo_entry['@id'], photo_size=photo_entry['@size'], photo_text=photo_entry['#text']) db.session.add(new_photo) print("Loaded Photos.")
def add_my_photo(): """Adds my photo to User1""" my_photo = Photo( user_id=1, photo_url= "http://fellowship.hackbrightacademy.com/media/CACHE/images/students/IMG_0005/6259c4fbf765821b3b73f6a0964592f9.jpg", ) print my_photo db.session.add(my_photo) db.session.commit()
def _check(self, photo_id): '''Helper method which checks the proper permissions for deleting the photograph. Return ------ data : dict Data to be passed to the template. error : boolean True if user has permission to delete the photo ''' user_id, user = self.get_user() if not user: self.redirect('/') referrer = self.request.referrer data = { 'page_title': 'Delete Photograph', 'userid': user_id, 'user': user, 'referrer': referrer if referrer else '/', } photo = Photo.get_by_id(photo_id) if not photo: data['error_msg'] = "Photograph doesn't exist." return data, True my_photo = user_id == photo.user.id() extra_photo = photo.competition is None if not extra_photo: comp = photo.competition.get() if comp.status != OPEN: error_msg = "Can only delete a photograph from an open competition." data['error_msg'] = error_msg return data, True else: comp = None #photo_user = photo.user.id() #if not user.admin and user_id != photo_user: if not self._user_permission(user, extra_photo, my_photo): error_msg = "You don't have permission to delete this photograph." data['error_msg'] = error_msg return data, True # no errors data['photo'] = photo data['url'] = photo.url(400) data['title'] = photo.title data['comp'] = comp return data, False
def get(self): try: content = Content.all() new_list_content = [] out_url = users.create_logout_url("/") for c in content: c.img = Photo.all().filter("content =", c.key()) new_list_content.append(c) path = os.path.join(os.path.dirname(__file__), 'templates/admin.html') self.response.out.write(template.render(path, {"content" : new_list_content, "url" : out_url})) except Exception: self.error(404)
def post(self, username, img_max_size): if self.cur_user and self.cur_user.flag>1: self.header['Content-Type'] = "text/html" rspd = {'status': 201, 'msg':'ok'} file_content = self.request.get('filetoupload','') if file_content: imgobj = images.Image(file_content) max_w = int(img_max_size) if imgobj.width <= max_w: #img_data = file_content pass else: imgobj.resize(width=max_w) imgobj.im_feeling_lucky() img_data = imgobj.execute_transforms(output_encoding=images.JPEG, quality=90) ni_obj = Photo(key_name = '%s-%s'%(username, str(int(time()))), content = img_data) ni_obj.put() if ni_obj.is_saved(): rspd['status'] = 200 rspd['msg'] = u'图片已成功上传' rspd['url'] = '%s/photo/%s.jpg' % (BASE_URL, ni_obj.key().name()) else: rspd['status'] = 500 rspd['msg'] = u'图片上传失败,可能是网络问题或图片太大,请刷新本页再上传' else: rspd['msg'] = u'没有上传图片' self.write(json.dumps(rspd)) else: self.error(403) self.write('403:forbidden')
def upload_photo_sub(): print("THIS IS MY FUNCTION") if request.method == 'POST': print("POSTTTTTTTTT!!!!!") print(request.files) if 'my_photo' not in request.files: print("no file part") flash('No file part') return redirect("/upload_gb_photo") file = request.files['my_photo'] print(dir(file)) if file.filename == '': print("no selected fileeeeee") flash('No selected file') return redirect("/upload_gb_photo") lat_data = request.form["hiddenLat"] long_data = request.form["hiddenLong"] submission_timestamp = request.form["hiddenTime"] user_date = request.form["gbPhotoDate"] if lat_data == '': flash("No location! Please click the map to mark photo location") return redirect("/upload_gb_photo") file.save('static/photos/' + file.filename) # lat_data = request.form["hiddenLat"] # long_data = request.form["hiddenLong"] # submission_timestamp = request.form["hiddenTime"] # user_date = request.form["gbPhotoDate"] gb_photo = Photo(photo_blob=file.filename, submitted_by=session['user_id'], photo_lat=lat_data, photo_long=long_data, submission_date=submission_timestamp, user_date=user_date) print(session['user_id']) db.session.add(gb_photo) db.session.commit() flash('photo successfully added!') print(type(gb_photo.photo_blob)) return render_template("submit_gb_form.html", photo=None) print(session) # Serve page! return render_template("submit_gb_form.html", photo=None)
def get(self, photo_id=0): user_id, user = self.get_user() if not user or not user.admin: self.redirect('/') return photo_id = int(photo_id) if photo_id == 0: photos = list(Photo.query().fetch()) else: photo = Photo.get_by_id(photo_id) if not photo: data = { 'user': user, 'page_title': 'Exif data - no such photo', 'message': 'no photo exists with this id', } self.render('error.html', **data) photos = [photo] results = [] for photo in photos: exif = blob_exif(photo.blob) results.append(( photo, exif, )) photo.populate(**exif) photo.put() data = { 'user': user, 'page_title': 'Exif data extractor', 'photos': results, } self.render('help/exif.html', **data)
def upload_files(files, collection_id): # Go through each file, create a new Photo() and upload to S3 for file in files: file.filename = secure_filename(file.filename) s3_key = upload_file_to_s3(file, bucket, collection_id) byte = get_photo_bytestring_from_s3(bucket, s3_key) (photo_width, photo_height) = get_photo_width_height(byte) db.session.add( Photo(collection_id=collection_id, s3_key=s3_key, byte_string=byte, width=photo_width, height=photo_height)) db.session.commit()
def upload_photo(): file = request.files['image'] site_id = request.form['site_id'] new_photo = Photo(photo_blob=file.read(), user_id=session['user_id'], site_id=site_id, date=datetime.datetime.today().date()) db.session.add(new_photo) db.session.commit() return redirect('/sites/{}'.format(site_id))
def seed_photos_by_userid(user_id, sort='interesting', per_page=100): """ Seed a user's photos into database given a user_id. sort: faves, views, comments or interesting. per_page: The maximum value is 500. """ params = base_params() params['method'] = "flickr.photos.getPopular" params['user_id'] = user_id params['sort'] = 'interesting' params['extras'] = ','.join( ['description', 'date_taken', 'owner_name', 'geo', 'tags', 'url_q']) params['per_page'] = per_page response = requests.get(API_URL, params=params).json() photos = response['photos']['photo'] for p in photos: photo_id = p['id'].encode('utf-8') if db.session.query(Photo).get(photo_id) is None: user_id = p['owner'].encode('utf-8') username = p['ownername'].encode('utf-8') description = p['description']['_content'].encode('utf-8') tags = p['tags'].encode('utf-8') title = p['title'].encode('utf-8') url = p['url_q'].encode('utf-8') date_taken = p['datetaken'].encode('utf-8') lat = p['latitude'] lon = p['longitude'] if lat == 0 and lon == 0: country_code = None else: country_code = rg.search((lat, lon))[0]['cc'] photo = Photo(photo_id=photo_id, user_id=user_id, username=username, description=description, tags=tags, title=title, url=url, date_taken=date_taken, lat=lat, lon=lon, country_code=country_code) db_utils.add_photo(photo) else: pass
def upload(): # check if the post request has the file part if 'file' not in request.files: flash('No file part') return redirect('/library') file = request.files['file'] # if user does not select file, browser also # submit an empty part without filename if file.filename == '': flash('No selected file') return redirect('/library') if file and allowed_photo_filename(file.filename): new_photo = Photo(user_id=session['user_id']) db.session.add(new_photo) db.session.flush() filename = f'{new_photo.photo_id}_{secure_filename(file.filename)}' s3.upload(file, filename) new_photo.original_photo = filename db.session.commit() return redirect(f'/processing/{new_photo.photo_id}')
def load_photos(photos_filename): print("Photos") Photo.query.delete() for row in open(photos_filename): row = row.rstrip() file_path, file_name, trip_id, pp_id = row.split(",") photo = Photo(file_path=file_path, file_name=file_name, trip_id=trip_id, pp_id=pp_id) # We need to add to the session or it won't ever be stored db.session.add(photo) # Once we're done, we should commit our work db.session.commit()
def get_photo_by_url(self, url): """ 根据 url 从数据库表 bai_photo 里取一行(python会自动处理成字典类型),转换成程序中的一篇 photo :param url: 一个 key, 编码为 unicode :return: Photo 类的一个实例 """ sql = "select * from bai_photo where url='{0}'".format(url) CrawlerLogger.logger.info('get photo SQL: {0}\n'.format(sql)) MysqlLogger.logger.info('get photo SQL: {0}\n'.format(sql)) photo_record = self.db.get(sql) # 字典, get 方法会判断是不是有重复的,有重复的会报错 if photo_record: return Photo.to_photo( photo_record ) # 这里其实不用 return 实例吧?return true 给 is_exist 判断就行了吧? return None
def execute_import(self): """ Performs the actual import, taking information from the session file and applying the settings/whatever to the various images in the import dir """ post_args = parse_qs(self._env["wsgi.input"].read()) if "import_id" not in post_args.keys(): raise Exception("need valid import_id") session_data = None session_id = post_args["import_id"][0] session_file_path = os.path.join("/tmp", "%s.txt" % session_id) with open(session_file_path, "r") as handle: session_data = json.loads(handle.read()) import_dir = os.path.join(S.IMPORT_DIR, session_data["rel_dir"]) success_results = [] deleted_results = [] failed_results = [] for file_hash in session_data["file_listing"].keys(): file_data = session_data["file_listing"][file_hash] fc = util.FileContainer.from_dict(file_data["file_data"]) result = { "filename": fc.name, "from": fc.rel_path } try: if fc.marked: to_remove = fc.file_path fc.destroy() Logger.info("not importing %s because it is marked" % to_remove) os.remove(to_remove) result["to"] = "deleted" result["id"] = None deleted_results.append(result) else: p = Photo.from_file_container(fc) p.move_file(src_dir=p.path, copy=False) p.get_or_create_thumb(Photo.SMALL_THUMB_SIZE) p.get_or_create_thumb(Photo.MEDIUM_THUMB_SIZE) p.store() result["id"] = p.id result["to"] = p.rel_path success_results.append(result) except Exception, e: result["error"] = str(e) Logger.error("was not able to do something: %s" % str(e)) failed_results.append(result)
def test_should_not_create_photo_without_id(self): test_photo = Photo().from_json( json.loads(''' {"code": "XxxXXxxXxxx", "dimensions": {"width": 1080, "height": 1349}, "comments_disabled": false, "owner": {"id": "11111111"}, "comments": {"count": 134}, "caption": "#sunday#black#girl#selfie", "likes": {"count": 12}, "date": 1476637330, "thumbnail_src": "https://scontent-waw1-1.cdninstagram.com/whatever/whatever", "is_video": false, "id": "", "display_src": "https://scontent-waw1-1.cdninstagram.com/whatever/whatever"}''' )) self.assertTrue(test_photo == None)
def _photo_with_high_score(self, data): '''Find the photograph(s) with the highest score. photo_score / (photos_in_comp - 1) Note: only need to consider first placed photos. ''' results = defaultdict(list) for photo in Photo.query(Photo.position == 1): comp = photo.competition.get() photo_count = comp.users().count() # max score for photo in a competition: 10 * (photo_count - 1) percent_score = photo.total_score / (10.0 * (photo_count - 1)) results[percent_score].append(photo) max_score = max(results.keys()) for photo in results[max_score]: data[photo.user.id()].high_score_photo += 1
def execute_import(self): """ Performs the actual import, taking information from the session file and applying the settings/whatever to the various images in the import dir """ post_args = parse_qs(self._env["wsgi.input"].read()) if "import_id" not in post_args.keys(): raise Exception("need valid import_id") session_data = None session_id = post_args["import_id"][0] session_file_path = os.path.join("/tmp", "%s.txt" % session_id) with open(session_file_path, "r") as handle: session_data = json.loads(handle.read()) import_dir = os.path.join(S.IMPORT_DIR, session_data["rel_dir"]) success_results = [] deleted_results = [] failed_results = [] for file_hash in session_data["file_listing"].keys(): file_data = session_data["file_listing"][file_hash] fc = util.FileContainer.from_dict(file_data["file_data"]) result = {"filename": fc.name, "from": fc.rel_path} try: if fc.marked: to_remove = fc.file_path fc.destroy() Logger.info("not importing %s because it is marked" % to_remove) os.remove(to_remove) result["to"] = "deleted" result["id"] = None deleted_results.append(result) else: p = Photo.from_file_container(fc) p.move_file(src_dir=p.path, copy=False) p.get_or_create_thumb(Photo.SMALL_THUMB_SIZE) p.get_or_create_thumb(Photo.MEDIUM_THUMB_SIZE) p.store() result["id"] = p.id result["to"] = p.rel_path success_results.append(result) except Exception, e: result["error"] = str(e) Logger.error("was not able to do something: %s" % str(e)) failed_results.append(result)
def get(self, photo_id=0): '''View a photograph''' user_id, user = self.get_user() photo_id = int(photo_id) photo = Photo.get_by_id(photo_id) if not photo: data = { 'page_title': 'Error', 'user': user, 'error_msg': 'Could not find photograph.' } self.render('error.html', **data) return can_view = self._can_view_photo(photo, user) if not can_view: msg = ( 'You cannot view pictures in competitions which are not ' 'finished.' ) data = { 'page_title': 'Cannot view picture', 'user': user, 'error_msg': msg } self.render('error.html', **data) return data = { 'page_title': 'Photo', 'page_subtitle': photo.title, 'user': user, 'userid': user_id, 'photoid': photo_id, 'can_comment': self._can_comment(user, photo), 'delete_my_photo': self._can_delete(user, photo), 'url': photo.url(), 'title': photo.title, 'comments': list(Comment.photo_comments(photo)) } data.update(photo.exif()) self.render('photo.html', **data)
def get_marked_photos(self): """ Renders a list of marked files """ offset = self._get_from_query("page", 1) - 1 limit = self._get_from_query("limit", S.DEFAULT_PER_PAGE) photos = Photo.get_marked() num_photos = len(photos) start_index = (offset * limit) + 1 end_index = num_photos tokens = { "photos": photos, "offset": offset, "limit": limit, "start_index": start_index, "end_index": end_index, "num_photos": num_photos } return self.construct_response(Template.render("photos/mark.html", tokens))
def get(self): user_id, user = self.get_user() if not user or not user.admin: self.redirect('/') return photos = list(Photo.query().fetch()) for photo in photos: comment_count = len(list(photo.comments())) photo.comment_count = comment_count photo.put() data = { 'user': user, 'page_title': 'Helps', 'photos': photos, } self.render('help/comments.html', **data)