예제 #1
0
    def test_simple_object_2(self):
        qs = "&id=foo&dog.name=lucy&dog.color=brown"
        result = parse(qs)

        self.assertEqual(result["id"], "foo")
        self.assertEqual(result["dog"]["name"], "lucy")
        self.assertEqual(result["dog"]["color"], "brown")
예제 #2
0
 def test_push_array(self):
     qs = "&id=foo&dog[]=z-lucy&dog[]=radar"
     result = parse(qs)
     self.assertEqual(result["id"], "foo")
     self.assertEqual(len(result["dog"]), 2)
     self.assertEqual(result["dog"][0], "z-lucy")
     self.assertEqual(result["dog"][1], "radar")
예제 #3
0
    def test_odd_names(self):
        qs = "&id=foo&dog[name.1]=lucy&dog[name[2]]=radar"
        result = parse(qs)

        self.assertEqual(result["id"], "foo")
        self.assertEqual(result["dog"]["name.1"], "lucy")
        self.assertEqual(result["dog"]["name[2]"], "radar")
예제 #4
0
    def test_simple_object_4(self):
        qs = "&id=foo&dog2[name]=lucy&dog2[name2]=lucy&dog[ color ]=brown"
        result = parse(qs)

        self.assertEqual(result["id"], "foo")
        self.assertEqual(result["dog2"]["name2"], "lucy")
        self.assertEqual(result["dog"]["color"], "brown")
예제 #5
0
    def test_simple(self):
        qs = "&id=foo&dog=lucy&cat=ollie"
        result = parse(qs)

        self.assertEqual(result["id"], "foo")
        self.assertEqual(result["dog"], "lucy")
        self.assertEqual(result["cat"], "ollie")
예제 #6
0
파일: views.py 프로젝트: kylewm/woodwind
def update_micropub_syndicate_to():
    def adapt_expanded(targets):
        """Backcompat support for old-style "syndicate-to-expanded" properties,
        e.g.,
        {
          "id": "twitter::kylewmahan",
          "name": "@kylewmahan",
          "service": "Twitter"
        }
        """
        if targets:
            return [{
                'uid': t.get('id'),
                'name': '{} on {}'.format(t.get('name'), t.get('service')),
            } for t in targets]
        return targets

    endpt = flask_login.current_user.micropub_endpoint
    token = flask_login.current_user.access_token
    if not endpt or not token:
        return
    resp = util.requests_get(endpt,
                             params={
                                 'q': 'syndicate-to',
                             },
                             headers={
                                 'Authorization': 'Bearer ' + token,
                                 'Accept': 'application/json',
                             })
    if resp.status_code // 100 != 2:
        flask.current_app.logger.warn(
            'Unexpected response querying micropub endpoint %s: %s', resp,
            resp.text)
        return

    flask.current_app.logger.debug('syndicate-to response: %s %s', resp,
                                   resp.text)

    content_type = resp.headers['content-type']
    if content_type:
        content_type = content_type.split(';', 1)[0]

    try:
        if content_type == 'application/json':
            blob = resp.json()
            syndicate_tos = adapt_expanded(blob.get('syndicate-to-expanded'))
            if not syndicate_tos:
                syndicate_tos = blob.get('syndicate-to')

        else:  # try to parse query string
            syndicate_tos = pyquerystring.parse(resp.text).get(
                'syndicate-to', [])
            if isinstance(syndicate_tos, list):
                syndicate_tos = list(syndicate_tos)

        flask_login.current_user.set_setting('syndicate-to', syndicate_tos)
        db.session.commit()
    except ValueError as e:
        flask.flash('Could not parse syndicate-to response: {}'.format(e))
예제 #7
0
def scrobble(request):
    # check session
    session_key = request.POST.get("s")
    try:
        profile = ScrobbleSession.objects.get(key=session_key).profile
    except ScrobbleSession.DoesNotExist:
        return HttpResponseBadRequest("BADSESSION")

    # turn query into usable format
    query_string = request.POST.urlencode()
    data = parse(query_string)

    timestamps = data.get("i", [])
    artists = data.get("a", [])
    tracks = data.get("t", [])
    albums = data.get("b", [])
    lengths = data.get("l", [])
    tracknumbers = data.get("n", [])

    # create scrobble form
    excluded_fields = ["artist", "album", "track", "date"]
    ScrobbleForm = modelform_factory(Scrobble, exclude=excluded_fields)

    for i, a, t, b, l, n in zip_longest(
        timestamps, artists, tracks, albums, lengths, tracknumbers
    ):
        # check if scrobble exists
        scrobble = Scrobble.objects.filter(profile=profile, timestamp=i)
        if scrobble.exists():
            """
            The Audioscrobbler protocol requires us to fail silently here.
            Any response except "OK" would prompt new submission attempts from
            the client. However, if a scrobble with the same profile and
            timestamp is already in the database, this submission will never be
            accepted and the client will try re-submitting indefinitely for as
            long as the submission queue isn't flushed manually. Per protocol,
            only the "OK" response prompts the client to remove a submission
            from the queue.
            """
            continue

        form = ScrobbleForm(
            {
                "profile": profile.pk,
                "timestamp": i,
                "artist_name": a,
                "track_title": t,
                "album_title": b,
                "length": l,
                "tracknumber": n,
            }
        )

        if not form.is_valid():
            return HttpResponseBadRequest("FAILED request invalid")
        else:
            form.save()

    return HttpResponse("OK")
예제 #8
0
    def test_simple_array(self):
        qs = "&id=foo&dog[1]=lucy&dog[0]=tucker"
        result = parse(qs)

        self.assertEqual(result["id"], "foo")
        self.assertEqual(len(result["dog"]), 2)
        self.assertEqual(result["dog"][0], "tucker")
        self.assertEqual(result["dog"][1], "lucy")
예제 #9
0
    def test_object_with_array(self):
        qs = "&id=foo&dog[0].name=lucy&dog[1].name=radar"
        result = parse(qs)

        self.assertEqual(result["id"], "foo")
        self.assertEqual(len(result["dog"]), 2)
        self.assertEqual(result["dog"][0]["name"], "lucy")
        self.assertEqual(result["dog"][1]["name"], "radar")
예제 #10
0
 def test_multidimensional_array_with_object(self):
     qs = "&id=foo&dog[0][0].name=lucy&dog[0][1].name=radar&bar=baz"
     result = parse(qs)
     self.assertEqual(result["id"], "foo")
     self.assertEqual(result["bar"], "baz")
     self.assertEqual(len(result["dog"]), 1)
     self.assertEqual(result["dog"][0][0]["name"], "lucy")
     self.assertEqual(result["dog"][0][1]["name"], "radar")
예제 #11
0
 def test_list_gap(self):
     qs = "&id=foo&dog[2]=dexter&cat=ollie"
     result = parse(qs)
     self.assertEqual(result["id"], "foo")
     self.assertEqual(len(result["dog"]), 3)
     self.assertEqual(result["dog"][0], None)
     self.assertEqual(result["dog"][1], None)
     self.assertEqual(result["dog"][2], "dexter")
     self.assertEqual(result["cat"], "ollie")
예제 #12
0
 def __init__(self, httprequest):
     super(HttpRestRequest, self).__init__(httprequest)
     if self.httprequest.mimetype == 'application/json':
         self.params = json.loads(self.httprequest.data)
     else:
         # We reparse the query_string in order to handle data structure
         # more information on https://github.com/aventurella/pyquerystring
         self.params = pyquerystring.parse(self.httprequest.query_string)
     self._determine_context_lang()
예제 #13
0
    def test_multidimensional_array3x(self):
        qs = "&id=foo&dog[0][0][0]=lucy&dog[0][0][1]=radar&dog[0][0][2]=tucker"
        result = parse(qs)

        self.assertEqual(result["id"], "foo")
        self.assertEqual(len(result["dog"][0][0]), 3)
        self.assertEqual(result["dog"][0][0][0], "lucy")
        self.assertEqual(result["dog"][0][0][1], "radar")
        self.assertEqual(result["dog"][0][0][2], "tucker")
예제 #14
0
    def test_push_array_object(self):
        qs = "&id=foo&dog.name[]=radar&dog.name[]=tucker&dog.name[]=lucy"
        result = parse(qs)

        self.assertEqual(result["id"], "foo")
        self.assertEqual(len(result["dog"]["name"]), 3)
        self.assertEqual(result["dog"]["name"][0], "radar")
        self.assertEqual(result["dog"]["name"][1], "tucker")
        self.assertEqual(result["dog"]["name"][2], "lucy")
예제 #15
0
    def test_multidimensional_array2x_object(self):
        qs = "&id=foo&dog[0][0].name=lucy&dog[0][1].name=ollie&dog[1][0].name=radar"
        result = parse(qs)

        self.assertEqual(result["id"], "foo")
        self.assertEqual(len(result["dog"]), 2)
        self.assertEqual(result["dog"][0][0]["name"], "lucy")
        self.assertEqual(result["dog"][0][1]["name"], "ollie")
        self.assertEqual(result["dog"][1][0]["name"], "radar")
예제 #16
0
def update_micropub_syndicate_to():

    def adapt_expanded(targets):
        """Backcompat support for old-style "syndicate-to-expanded" properties,
        e.g.,
        {
          "id": "twitter::kylewmahan",
          "name": "@kylewmahan",
          "service": "Twitter"
        }
        """
        if targets:
            return [{
                'uid': t.get('id'),
                'name': '{} on {}'.format(t.get('name'), t.get('service')),
            } for t in targets]
        return targets

    endpt = flask_login.current_user.micropub_endpoint
    token = flask_login.current_user.access_token
    if not endpt or not token:
        return
    resp = util.requests_get(endpt, params={
        'q': 'syndicate-to',
    }, headers={
        'Authorization': 'Bearer ' + token,
        'Accept': 'application/json',
    })
    if resp.status_code // 100 != 2:
        flask.current_app.logger.warn(
            'Unexpected response querying micropub endpoint %s: %s',
            resp, resp.text)
        return

    flask.current_app.logger.debug('syndicate-to response: %s %s',
                                   resp, resp.text)

    content_type = resp.headers['content-type']
    if content_type:
        content_type = content_type.split(';', 1)[0]

    if content_type == 'application/json':
        blob = resp.json()
        syndicate_tos = adapt_expanded(blob.get('syndicate-to-expanded'))
        if not syndicate_tos:
            syndicate_tos = blob.get('syndicate-to')

    else:  # try to parse query string
        syndicate_tos = pyquerystring.parse(resp.text).get('syndicate-to', [])
        if isinstance(syndicate_tos, list):
            syndicate_tos = list(syndicate_tos)

    flask_login.current_user.set_setting('syndicate-to', syndicate_tos)
    db.session.commit()
예제 #17
0
 def __init__(self, httprequest):
     super(HttpRestRequest, self).__init__(httprequest)
     if self.httprequest.mimetype == 'application/json':
         self.params = json.loads(self.httprequest.data)
     else:
         # We reparse the query_string in order to handle data structure
         # more information on https://github.com/aventurella/pyquerystring
         self.params = pyquerystring.parse(self.httprequest.query_string)
     lang = self.httprequest.headers.get('Lang')
     if lang:
         self._context = self._context or {}
         self._context['lang'] = lang
예제 #18
0
 def test_array_with_object_with_array(self):
     qs = "&id=foo&dog[0].name=lucy&dog[0].attributes[0]=tail&dog[0].attributes[1]=paws&dog[0].attributes[2]=ears&dog[0].attributes[3].type=dog&dog[1].name=radar"
     result = parse(qs)
     self.assertEqual(result["id"], "foo")
     self.assertEqual(len(result["dog"]), 2)
     self.assertEqual(result["dog"][0]["name"], "lucy")
     self.assertEqual(len(result["dog"][0]["attributes"]), 4)
     self.assertEqual(result["dog"][0]["attributes"][0], "tail")
     self.assertEqual(result["dog"][0]["attributes"][1], "paws")
     self.assertEqual(result["dog"][0]["attributes"][2], "ears")
     self.assertEqual(result["dog"][0]["attributes"][3]["type"], "dog")
     self.assertEqual(result["dog"][1]["name"], "radar")
예제 #19
0
 def __init__(self, httprequest):
     super(HttpRestRequest, self).__init__(httprequest)
     if self.httprequest.mimetype == "application/json":
         data = self.httprequest.get_data().decode(self.httprequest.charset)
         try:
             self.params = json.loads(data)
         except ValueError as e:
             msg = "Invalid JSON data: %s" % str(e)
             _logger.info("%s: %s", self.httprequest.path, msg)
             raise BadRequest(msg) from e
     else:
         # We reparse the query_string in order to handle data structure
         # more information on https://github.com/aventurella/pyquerystring
         self.params = pyquerystring.parse(
             self.httprequest.query_string.decode("utf-8"))
     self._determine_context_lang()
예제 #20
0
    def test_lotsa_items(self):
        qs = (
            "&id=foo&dog[2]=dexter&dog[1]=tucker&dog[0]=lucy&dog[3]=lucy"
            "&dog[4]=lucy&dog[5]=lucy&dog[6]=lucy&dog[7]=lucy&dog[8]=lucy"
            "&dog[9]=lucy&dog[10]=fido&dog[11]=lucyagain"
        )
        result = parse(qs)

        self.assertEqual(result["id"], "foo")
        self.assertEqual(len(result["dog"]), 12)
        self.assertEqual(result["dog"][0], "lucy")
        self.assertEqual(result["dog"][1], "tucker")
        self.assertEqual(result["dog"][2], "dexter")
        self.assertEqual(result["dog"][3], "lucy")
        self.assertEqual(result["dog"][4], "lucy")
        self.assertEqual(result["dog"][5], "lucy")
        self.assertEqual(result["dog"][10], "fido")
        self.assertEqual(result["dog"][11], "lucyagain")
예제 #21
0
    def get_context_data(self, request):
        """
        Gets paginated data.
        Returned as json dict.
        """

        json_response = dict(draw=0,
                             recordsTotal=0,
                             recordsFiltered=0,
                             data=[])

        additional_data = request.GET.get("additional_data")
        filter_params = parse(additional_data) if additional_data else {}
        try:
            qs = self.get_initial_queryset(request)
            total_records = qs.count()
            qs = self.filter_by_search(qs)
            if filter_params:
                qs = qs.filter(**filter_params)

            # number of records after filtering
            total_display_records = qs.count()

            qs = self.ordering(qs)
            qs = self.paging(qs)
            data = self.prepare_results(qs)
            json_response.update({
                "draw": int(self._querydict.get('draw', 0)),
                "recordsTotal": total_records,
                "recordsFiltered": total_display_records,
                "data": data
            })

        except Exception as e:
            LOG.exception(str(e))
            json_response['error'] = self.report_traceback()

        return json_response
예제 #22
0
    def test_mixed1(self):
        qs = "&id=foo&dog[2]=dexter&dog[1]=tucker&dog[0]=lucy&cat=ollie\
&pets[1].name=pogo&pets[1].type=catz&pets[0].name=kiki\
&pets[0].type=cat&fish.name=robofish&fish.type=fishz\
&person.name[0]=adam&person.name[1]=adamz\
&plants.name[0][1]=flower&plants.name[0][0]=tree\
&plants.name[1][0]=willow&plants.name[1][1]=fern\
&end[0][0][0]=lucy&end[0][0][1]=radar&end[0][0][2]=tucker"
        result = parse(qs)

        self.assertEqual(result["id"], "foo")
        self.assertEqual(len(result["dog"]), 3)
        self.assertEqual(result["dog"][0], "lucy")
        self.assertEqual(result["dog"][1], "tucker")
        self.assertEqual(result["dog"][2], "dexter")
        self.assertEqual(result["cat"], "ollie")
        self.assertEqual(len(result["pets"]), 2)
        self.assertEqual(result["pets"][0]["name"], "kiki")
        self.assertEqual(result["pets"][0]["type"], "cat")
        self.assertEqual(result["pets"][1]["name"], "pogo")
        self.assertEqual(result["pets"][1]["type"], "catz")
        self.assertEqual(result["fish"]["name"], "robofish")
        self.assertEqual(result["fish"]["type"], "fishz")
        self.assertEqual(len(result["person"]["name"]), 2)
        self.assertEqual(result["person"]["name"][0], "adam")
        self.assertEqual(result["person"]["name"][1], "adamz")
        self.assertEqual(len(result["plants"]["name"]), 2)
        self.assertEqual(len(result["plants"]["name"][0]), 2)
        self.assertEqual(len(result["plants"]["name"][1]), 2)
        self.assertEqual(result["plants"]["name"][0][0], "tree")
        self.assertEqual(result["plants"]["name"][0][1], "flower")
        self.assertEqual(result["plants"]["name"][1][0], "willow")
        self.assertEqual(result["plants"]["name"][1][1], "fern")
        self.assertEqual(result["end"][0][0][0], "lucy")
        self.assertEqual(result["end"][0][0][1], "radar")
        self.assertEqual(result["end"][0][0][2], "tucker")
예제 #23
0
 def test_overlaping_array_objects(self):
     qs = "&id[1].name=foo&id[1].name=foo2"
     result = parse(qs)
     self.assertEqual(result["id"][1]['name'], "foo2")
예제 #24
0
 def _querydict(self):
     if self.request.method == 'POST':
         return self.request.POST
     else:
         return parse(self.request.GET)
예제 #25
0
    def test_simple_object_1(self):
        qs = "&id=foo&dog.name=lucy"
        result = parse(qs)

        self.assertEqual(result["id"], "foo")
        self.assertEqual(result["dog"]["name"], "lucy")
예제 #26
0
 def test_bad_format(self):
     qs = "&id=foo&dog[1]]=lucy"
     with self.assertRaises(IOError):
         parse(qs)
예제 #27
0
 def test_overlaping_array_keys(self):
     qs = "&id[1]=foo&id[1]=foo2"
     result = parse(qs)
     self.assertEqual(result["id"][1], "foo2")
예제 #28
0
    def _querydict(self):
        if self.request.method == 'POST':
            return self.request.POST

        return parse(self.request.GET)
예제 #29
0
def bench():
    parse("id=1")