def _get_spots(self, search_args):
        consumer, client = oauth_initialization()

        #for key in options:
        #    if isinstance(options[key], types.ListType):
        #        for item in options[key]:
        #            args.append("{0}={1}".format(urllib.quote(key), urllib.quote(item)))
        #    else:
        #        args.append("{0}={1}".format(urllib.quote(key), urllib.quote(options[key])))

        #url = "{0}/api/v1/spot/search?{1}".format(settings.SS_WEB_SERVER_HOST, "&".join(search_args))
        url = "{0}/api/v1/spot/all".format(settings.SS_WEB_SERVER_HOST)

        resp, content = client.request(url, 'GET')

        if resp.status != 200:
            raise Exception("Unable to load spots")

        i18n_json = []
        for spot in json.loads(content):
            string_val = ''
            for x in range(0, len(spot['type'])):
                if x is 0:
                    string_val = _(spot['type'][x])
                else:
                    string_val = string_val + ', ' + _(spot['type'][x])

                spot['type'] = string_val
                i18n_json.append(spot)

        return i18n_json
Beispiel #2
0
    def _get_spots(self, search_args):
        consumer, client = oauth_initialization()

        #for key in options:
        #    if isinstance(options[key], types.ListType):
        #        for item in options[key]:
        #            args.append("{0}={1}".format(urllib.quote(key), urllib.quote(item)))
        #    else:
        #        args.append("{0}={1}".format(urllib.quote(key), urllib.quote(options[key])))

        #url = "{0}/api/v1/spot/search?{1}".format(settings.SS_WEB_SERVER_HOST, "&".join(search_args))
        url = "{0}/api/v1/spot/all".format(settings.SS_WEB_SERVER_HOST)

        resp, content = client.request(url, 'GET')

        if resp.status != 200:
            raise Exception("Unable to load spots")

        i18n_json = []
        for spot in json.loads(content):
            string_val = ''
            for x in range(0, len(spot['type'])):
                if x is 0:
                    string_val = _(spot['type'][x])
                else:
                    string_val = string_val + ', ' + _(spot['type'][x])

                spot['type'] = string_val
                i18n_json.append(spot)

        return i18n_json
Beispiel #3
0
def BuildingsView(request):
    # Required settings for the client
    consumer, client = oauth_initialization()

    url_params = []

    for key, value in request.GET.items():
        if key.startswith('oauth_'):
            pass
        else:
            url_params.append('{0}={1}'.format(key, value))

    url = "{0}/api/v1/buildings?{1}".format(settings.SS_WEB_SERVER_HOST, '&'.join(url_params))

    resp, content = client.request(url, 'GET')
    if resp.status == 404:
        url = request.get_host()
        url = url + "/contact"
        raise Http404
    elif resp.status != 200:
        response = HttpResponse("Error loading buildings")
        response.status_code = resp.status_code
        return response

    #
    # FILTER: params["manager"] == REMOTE_USER
    #



    return HttpResponse(content, mimetype='application/json')
Beispiel #4
0
    def _pull(self, method, spot_url):
        consumer, client = oauth_initialization()

        resp, content = client.request(spot_url, "GET")

        if resp.status == 200:
            return json.loads(content) if resp.get("content-type").lower() == "application/json" else content

        raise SpotException({"status_code": resp.status, "status_text": "Error loading spot"})
Beispiel #5
0
    def _pull(self, method, spot_url):
        consumer, client = oauth_initialization()

        resp, content = client.request(spot_url, 'GET')

        if resp.status == 200:
            return json.loads(content) if resp.get('content-type').lower() == 'application/json' else content

        raise SpotException({'status_code': resp.status,
                             'status_text': "Error loading spot"})
Beispiel #6
0
    def _push(self, method, spot_url, spot, user):
        consumer, client = oauth_initialization()

        headers = {"XOAUTH_USER": "******" % user, "Content-Type": "application/json", "Accept": "application/json"}

        if "etag" in spot and len(spot.get("etag")):
            headers["If-Match"] = spot.get("etag")

        resp, content = client.request(spot_url, method=method, body=json.dumps(spot), headers=headers)

        if resp.status == 200 or resp.status == 201:
            return resp, json.loads(content) if content else {}

        raise Exception("Unable to {0} spot: Server response {1}".format(method, resp.status))
Beispiel #7
0
    def get(self):
        # Required settings for the client
        consumer, client = oauth_initialization()

        url = "{0}/api/v1/schema".format(settings.SS_WEB_SERVER_HOST)
        resp, content = client.request(url, 'GET')
        if resp.status == 200:
            schema = json.loads(content)
        else:
            raise SpotSchemaException({
                    'status_code': resp.status,
                    'status_text': "Error loading schema"
                    })

        return schema
Beispiel #8
0
    def _spot_image(self, spot_id, image_id):
        try:
            link = SpotImageLink.objects.get(id=image_id)
        except:
            self.error404_response()  # no return

        # Required settings for the client
        consumer, client = oauth_initialization()
        url = "{0}/api/v1/spot/{1}/image/{2}".format(
            settings.SS_WEB_SERVER_HOST, spot_id, link.image_id)

        resp, content = client.request(url, 'GET')

        if resp.status != 200:
            return self.error_response(resp.status, msg="Error loading image")

        return self.json_response(content)
    def _spot_image(self, spot_id, image_id):
        try:
            link = SpotImageLink.objects.get(id=image_id)
        except:
            self.error404_response()  # no return

        # Required settings for the client
        consumer, client = oauth_initialization()
        url = "{0}/api/v1/spot/{1}/image/{2}".format(settings.SS_WEB_SERVER_HOST,
                                                     spot_id, link.image_id)

        resp, content = client.request(url, 'GET')

        if resp.status != 200:
            return self.error_response(resp.status, msg="Error loading image")

        return self.json_response(content)
Beispiel #10
0
def _update_reviews(request):
    Permitted().is_admin(request.user)
    consumer, client = oauth_initialization()

    for key in request.POST:
        m = re.search('review_([\d]+)', key)
        if m:
            review_id = m.group(1)
            publish = False
            delete = False
            review = request.POST["review_%s" % review_id]
            if ("publish_%s" % review_id) in request.POST:
                publish = True if request.POST["publish_%s" %
                                               review_id] else False
            if ("delete_%s" % review_id) in request.POST:
                delete = True if request.POST["delete_%s" %
                                              review_id] else False

            url = "{0}/api/v1/reviews/unpublished".format(
                settings.SS_WEB_SERVER_HOST)

            headers = {
                "X-OAuth-User": "******" % request.user.username,
                'Content-Type': 'application/json',
                'Accept': 'application/json'
            }

            body = json.dumps({
                "review_id": review_id,
                "review": review,
                "publish": publish,
                "delete": delete,
            })

            resp, content = client.request(url,
                                           'POST',
                                           body=body,
                                           headers=headers)

            if resp.status != 200:
                raise Exception("Error loading reviews: %s" % content)

    return HttpResponseRedirect(
        reverse('spacescout_admin.views.reviews.unpublished'))
Beispiel #11
0
    def _push(self, method, spot_url, spot, user):
        consumer, client = oauth_initialization()

        headers = {
            "X-OAuth-User": "******" % user,
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        }

        if 'etag' in spot and len(spot.get('etag')):
            headers['If-Match'] = spot.get('etag')

        resp, content = client.request(spot_url,
                                       method=method,
                                       body=json.dumps(spot),
                                       headers=headers)

        if resp.status == 200 or resp.status == 201:
            return resp, json.loads(content) if content else {}

        raise Exception("Unable to {0} spot: Server response {1}".format(method, resp.status))
Beispiel #12
0
    def post(self, image_path, description, user):
        consumer, client = oauth_initialization()
        resp, content = client.request("{0}/api/v1/spot".format(settings.SS_WEB_SERVER_HOST), 'GET')
        i = resp['content-location'].find('oauth_signature=')
        i += len('oauth_signature=')
        signature = resp['content-location'][i:]

        oauth_key = ""
        if hasattr(settings, "SS_ADMIN_OAUTH_KEY"):
            oauth_key = settings.SS_ADMIN_OAUTH_KEY
        elif hasattr(settings, "SS_WEB_OAUTH_KEY"):
            oauth_key = settings.SS_WEB_OAUTH_KEY
        else:
            raise(Exception("Required setting missing: SS_ADMIN_OAUTH_KEY"))



        authorization = 'OAuth oauth_version="1.0",oauth_nonce="%s",oauth_timestamp="%d",oauth_consumer_key="%s",oauth_signature_method="HMAC-SHA1",oauth_signature="%s"' % (oauth_nonce(), int(time.time()), oauth_key, signature)

        register_openers()

        f = open(image_path, mode='rb')

        datagen, headers = multipart_encode({
            'description': description,
            'image': f
        })

        headers["X-OAuth-User"] = "******" % user
        headers["Authorization"] = authorization
        req = urllib2.Request(self._service_url(), datagen, headers)
        try:
            response = urllib2.urlopen(req)
        except (urllib2.URLError, urllib2.HTTPError) as e:
            raise SpotException({'status_code': e.code,
                                 'status_text': e.args})

        f.close()

        return { 'id': re.match(r'.*/(\d+)$', response.info().get('Location')).group(1) }
Beispiel #13
0
def _show_unpublished(request):
    Permitted().is_admin(request.user)
    consumer, client = oauth_initialization()

    url = "{0}/api/v1/reviews/unpublished".format(settings.SS_WEB_SERVER_HOST)

    headers = {
        "X-OAuth-User": "******" % request.user.username,
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }

    resp, content = client.request(url, 'GET', headers=headers)

    if resp.status != 200:
        raise Exception("Error loading reviews: %s" % content)

    reviews = json.loads(content)

    return render_to_response('spacescout_admin/reviews/unpublished.html',
                             { "reviews": reviews},
                             context_instance=RequestContext(request))
Beispiel #14
0
def _show_unpublished(request):
    Permitted().is_admin(request.user)
    consumer, client = oauth_initialization()

    url = "{0}/api/v1/reviews/unpublished".format(settings.SS_WEB_SERVER_HOST)

    headers = {
        "X-OAuth-User": "******" % request.user.username,
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }

    resp, content = client.request(url, 'GET', headers=headers)

    if resp.status != 200:
        raise Exception("Error loading reviews: %s" % content)

    reviews = json.loads(content)

    return render_to_response('spacescout_admin/reviews/unpublished.html',
                              {"reviews": reviews},
                              context_instance=RequestContext(request))
Beispiel #15
0
def _update_reviews(request):
    Permitted().is_admin(request.user)
    consumer, client = oauth_initialization()

    for key in request.POST:
        m = re.search('review_([\d]+)', key)
        if m:
            review_id = m.group(1)
            publish = False
            delete = False
            review = request.POST["review_%s" % review_id]
            if ("publish_%s" % review_id) in request.POST:
                publish = True if request.POST["publish_%s" % review_id] else False
            if ("delete_%s" % review_id) in request.POST:
                delete = True if request.POST["delete_%s" % review_id] else False

            url = "{0}/api/v1/reviews/unpublished".format(settings.SS_WEB_SERVER_HOST)

            headers = {
                "X-OAuth-User": "******" % request.user.username,
                'Content-Type': 'application/json',
                'Accept': 'application/json'
            }

            body = json.dumps({
                "review_id": review_id,
                "review": review,
                "publish": publish,
                "delete": delete,
            })

            resp, content = client.request(url, 'POST', body=body, headers=headers)

            if resp.status != 200:
                raise Exception("Error loading reviews: %s" % content)


    return HttpResponseRedirect(reverse('spacescout_admin.views.reviews.unpublished'))