def add_link(db, userid):
    '''Adds a link to the list.
    On success, returns the entry created.'''
    if 'application/json' not in request.content_type:
        return HTTPError(415, "Only json is accepted")
    # Check required fields
    if ('url' not in request.json or request.json['url'] is None
            or len(request.json['url']) < 1):
        return HTTPError(400, "Must specify a url")

    # Sha is optional, generate if not present
    if 'sha' not in request.json:
        request.json['sha'] = binascii.b2a_hex(os.urandom(15))

    args = [userid, request.json['url'], request.json['sha']]
    stmt = 'INSERT INTO links (userid, url, sha) VALUES(?, ?, ?)'

    db.execute(stmt, args)

    if db.total_changes > 0:
        # Regid is optional to provide from the client
        # If present, it will not receive a GCM msg
        regid = None
        if 'regid' in request.query:
            regid = request.query['regid']
        send_link(userid, request.json['sha'], regid)

    return get_link(db, request.json['sha'], userid)
示例#2
0
def add_link(db, userid):
    '''Adds a link to the list.
    On success, returns the entry created.'''
    if 'application/json' not in request.content_type:
        return HTTPError(415, "Only json is accepted")
    # Check required fields
    if ('url' not in request.json or request.json['url'] is None
        or len(request.json['url']) < 1):
        return HTTPError(400, "Must specify a url")

    # Sha is optional, generate if not present
    if 'sha' not in request.json:
        request.json['sha'] = binascii.b2a_hex(os.urandom(15))

    args = [userid,
            request.json['url'],
            request.json['sha']]
    stmt = 'INSERT INTO links (userid, url, sha) VALUES(?, ?, ?)'

    db.execute(stmt, args)

    if db.total_changes > 0:
        # Regid is optional to provide from the client
        # If present, it will not receive a GCM msg
        regid = None
        if 'regid' in request.query:
            regid = request.query['regid']
        send_link(userid, request.json['sha'], regid)

    return get_link(db, request.json['sha'], userid)
示例#3
0
    def add_link(self, request):
        current_user = endpoints.get_current_user()
        if current_user is None:
            raise endpoints.UnauthorizedException('Invalid token.')

        # Generate an ID if one wasn't included
        sha = request.sha
        if sha is None:
            sha = binascii.b2a_hex(os.urandom(15))
        # Construct object to save
        link = LinkModel(key=ndb.Key(LinkModel, sha),
                         sha=sha,
                         url=request.url,
                         deleted=request.deleted,
                         userid=current_user)
        # And save it
        link.put()

        # Notify through GCM
        send_link(link, request.regid)

        # Return a complete link
        return Link(url = link.url,
                    sha = link.sha,
                    timestamp = datetime_to_string(link.timestamp))
    def add_link(self, request):
        current_user = endpoints.get_current_user()
        if current_user is None:
            raise endpoints.UnauthorizedException('Invalid token.')

        # Generate an ID if one wasn't included
        sha = request.sha
        if sha is None:
            sha = binascii.b2a_hex(os.urandom(15))
        # Construct object to save
        link = LinkModel(key=ndb.Key(LinkModel, sha),
                         sha=sha,
                         url=request.url,
                         deleted=request.deleted,
                         userid=current_user)
        # And save it
        link.put()

        # Notify through GCM
        send_link(link, request.regid)

        # Return a complete link
        return Link(url=link.url,
                    sha=link.sha,
                    timestamp=datetime_to_string(link.timestamp))
示例#5
0
def delete_link(db, sha, userid):
    '''Deletes a specific link from the list.
    On success, returns an empty response'''
    db.execute('UPDATE links SET deleted = 1, timestamp = CURRENT_TIMESTAMP \
    WHERE sha IS ? AND userid is ?', [sha, userid])

    if db.total_changes > 0:
        # Regid is optional to provide from the client
        # If present, it will not receive a GCM msg
        regid = None
        if 'regid' in request.query:
            regid = request.query['regid']
        send_link(userid, sha, regid)

    return {}
def delete_link(db, sha, userid):
    '''Deletes a specific link from the list.
    On success, returns an empty response'''
    db.execute(
        'UPDATE links SET deleted = 1, timestamp = CURRENT_TIMESTAMP \
    WHERE sha IS ? AND userid is ?', [sha, userid])

    if db.total_changes > 0:
        # Regid is optional to provide from the client
        # If present, it will not receive a GCM msg
        regid = None
        if 'regid' in request.query:
            regid = request.query['regid']
        send_link(userid, sha, regid)

    return {}
示例#7
0
    def delete_link(self, request):
        current_user = endpoints.get_current_user()
        if current_user is None:
            raise endpoints.UnauthorizedException('Invalid token.')

        link_key = ndb.Key(LinkModel, request.sha)
        link = link_key.get()
        if link is not None:
            link.deleted = True
            link.put()
        else:
            raise endpoints.NotFoundException('No such item')

        # Notify through GCM
        send_link(link, request.regid)

        return message_types.VoidMessage()
    def delete_link(self, request):
        current_user = endpoints.get_current_user()
        if current_user is None:
            raise endpoints.UnauthorizedException('Invalid token.')

        link_key = ndb.Key(LinkModel, request.sha)
        link = link_key.get()
        if link is not None:
            link.deleted = True
            link.put()
        else:
            raise endpoints.NotFoundException('No such item')

        # Notify through GCM
        send_link(link, request.regid)

        return message_types.VoidMessage()