예제 #1
0
    def post(self):
        body = request.get_json()
        fields = ['picture_id', 'message']
        if not fields_are_in(body, fields):
            return {'error': 'Missing a field'}, 400
        if is_empy_or_none(body):
            return {'error': 'A field is empty or None'}, 400

        # Get current requesting user
        user_id = get_jwt_identity()
        current_user = User.objects(id=user_id).first()

        if current_user is None:
            return {
                'error': 'Header token is not good, please login again'
            }, 401

        picture = Picture.objects(id=body.get('picture_id')).first()

        if picture is None:
            return {'error': 'Picture id does not exist in database'}, 401
        comment = {
            'user': current_user.username,
            'message': body.get('message')
        }

        comment = Comment(**comment)
        comment.save()

        Picture.objects(id=body.get('picture_id')).update_one(
            push__comments=comment)
        Picture.objects(id=body.get('picture_id')).update_one(
            nb_comments=picture.nb_comments + 1)

        return {'message': 'Comment successfully added to picture'}, 200
예제 #2
0
def pictureTestData():
    newPic = Picture(picture_id = 1, file_name = "garden.jpg", thumbnail_name = "thumbnail_garden.png", project_id = 1)
    newPic1 = Picture(picture_id = 2, file_name = "corner.jpg", thumbnail_name = "thumbnail_corner.png", project_id = 1)
    newPic2 = Picture(picture_id = 3, file_name = "backyard.png", thumbnail_name = "thumbnail_backyard.png", project_id = 2)
    dbSession.add(newPic)
    dbSession.add(newPic1)
    dbSession.add(newPic2)
    dbSession.commit()
예제 #3
0
    def post(self):
        body = request.get_json()
        fields = ['picture_id']
        if not fields_are_in(body, fields):
            return {'error': 'Missing a field'}, 400
        if is_empy_or_none(body):
            return {'error': 'A field is empty or None'}, 400

        # Get current requesting user
        user_id = get_jwt_identity()
        current_user = User.objects(id=user_id).first()

        if current_user is None:
            return {
                'error': 'Header token is not good, please login again'
            }, 401

        picture = Picture.objects(id=body.get('picture_id')).first()

        if picture is None:
            return {'error': 'Picture id does not exist in database'}, 401

        if current_user.username in picture.liked_by:
            Picture.objects(id=body.get('picture_id')).update_one(
                pull__liked_by=current_user.username)
            Picture.objects(id=body.get('picture_id')).update_one(
                nb_likes=picture.nb_likes - 1)
            return {'message': 'Successfully disliked picture'}, 200

        Picture.objects(id=body.get('picture_id')).update_one(
            push__liked_by=current_user.username)
        Picture.objects(id=body.get('picture_id')).update_one(
            nb_likes=picture.nb_likes + 1)
        return {'message': 'Successfully liked picture'}, 200
예제 #4
0
    def post(self):
        body = request.get_json()
        fields = ['link', 'message']
        if not fields_are_in(body, fields):
            return {'error': 'Missing a field'}, 400
        if is_empy_or_none(
                dict({
                    'link': body.get('link'),
                    'message': body.get('message')
                })):
            return {'error': 'A field is empty or None'}, 400

        # Get current requesting user
        user_id = get_jwt_identity()
        current_user = User.objects(id=user_id).first()

        if current_user is None:
            return {
                'error': 'Header token is not good, please login again'
            }, 401

        picture = {
            'user': current_user.username,
            'owner': current_user.username,
            'link': body.get('link'),
            'message': body.get('message'),
            'date': datetime.datetime.now(),
            'nb_likes': 0,
            'nb_comments': 0,
        }
        picture = Picture(**picture)
        picture.save()

        User.objects(id=user_id).update_one(push__image_queue=picture)
        User.objects(id=user_id).update_one(push__pictures=picture)
        User.objects(id=user_id).update_one(
            nb_pictures=current_user.nb_pictures + 1)

        User.objects(following__in=[current_user]).update_one(
            push__image_queue=picture)

        return {
            'message':
            'Picture successfully added to user {} '.format(
                current_user.username)
        }, 200
예제 #5
0
파일: app.py 프로젝트: alexkryu4kov/autism
def return_picture():
    exp = Experiment()
    if request.method == 'GET':
        db.session.add(
            Picture(id='2', name='2', category='animal', picture='vk.com/'))
        db.session.commit()
        return 'success'
    if request.method == 'POST':
        pictures = db.session.query(Picture)
        # for picture in pictures:
        #    return picture.picture
        # return jsonify(pictures[exp.choose_elem()].picture)
        return jsonify(pictures[1].picture)
예제 #6
0
파일: pictures.py 프로젝트: mswiate/BowLegs
def create_picture(img):
    filename = secure_filename(img.filename)
    content_type = img.content_type
    id = str(uuid.uuid4())
    file_path = "{}/{}.png".format(DATA_DIR, id)
    img.save(file_path)
    picture = Picture(id, file_path)
    db_session.add(picture)
    db_session.commit()

    producer = KafkaProducer(
        bootstrap_servers=settings.KAFKA_BOOTSTRAP_SERVERS)
    data = str.encode(picture.id)
    producer.send(settings.KAFKA_TOPIC, data)
    producer.flush()

    return picture
예제 #7
0
    def get(self, username):
        # Get current requesting user
        user_id = get_jwt_identity()
        current_user = User.objects(id=user_id).first()

        if current_user is None:
            return {'error': 'Header token is not good, please login again'}, 401

        user_info = User.objects(username=username).first()

        if user_info is None:
            return {'error': 'User {} does not exist'.format(username)}, 401

        user_info = json.loads(user_info.to_json())

        del user_info['password']
        del user_info['image_queue']
        del user_info['_id']
        del user_info['nb_login']
        del user_info['dates']
        del user_info['following']

        user_info['already_follow'] = False

        for user in user_info['followers']:
            if user['$oid'] == user_id:
                user_info['already_follow'] = True
                break
        del user_info['followers']

        for pic in range(len(user_info['pictures'])):
            user_info['pictures'][pic] = json.loads(Picture.objects(id=user_info['pictures'][pic]['$oid']).first().to_json())
            user_info['pictures'][pic]['id'] = user_info['pictures'][pic]['_id']['$oid']
            del user_info['pictures'][pic]['_id']
            del user_info['pictures'][pic]['date']
            del user_info['pictures'][pic]['owner']
            for com in range(len(user_info['pictures'][pic]['comments'])):
                user_info['pictures'][pic]['comments'][com] = json.loads(Comment.objects(id=user_info['pictures'][pic]['comments'][com]['$oid']).first().to_json())
                del user_info['pictures'][pic]['comments'][com]['_id']
        user_info['pictures'] = user_info['pictures'][::-1]

        return Response(json.dumps(user_info), mimetype="application/json", status=200)
예제 #8
0
def uploadPicture():
    """ Saves the image and adds the picture name to a related project """
    if request.method == 'POST':
        project_id = request.form['proj_id']
        picture = request.files['picture']
        # Parse file type and file name
        filename = secure_filename(picture.filename)
        filename, file_extension = os.path.splitext(filename)
        if filename != '':
            # Generate a unique file name to store the file
            filename = str(uuid.uuid4())
            try:
                # Store filepath into the database
                newPicture = Picture(project_id=project_id,
                                     file_name=filename + file_extension,
                                     thumbnail_name=thumbnailPrefix +
                                     filename + thumbnailExt)
                dbSession.add(newPicture)

                # Save picture in the picture directory
                picturePath = os.path.join(app_root, pictureDir,
                                           filename + file_extension)
                picture.save(picturePath)

                # Create thumbnail of picture
                thumb = Image.open(picturePath)

                # Source: javiergodinez.blogspot.ca/2008/03/square-thumbnail-with-python-image.html
                width, height = thumb.size

                if width > height:
                    delta = width - height
                    left = int(delta / 2)
                    upper = 0
                    right = height + left
                    lower = height
                else:
                    delta = height - width
                    left = 0
                    upper = int(delta / 2)
                    right = width
                    lower = width + upper

                thumb = thumb.crop((left, upper, right, lower))
                thumb.thumbnail(thumbnailSize, Image.ANTIALIAS)

                # Save thumbnail in the thumbnail directory
                thumbnailPath = os.path.join(
                    app_root, thumbnailDir,
                    thumbnailPrefix + filename + thumbnailExt)
                thumb.save(thumbnailPath)
                thumb.close()

                # Commit and return ok status
                dbSession.commit()
                return created_request("Picture was uploaded")
            except:
                return bad_request(
                    "Invalid project id or an error when saving the file has occured"
                )
        return bad_request("No file provided")