Exemplo n.º 1
0
def itemaction(item_id, action):
    """
    :URL: /item/<item_id>/<action>
    :Methods: GET, POST

    Update or query the logged in user's record for an item.

    If a POST request is received then the current record is returned instead of a redirect back to the previous page.
    Setting the accept:application/json header will always return JSON regardless of request type.

    :Allowed actions:
     * 'status'    - Return the item's current status
     * 'have'      - Mark an item as part of the user's collection
     * 'donthave'  - Remove the item from the user's collection
     * 'show'      - If the item is in the user's collection mark it as visible to others
     * 'hide'      - If the item is in the user's collection hide it from others
     * 'willtrade' - Mark the item as available for trade
     * 'wonttrade' - Don't show this item as available for trade
     * 'want'      - Add this item to the user's want list
     * 'dontwant ' - Remove this item from the user's want list

    :Sample record:

    .. code-block:: javascript

        {"hidden": 1, "want": 0, "have": 1, "willtrade": 0}
    """

    try:
        user = SiteUser.create(session['username'])
    except (NoUser, KeyError):
        user = None

    def get_record():
        return json.dumps(user.query_collection(item_id).values())

    if action == 'status':
        if not user:
            return '{}'
        else:
            return get_record()

    if user:
        try: 
            ownwant(item_id, user.uid, actions[action])
        except (NoItem, KeyError, ValueError):
            return page_not_found()

        if request.method == 'POST' or request_wants_json():
            return get_record()
    else:
        if request_wants_json():
            return '{}', 400
        flash('You must be logged in to have a collection')

    return redirect_back('/item/' + item_id)
Exemplo n.º 2
0
def itemaction(item_id, action):
    """
    :URL: /item/<item_id>/<action>
    :Methods: GET, POST

    Update or query the logged in user's record for an item.

    If a POST request is received then the current record is returned instead of a redirect back to the previous page.
    Setting the accept:application/json header will always return JSON regardless of request type.

    :Allowed actions:
     * 'status'    - Return the item's current status
     * 'have'      - Mark an item as part of the user's collection
     * 'donthave'  - Remove the item from the user's collection
     * 'show'      - If the item is in the user's collection mark it as visible to others
     * 'hide'      - If the item is in the user's collection hide it from others
     * 'willtrade' - Mark the item as available for trade
     * 'wonttrade' - Don't show this item as available for trade
     * 'want'      - Add this item to the user's want list
     * 'dontwant ' - Remove this item from the user's want list

    :Sample record:

    .. code-block:: javascript

        {"hidden": 1, "want": 0, "have": 1, "willtrade": 0}
    """

    try:
        user = SiteUser.create(session['username'])
    except (NoUser, KeyError):
        user = None

    def get_record():
        return json.dumps(user.query_collection(item_id).values())

    if action == 'status':
        if not user:
            return '{}'
        else:
            return get_record()

    if user:
        try: 
            ownwant(item_id, user.uid, actions[action])
        except (NoItem, KeyError):
            return page_not_found()

        if request.method == 'POST' or request_wants_json():
            return get_record()
    else:
        if request_wants_json():
            return '{}', 400
        flash('You must be logged in to have a collection')

    return redirect_back('/item/' + item_id)
Exemplo n.º 3
0
def pm_action(username, messageid, action):
    """
    :URL: /user/<username>/pm/<messageid>/<action>
    :Methods: GET, POST
    :Actions:
        * read
        * unread
        * delete
        * undelete

    Setting the accept:application/json header will return JSON instead of a redirect.
    """

    pd = PageData()
    dmid = deobfuscate(messageid)

    if not 'username' in session or pd.authuser.username != username or dmid is None:
        return render_template('pm_error.html', pd=pd)

    pm = TradeMessage.create(dmid)
    if action == 'read':
        pm.read(pd.authuser.username)
    elif action == 'unread':
        pm.unread(pd.authuser.username)
    elif action == 'delete':
        pm.delete(pd.authuser.username)
    elif action == 'undelete':
        pm.undelete(pd.authuser.username)

    if request_wants_json():
        return '{}'
    else:
        return redirect_back('/')
Exemplo n.º 4
0
def pm_action(username, messageid, action):
    """
    :URL: /user/<username>/pm/<messageid>/<action>
    :Methods: GET
    :Actions:
        * read
        * unread
        * delete
        * undelete

    Setting the accept:application/json header will return JSON instead of a redirect.
    """

    pd = PageData()
    dmid = deobfuscate(messageid)

    if not 'username' in session or pd.authuser.username != username or dmid is None:
        return render_template('pm_error.html', pd=pd)

    pm = TradeMessage.create(dmid)
    if action == 'read':
        pm.read(pd.authuser.username)
    elif action == 'unread':
        pm.unread(pd.authuser.username)
    elif action == 'delete':
        pm.delete(pd.authuser.username)
    elif action == 'undelete':
        pm.undelete(pd.authuser.username)

    if request_wants_json():
        return '{}'
    else:
        return redirect_back('/')
Exemplo n.º 5
0
def reallydelete_item(item_id):
    try:
        delitem = SiteItem.create(item_id)
    except NoItem: 
        return page_not_found()

    delitem.delete()

    if request_wants_json():
        return '{}'
    else:
        return redirect(url_for('index'))
Exemplo n.º 6
0
def reallydelete_item(item_id):
    try:
        delitem = SiteItem.create(item_id)
    except NoItem:
        return page_not_found()

    delitem.delete()

    if request_wants_json():
        return '{}'
    else:
        pd = PageData()
        pd.title = delitem.name + " has been deleted"
        pd.accessreq = 255
        pd.conftext = delitem.name + " has been deleted. I hope you meant to do that."
        pd.conftarget = ""
        pd.conflinktext = ""

        return render_template('confirm.html', pd=pd)
Exemplo n.º 7
0
def tag_search(pd):
    offset = (pd.page - 1) * pd.limit
    results = core.tag_search(pd.query, pd.limit, offset, pd.sort)

    pd.results = results['tags']
    pd.num_results = results['maxresults']
    pd.num_pages = -(-pd.num_results // pd.limit) # round up

    if pd.num_results == 0:
        pd.results = [None]

    if request_wants_json():
        resp = dict()
        resp['results'] = pd.results
        resp['query'] = pd.query
        resp['num_results'] = pd.num_results
        resp['num_pages'] = pd.num_pages
        resp['limit'] = pd.limit
        return json.dumps(resp)

    return render_template('search/tags.html', pd=pd)
Exemplo n.º 8
0
def user_search(pd):
    offset = (pd.page - 1) * pd.limit
    results = core.user_search(pd.query, pd.limit, offset, pd.sort)

    pd.results = results['users']
    pd.num_results = results['maxresults']
    pd.num_pages = -(-pd.num_results // pd.limit) # round up

    if pd.num_results == 0:
        pd.results = [None]

    if request_wants_json():
        resp = dict()
        resp['results'] = list()
        for item in pd.results:
            resp['results'].append(item.values())

        resp['query'] = pd.query
        resp['num_results'] = pd.num_results
        resp['num_pages'] = pd.num_pages
        resp['limit'] = pd.limit
        return json.dumps(resp)

    return render_template('search/users.html', pd=pd)
Exemplo n.º 9
0
def show_item(item_id, edit=None):
    """
    :URLs:
        * /item/<item_id>/history/<edit>
        * /item/<item_id>

    :Methods: GET

    Setting the accept:application/json header will return JSON.

    :Sample response:

    .. code-block:: javascript
    {
        "added": "2016-05-23 20:52:12",
        "body": "",
        "body_rendered": "",
        "description": 384,
        "images": [
            443,
            444
        ],
        "modified": "2016-05-23 20:53:19",
        "name": "SSFC",
        "tags": {
            "FO": false,
            "Front Office": true,
            "MLS": true,
        },
        "uid": 388
    }

    * added         - Date added, always UTC
    * modified      - Late modified, also always UTC
    * name          - Item's name
    * body          - raw unrendered description body
    * body_rendered - rendered content
    * description   - edit identifier
    * images        - array of image ids associated with this item
    * tags          - dict of tags, keys are the tag title. the value is a bool which will be set to true if the tag was directly applied and false if inherited.
    """

    if item_id is 'new':
        return redirect("/item/" + item_id + "/edit")

    try:
        showitem = SiteItem.create(item_id)

        if edit:
            showitem.old = True
            showitem.edit = edit
        else:
            showitem.old = False
            showitem.edit = None

        showitem.description_content = showitem.body(edit)
    except NoItem:
        return page_not_found()

    if request_wants_json():
        values = showitem.values()
        values['body_rendered'] = render_markdown(values['body'])
        return json.dumps(values)
    else:
        pd = PageData()

        pd.title = showitem.name
        pd.item = showitem

        return render_template('item.html', pd=pd)
Exemplo n.º 10
0
def searchitem():
    """
    :URL: /item/search?page=<page>&limit=<max results>&query=<search query>&sort=<sort type>

    :Method: GET

    :Sort Types:
        * name - Alphabetical by name
        * added - By added date, latest first
        * modified - Last modified

    :Sample Response: Setting the accept:application/json header will return JSON. 

    .. code-block:: javascript

    {
        "limit": 2,
        "num_pages": 4,
        "num_results": 8,
        "query": "Cascadia",
        "results": [
            {
                "added": "2016-05-22 17:52:36",
                "body": "Blue/White (Cascadia Fringe, Gisele Currier Memorial Fundraiser)",
                "description": 460,
                "images": [
                    388,
                    389
                ],
                "modified": "2016-05-24 22:45:33",
                "name": "No Pity MLS Blue White Fringe (Cascadia Fringe) 2012",
                "uid": 362
            },
            {
                "added": "2016-05-22 17:02:15",
                "body": "",
                "description": 317,
                "images": [
                    364,
                    365
                ],
                "modified": "2016-05-22 17:02:15",
                "name": "Cascadia",
                "uid": 350
            }
        ]
    }
    """

    pd = PageData()
    pd.query = request.args.get('query')
    pd.limit = request.args.get('limit')
    pd.page = request.args.get('page')
    pd.sort = request.args.get('sort')

    if not pd.limit:
        pd.limit = 20
    else:
        pd.limit = int(pd.limit)

    if not pd.page:
        pd.page = 1
    else:
        pd.page = int(pd.page)

    offset = (pd.page - 1) * pd.limit

    results = core.item_search(pd.query, pd.limit, offset, pd.sort)

    pd.results = results['items']
    pd.num_results = results['maxresults']
    pd.num_pages = -(-pd.num_results // pd.limit)  # round up

    if pd.num_results == 0:
        pd.results = [None]

    if request_wants_json():
        resp = dict()
        resp['results'] = list()
        for item in pd.results:
            resp['results'].append(item.values())

        resp['query'] = pd.query
        resp['num_results'] = pd.num_results
        resp['num_pages'] = pd.num_pages
        resp['limit'] = pd.limit
        return json.dumps(resp)

    return render_template('search.html', pd=pd)
Exemplo n.º 11
0
def show_item(item_id, edit=None):
    """
    :URLs:
        * /item/<item_id>/history/<edit>
        * /item/<item_id>

    :Methods: GET

    Setting the accept:application/json header will return JSON.

    :Sample response:

    .. code-block:: javascript
    {
        "added": "2016-05-23 20:52:12",
        "body": "",
        "body_rendered": "",
        "description": 384,
        "images": [
            443,
            444
        ],
        "modified": "2016-05-23 20:53:19",
        "name": "SSFC",
        "tags": {
            "FO": false,
            "Front Office": true,
            "MLS": true,
        },
        "uid": 388
    }

    * added         - Date added, always UTC
    * modified      - Late modified, also always UTC
    * name          - Item's name
    * body          - raw unrendered description body for the active edit
    * body_rendered - rendered content for the active edit
    * description   - edit identifier
    * images        - array of image ids associated with this item
    * tags          - dict of tags, keys are the tag title. the value is a bool which will be set to true if the tag was directly applied and false if inherited.
    """

    if item_id is 'new':
        return redirect("/item/" + item_id + "/edit")

    try:
        showitem = SiteItem.create(item_id)

        if showitem.deleted:
            return page_not_found()

        if edit:
            edit = int(edit)

        showitem.edit = edit

        if edit and edit not in [int(i.uid) for i in showitem.history()]:
            return page_not_found()
    except (NoItem, ValueError):
        return page_not_found()

    if request_wants_json():
        values = showitem.values(edit)
        values['body_rendered'] = render_markdown(values['body'])

        return json.dumps(values)
    else:
        pd = PageData()

        pd.title = showitem.name
        pd.item = showitem

        return render_template('item.html', pd=pd)