Beispiel #1
0
    def test_init(self):

        # Dimensions need rounding
        picture = Picture(user_id=4, width=10.2, height=5.7)
        pic = ar.Pic(picture=picture, margin=1)
        self.assertEqual(pic.w, 12)
        self.assertEqual(pic.h, 7)
        self.assertIsNone(pic.x1)
        self.assertIsNone(pic.x2)
        self.assertIsNone(pic.y1)
        self.assertIsNone(pic.y2)

        # Only one dimension will be rounded
        picture = Picture(user_id=4, width=10, height=5.7)
        pic = ar.Pic(picture=picture, margin=1)
        self.assertEqual(pic.w, 11)
        self.assertEqual(pic.h, 7)
        self.assertIsNone(pic.x1)
        self.assertIsNone(pic.x2)
        self.assertIsNone(pic.y1)
        self.assertIsNone(pic.y2)

        # No margin
        picture = Picture(user_id=4, width=10, height=5.7)
        pic = ar.Pic(picture=picture, margin=0)
        self.assertEqual(pic.w, 10)
        self.assertEqual(pic.h, 6)
        self.assertIsNone(pic.x1)
        self.assertIsNone(pic.x2)
        self.assertIsNone(pic.y1)
        self.assertIsNone(pic.y2)
def add_picture(name, description, picture_link):

    product_object = Picture(name=name,
                             description=description,
                             picture_link=picture_link)
    session.add(product_object)
    session.commit()
Beispiel #3
0
def create_picture(collection_id, url):
    """Create and return a new picture."""

    new_picture = Picture(collection_id=collection_id, url=url)

    db.session.add(new_picture)
    db.session.commit()

    return new_picture
Beispiel #4
0
def load_pictures():

    for _ in range(0, 30):
        new_feeling = random.choice(feels)
        print new_feeling
        # photos = fApi.get_photos(new_feeling)
        photos = get_giphy(feels)
        pic = Picture(giphy_url=photos)
        db.session.add(pic)

    db.session.commit()
Beispiel #5
0
def make_listing_profile():
    """add listing info to database"""

    roommates = request.form.getlist("roommates")

    laundry = True if request.form.get("laundry") == "True" else False
    pets = True if request.form.get("pets") == "True" else False
    living_there = True if request.form.get(
        "living_there") == "True" else False
    listing_id = request.form.get("listing_name")
    user_id = session["current_user"]

    main_photo = functions.save_photo("main_photo")
    photos = []
    photos.append(functions.save_photo("photo_1"))
    photos.append(functions.save_photo("photo_2"))
    photos.append(functions.save_photo("photo_3"))

    kwargs = dict(listing_id=listing_id,
                  address=request.form.get("address"),
                  neighborhood=request.form.get("neighborhood"),
                  price=int(request.form.get("price")),
                  avail_as_of=request.form.get("avail_date"),
                  length_of_rental=request.form.get("duration"),
                  main_photo=main_photo,
                  bedrooms=request.form.get("bedrooms"),
                  bathrooms=request.form.get("bathrooms"),
                  laundry=laundry,
                  pets=pets,
                  description=request.form.get("description"),
                  primary_lister=user_id)

    if Listing.query.get(listing_id):
        flash("Listing Name Taken")
        return redirect("/listings/{}".format(listing_id))
    else:
        for key in kwargs.keys():
            if kwargs[key] == "":
                del kwargs[key]
        db.session.add(Listing(**kwargs))
        db.session.commit()

        if living_there:
            functions.add_UserListing(user_id, listing_id)

        for roommate in roommates:
            functions.add_UserListing(roommate, listing_id)

        for photo in photos:
            db.session.add(Picture(listing_id=listing_id, photo=photo))
        db.session.commit()

        return redirect("/listings/{}".format(listing_id))
Beispiel #6
0
def load_pictures():
    """Load pictures from seed data into database"""

    print "Pictures"

    with open("seed_data/pictures.txt") as pictures:
        for picture in pictures:
            picture = picture.rstrip()
            picture_id, listing_id, photo = picture.split("|")

            picture = Picture(picture_id=picture_id,
                              listing_id=listing_id,
                              photo=photo)

            db.session.add(picture)

    db.session.commit()
Beispiel #7
0
def upload_image():
    # if "file" not in request.files:
    #     return make_response(jsonify({
    #         "msg": "Coudn't upload the file"
    #     }), 400)

    images = request.files
    permission = request.form
    imageList = []
    # keyList = sorted(images.keys())
    print(permission)
    # print(images['is_private0'])
    i = 0
    for key in images.keys():
        file = images[key]
        hardcoded_privacy_key = "is_private" + str(i)
        hardcoded_caption_key = "caption" + str(i)

        is_private = permission[hardcoded_privacy_key]
        caption = permission[hardcoded_caption_key]
        print(is_private + " " + caption)
        if file.filename == '':
            return make_response(jsonify({
                "msg": 'File name not found'
            }), 400)
        name = file.filename
        if allowed_file(file.filename):
            now = datetime.now()
            timestamp = str(datetime.timestamp(now))
            filename = file.filename.rsplit('.', 1)
            name = filename[0] + "-" + timestamp + '.' + filename[1]
            file.save(os.path.join(UPLOAD_FOLDER, name))
            path = 'http://127.0.0.1:5000' + url_for('static', filename=name)
            id = get_user_id(request.headers.get('Authorization'))
            if is_private == 'true':
                is_private = True
            else:
                is_private = False
            picture = Picture(id, path, is_private=is_private, picture_caption=caption)
            db.session.add(picture)
            db.session.commit()
            imageList.append(('http://127.0.0.1:5000' + url_for('static', filename=name)))
            i += 1
    return make_response(jsonify({
        'imageList': imageList
    }), 200)
Beispiel #8
0
def load_pictures(seed_file_path):
    """Add sample pictures to database from text file.

    Data file is pipe seperated:
    picture_id | user_id | width | height | image_file |
        picture_name | image_attribution | public
    """

    seed_image_folder_path = "static/img_samples/"

    with open(seed_file_path) as seed_file:
        for line in seed_file:
            tokens = line.rstrip().split("|")

            picture_id = int(tokens[0])
            user_id = int(tokens[1])
            width = float(tokens[2])
            height = float(tokens[3])
            image_raw = tokens[4].strip()
            image_file = (seed_image_folder_path +
                          image_raw) if image_raw else None
            picture_name = tokens[5].strip()
            image_attribution = tokens[6].strip()
            public = tokens[7].strip().lower() == 'public'

            picture = Picture(
                picture_id=picture_id,
                user_id=user_id,
                width=width,
                height=height,
                image_file=image_file,
                picture_name=picture_name,
                image_attribution=image_attribution,
                public=public,
            )

            db.session.add(picture)

        db.session.commit()

    # Reset seq/counter
    result = db.session.query(func.max(Picture.picture_id)).one()
    max_id = int(result[0])
    query = "ALTER SEQUENCE pictures_picture_id_seq RESTART WITH :next_id"
    db.session.execute(query, {'next_id': max_id + 1})
    db.session.commit()
Beispiel #9
0
def upload_photos(event_id):
    """Upload photos to particular event."""

    # gets a file class object
    image = request.files['image']
    # get actual file name
    filename = secure_filename(image.filename)
    # save file into my folder
    image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

    new_photo = Picture(filename=filename,
                        uploader_id=session['user_id'],
                        event_id=event_id)

    db.session.add(new_photo)
    db.session.commit()

    return redirect('/event-page/{event_id}'.format(event_id=event_id))
Beispiel #10
0
 def post(self):
     action = self.param('action')
     picName = self.request.get('picName')
     if(action == 'add'):
         picContent = self.request.get('pic')
         pic = Picture()
         pic.picName = picName
         if(picContent != None):
             pic.picContent = db.Blob(picContent)
             pic.thumbnail = db.Blob(images.resize(picContent,128,128))
         pic.put()
         pic.picLink = '/picture/show?img_id=%s&type=full'%pic.key().id()
         pic.put()
     elif(action == 'edit'):
          key = self.param('id')
          pic = Picture.get_by_id(int(key))
          pic.picName = picName
          pic.put()
     self.redirect('/picture')
def query(dataset_dir: str, query_dir: str, methods: List[AbstractMethod]):
    data = Data(dataset_dir)
    file_names = fnmatch.filter(os.listdir(query_dir), '*.jpg')

    print('Training...')
    texts_recs = [method.train(data.pictures) for method in methods]

    print('Querying...')

    query_pictures = seq(file_names).map(lambda query_name: Picture(query_dir, query_name)).to_list()

    results = []
    for method in methods:
        print('\tRunning method', method.__class__.__name__)
        mres = []
        for picture in tqdm(query_pictures, file=sys.stdout):
            mres.append((picture,) + get_result(method, picture))
        results.append(mres)

    return results, texts_recs
Beispiel #12
0
def save_results():
    """Saving the img and the text from results"""

    # Get form variables
    keyword = request.form["keywords"]
    text_results = request.form["twitter"]
    giphy_url = request.form["giphy"]
    block = request.form["b_text"]
    sentiment = request.form["sentiment"]
    lat = request.form["lat"]
    lng = request.form["long"]

    #Saving current user info into the db
    user_id = session.get("user_id")
    #Saving tweet data to db
    save_twit = Tweet(tweet_text=text_results)
    db.session.add(save_twit)
    db.session.commit()
    #Saving giphy data to db
    save_gif = Picture(giphy_url=giphy_url)
    db.session.add(save_gif)
    db.session.commit()


    new_keyword = Result(keywords=keyword, 
                         tweet_id=save_twit.tweet_id, 
                         giphy_id=save_gif.giphy_id,
                         block_text=block,
                         sentiment=sentiment,
                         generated_at=datetime.datetime.now(),
                         user_id=user_id,
                         lat=lat,
                         lng=lng)

    
    db.session.add(new_keyword)
    db.session.commit()


    return redirect("/users/%s" % user_id)
Beispiel #13
0
def attempt_upload():
    """Returns true after uploading user photo to server then to cloud.

    Otherwise returns false.
    """

    lazy_load_of_upload_imports()

    filename_provided = pictures.save(request.files['picture'])

    width = to_float_from_input(request.form.get('width'))
    height = to_float_from_input(request.form.get('height'))
    name = to_clean_string_from_input(request.form.get('name'), 100)
    # TODO: Validate hight/width roughly match image, are positive and nonzero

    user_id = session.get('user_id', None)

    if filename_provided and width and height and user_id:

        picture = Picture(user_id=user_id, width=width, height=height)
        if name:
            picture.picture_name = name
        db.session.add(picture)
        db.session.flush()

        # Rename file after adding so that the picture_id can be used,
        # this may not really be neccesary to include in the file name.
        filename = rename_picture_on_server(filename_provided, picture.picture_id)
        url = move_picture_to_cloud(filename)
        picture.image_file = url
        db.session.commit()

        return True

    else:
        return False
Beispiel #14
0
    def __init__(self, directory: str):
        self.pictures = []

        file_names = fnmatch.filter(os.listdir(directory), '*.jpg')
        for file_name in file_names:
            self.pictures.append(Picture(directory, file_name))
Beispiel #15
0
async def handle_incoming(request):
    form = await request.post()
    message_sid = form['MessageSid']
    from_number = form['From']
    num_media = int(form.get('NumMedia', 0))
    message = form.get('Body', None)

    session = request.app['session']()
    user = User.get_or_create_user(session, from_number)

    def log(message='', event=None, **kwargs):
        extra = {
            'user_id': user.id,
            'tc_status': user.tc_status,
            'message_sid': message_sid
        }
        if event is not None:
            extra[event] = True
        extra.update(kwargs)
        logger.info(message, extra=extra)

    log('Received message', event='received_message')

    if num_media > 0:
        media_files = [(form.get("MediaUrl{}".format(i), ''),
                        form.get("MediaContentType{}".format(i), ''))
                       for i in range(0, num_media)]
        for media_url, _ in media_files:
            # TODO upload pictures to google cloud storage and delete from twilio servers
            picture = Picture(source_url=media_url,
                              create_time=datetime.now(),
                              message_sid=message_sid)
            print(media_url)
            session.add(picture)
            user.add_pending(picture)

    session.commit()

    worker_loop = request.app['worker']['loop']
    response = None

    if user.tc_status is TCStatus.NOTHING:

        log('New user, sending terms and conditions', 'send_tc')
        response = config.TC_AGREEMENT_TEXT
        user.tc_status = TCStatus.SENT
    elif user.tc_status is TCStatus.SENT:

        if message is not None:
            if message.upper() == 'YES':
                log('Agreed to terms and conditions', 'tc_agreed')
                user.tc_status = TCStatus.AGREED
                response = 'Thank you for agreeing to the terms and conditions.'
                if len(user.pending) > 0:
                    response += " Processing your {} picture(s) now".format(
                        len(user.pending))
                    asyncio.run_coroutine_threadsafe(
                        process_pictures(session, user.pending), worker_loop)
                    # await process_pictures(session, user.pending)
                else:
                    response += " You can now send me pictures to convert"
        else:
            log('Remind to agree to terms and conditions', 'tc_remind')
            response = 'You must agree to the terms and conditions by responding YES before continuing.'
            if len(user.pending) > 0:
                response += " You have {} pictures pending".format(
                    len(user.pending))
            response = ' '.join(response)
    else:

        assert user.tc_status is TCStatus.AGREED
        if num_media > 0:
            log('Received new pictures',
                'more_pictures',
                num_new_pictures=num_media)
            response = "Got it! Hard at work processing {} more /picture(s) for you!".format(
                num_media)
            asyncio.run_coroutine_threadsafe(
                process_pictures(session, user.pending), worker_loop)
            # await process_pictures(session, user.pending)

    session.commit()

    if not response:
        response = "Sorry, I didn't understand that!"
    mesg_resp = MessagingResponse()
    mesg_resp.message(response)
    return Response(text=str(mesg_resp), content_type='text/xml')