コード例 #1
0
def getForeignAuthor(uuid):
    author = None
    for node in Node.objects.getActiveNodes():

        request = ProfileRequestFactory.create(node)
        response = request.get(uuid)
        if(response.status_code != 200):
            # Node not reachable
            print("Node %s returned us status code %s!!!" % (node.host_name, response.status_code))
            continue

        obj = response.json()

        try:
            author = Author.objects.get(uuid=uuid, node=node)
            # since there is no defined profile JSON, can't expect these to be in the request.
            # best to use obj.get('github_id', Default) which will give you Default if not in the request
            # indexing will throw an error
            author.github_id = obj.get('github_username', "")
            author.about = obj.get('bio', "")
            author.username = obj.get('displayname', "Unknown Author")
            author.avatar = "foreign_avatar.jpg"
            author.save()
            break
        except Author.DoesNotExist:
            author = Author.objects.create(uuid=uuid, username=obj.get('displayname', "Unknown Author"),
                                           node=node, about=obj.get('bio', ""), github_id=obj.get('github_username', ""),
                                           avatar="foreign_avatar.jpg")
            author.save()
            break
    return author
コード例 #2
0
ファイル: utils.py プロジェクト: Roshack/cmput410-project
def get_foreign_profile_data(uuid, node):
    """
    Fetches the profile for the foreign user specified by uuid from the specfied node
    """
    try:
        request = ProfileRequestFactory.create(node)
        response = request.get(uuid)

        if(response.status_code != 200):
            # Node not reachable or some other mishap
            logger.log(response.content)
            logger.log("HTTP %s returned from %s on profile request." % (response.status_code, node.host))
            print("HTTP %s returned from %s on profile request." % (response.status_code, node.host))
            return {}

        data = response.json()
    except NotImplementedError:
        logger.log("Frien Request Factory not implemented for %s." % (node.host))
        data = {}
    except :
        # This probably means we couldn't issue a request, only seen this using local tests
        logger.log("Exception thrown on profile request to %s." % (node.host))
        data = {}

    return data