Example #1
0
def collection():
    collection_id = request.args.get('id')
    collection = Collection.fetch_by_id(collection_id)

    photos = collection.fetch_photos()

    photos_json = json.dumps(photos)
    loaded_photos = json.loads(photos_json)
    return render_template('collection.html',
                           photos=loaded_photos,
                           name=collection.name,
                           info=collection.info)
Example #2
0
def add_camera():
    if 'photo' in session:
        photo = session['photo']
        db_photo = None
        collection_id = None

        taken_coords = photo['taken_coords']

        # Check if the photo is already in the database
        if 'id' in photo.keys():
            db_photo = Photo.fetch_by_id(photo['id'])
            collection_id = db_photo.collection_id

        loc_new = request.form.get("loc_new")
        loc_id = request.form.get("loc_id")

        lat = request.form.get("lat2")
        lon = request.form.get("lon2")

        # Update destination coords
        dest_coords = (lat, lon)
        photo['dest_coords'] = dest_coords

        if loc_id:
            print("add to location")
            location = Location.query.get(loc_id)
            location.add_photo(db_photo)

        else:
            print("new location")
            loc_name = "Untitled"
            if loc_new:
                loc_name = loc_new

            location = Location(lat=lat, lon=lon, name=loc_name)

        if not db_photo:
            db_photo = Photo(pic=photo['pic'],
                             taken=photo['taken'],
                             coords=dest_coords,
                             taken_coords=photo['taken_coords'],
                             loc=location,
                             title=photo['title'])
        else:
            db_photo.update(pic=photo['pic'],
                            taken=photo['taken'],
                            coords=dest_coords,
                            taken_coords=photo['taken_coords'],
                            loc=location,
                            title=photo['title'])

        location.add_photo(db_photo)

        # Remove photo from collection
        if collection_id:
            db_photo.remove_collection()
            collection = Collection.fetch_by_id(collection_id)

            # If collection is empty, delete it
            if len(collection.c_photos) == 0:
                collection.delete()

        # # Add to location
        # new_loc = None
        # # Very very inefficient
        # locations = Location.fetch_all()
        # for location in locations:
        #     if location.equals(lat, lon):
        #         new_loc = location
        #         break

        # if new_loc is None:
        #     new_loc = Location(lat=lat, lon=lon)

        # new_loc.add_photo(Photo(pic=photo['pic'], taken=photo['taken'], coords = dest_coords,
        #                         taken_coords=photo['taken_coords'], loc=new_loc))
        session.pop('photo')
    else:
        return redirect("/")
    return redirect("/")