示例#1
0
def friends(friend):
    from utils.PetsHelper import query_from_pet_name
    from utils.FriendsHelper import pending_friend, friend_from_request, ask_friend, decorate_friends_as_peto
    from storage.models import sanitized_collection, commit

    error = Error()
    friends = {}

    try:
        peto = petsOwner_from_session()
        # friends list
        if request.method == 'GET':
            pass

        # add friend
        if request.method == 'POST':
            # check first that he won't add himself
            seed = friend  # label
            if seed == peto.seed:
                # can't add himself
                raise ErrorException(Error(code=Error_code.FRNDALONE))
            else:
                # add friend
                friend = friend_from_request(seed)
                ask_friend(peto=peto, friend=friend)
                commit()

    except ErrorException as e:
        error = e.error
        pet = {}

    # decorate friends as petowner. Sinon le client devra récupérer chaque id
    # de la liste des friends.
    friends = decorate_friends_as_peto(peto.friends_to)
    return jsonify({"error": error.to_dict(), "friends": friends})
示例#2
0
def pets_badge(pet_name):
    from images_upload.uploader import upload_file
    from utils.PetsHelper import query_from_pet_name
    from storage.models import commit, sanitizer
    error   = Error()
    pet     = {}

    try:
        peto = petsOwner_from_session()
        pet  = query_from_pet_name(peto, pet_name)

        if request.method == 'GET':
            pass

        elif request.method == 'PUT':
            #TODO validation
            path = upload_file(request.files["image"], bucketName=Bucket.BADGE.value)
            pet.url_badge = path
            commit()

        elif request.method == 'DELETE':
            #TODO validation
            path = upload_file(request.files["image"], bucketName=Bucket.BADGE.value)
            pet.url_badge = path
            commit()

    except ErrorException as e:
        error = e.error

    return jsonify({"error" : error.to_dict(), "woof" : sanitizer(pet)})
示例#3
0
def pets():
    from storage.models import PetsOwner, sanitized_collection, commit, put_sanitized
    from utils.PetsHelper import new_pet, put_from_sanitized

    error = Error()
    pets  = {}

    try:
        peto = petsOwner_from_session()

        # • Retourne l'utilisateur actuel.
        if request.method == 'GET':
            pets = peto.pets

        # • Modifie l'utilisateur.
        elif request.method == 'POST':
            data        = request.get_json()
            sanitized   = schema.validate_pet(data)
            pet         = new_pet(peto)
            # mutate and save
            put_from_sanitized(sanitized, pet, peto)
            commit()
            pets        = peto.pets

    except ErrorException as e:
        error = e.error

    return jsonify({"error" : error.to_dict(), "pets" : sanitized_collection(pets)})
示例#4
0
def pets_pet(pet_name):
    from utils.PetsHelper import query_from_pet_name, put_from_sanitized
    from utils.UserHelper import petsOwner_from_session
    from storage.models import Pet, commit, delete_n_commit, sanitizer

    error = Error()
    pet   = {}
    try:
        peto    = petsOwner_from_session()
        pet     = query_from_pet_name(peto, pet_name)

        if request.method == 'GET':
            pass

        elif request.method == 'PUT':
            data      = request.get_json()
            sanitized = schema.validate_pet(data)
            put_from_sanitized(sanitized, pet, peto)
            commit()

        elif request.method == 'DELETE':
            delete_n_commit(pet)
            pet = {}

    except ErrorException as e:
        error = e.error
        pet   = {}

    return jsonify({"error" : error.to_dict(), "woof" : sanitizer(pet)})
示例#5
0
def me_profil():
    from storage.models import PetsOwner, delete_n_commit, sanitizer, commit

    error = Error()
    peto = {}
    try:
        peto = petsOwner_from_session()
        # • Retourne l'utilisateur actuel.
        if request.method == 'GET':
            pass

        # • Modifie l'utilisateur.
        elif request.method == 'PUT':
            data = request.get_json()
            sanitized = schema.validate_me(data)
            put_sanitized(sanitized, peto)
            commit()

        # • Supprime l'utilisateur.
        elif request.method == 'DELETE':
            delete_n_commit(peto)
            peto = PetsOwner()

    except ErrorException as e:
        error = e.error

    return jsonify({"error": error.to_dict(), "me": sanitizer(peto)})
示例#6
0
def pets_feeds(pet_name, current_page):
    from images_upload.uploader import upload_file
    from utils.PetsHelper import query_from_pet_name, new_feed, query_from_feed_uuid
    from storage.models import commit, sanitized_collection, merge_dicts, put_sanitized
    from storage.models import Feed, delete_n_commit
    from sqlalchemy import and_, desc

    error = Error()
    feeds = {}
    pages = {}

    try:
        def represents_int(s):
            try:
                int(s)
                return True
            except ValueError:
                return False

        if represents_int(pet_name):
            raise ErrorException(Error(code=Error_code.NOTIMPL))

        if isinstance(pet_name, int):
            raise ErrorException(Error(code=Error_code.NOTIMPL))

        peto = petsOwner_from_session()

        if request.method == 'GET':
            pet = query_from_pet_name(peto, pet_name)

            try:
                print "current page", current_page
                _pages  = Feed.query.filter(and_(Feed._pet_id == pet.id)).order_by(desc(Feed.cre_date)).paginate(page=int(current_page), per_page=10)
                feeds   = _pages.items
                #TODO refactor!
                pages = {"total": _pages.total, "page": _pages.page, "per_page": _pages.per_page}
            except: # No op.
                raise ErrorException(Error(code=Error_code.OUTOFSCOPE))

        elif request.method == 'POST':
            pet         = query_from_pet_name(peto, pet_name)
            data        = merge_dicts(request.files, request.form)
            sanitized   = schema.validate_feed(data)
            feed        = new_feed(pet)

            try:
                path = upload_file(sanitized["image"], bucketName=Bucket.FEEDS.value)
            except: # No op.
                raise ErrorException(Error(code=Error_code.WRGDCTYPE))

            # sanitize
            sanitized["url_feed"] = path
            del sanitized["image"]

            put_sanitized(sanitized, feed)
            commit()
            feeds = [feed] # on ne retourne que le post updaté

        elif request.method == 'PUT':
            uuid        = pet_name # label change
            data        = merge_dicts(request.files, request.form)
            sanitized   = schema.validate_feed(data, image_optional=True)
            feed        = query_from_feed_uuid(uuid, peto)
            # note: Les orphan link sont enlevé par un deamon par cycles
            try:
                put_sanitized(sanitized, feed)
            except: # No op.
                raise ErrorException(Error(code=Error_code.WRGDCTYPE))
            commit()
            feeds = [feed]

        elif request.method == 'DELETE':
            uuid        = pet_name # change label
            feed        = query_from_feed_uuid(uuid, peto)
            delete_n_commit(feed)

    except ErrorException as e:
        error = e.error

    result_list = { "error" : error.to_dict(),
                    "feeds" : sanitized_collection(feeds),
                    "pages" : pages}

    return jsonify(result_list)