def _address(key):
    addresses = llist(mongo.db.address.find({'address': key}))
    address = latest(addresses)
    if not address:
        return abort(404)

    street = latest(mongo.db.street.find({'street': address['street']}))

    children = sorted_naturally(
        llist(mongo.db.address.find({'parent-address': key})))
    parents = address_parents(address)

    addresses = addresses + children + parents

    postcode = mongo.db['address-postcode'].find_one({'address':
                                                      key})['postcode']

    return render_template("address.html",
                           address=address,
                           addresses=addresses,
                           street=street,
                           postcode=postcode,
                           uprn=decode(key),
                           parents=parents,
                           children=children)
Ejemplo n.º 2
0
def is_like_cid(cid):
    """ Raises ValueError if the cid given on input does not look like a genuine CID
    produced by zato.common.util.new_cid
    """
    if not isinstance(cid, string_types):
        raise ValueError(
            'CID `{}` should be string like instead of `{}`'.format(
                cid, type(cid)))

    len_given = len(cid)
    len_expected = CID_LENGTH + 1  # CID_LENGTH doesn't count 'K' in

    if len_given != len_expected:
        raise ValueError(
            'CID `{}` should have length `{}` instead of `{}`'.format(
                cid, len_expected, len_given))

    if not cid.startswith('K'):
        raise ValueError('CID `{}` should start with `K`'.format(cid))

    value = decode(cid[1:])
    if (value >> 128) != 0:
        raise ValueError('There aren\'t 128 bits in CID `{}`'.format(value))

    return True
Ejemplo n.º 3
0
 def get_object(self, queryset=None):
     key = self.kwargs.get(self.pk_url_kwarg, None)
     try:
         pk = base32_crockford.decode(key)
     except ValueError:
         raise Http404
     return get_object_or_404(Post.objects.published(), pk=pk)
Ejemplo n.º 4
0
def revert_doi(doi):
    '''
    Returns offset and internal ID belonging to a DOI that was generated
    with generate_doi().

    Args:
        doi (str): The DOI
    Returns:
        {"prefix" (str): the prefix, "offset" (int): the offset,
         "intid" (int): internal ID}

    '''
    try:
        prefix, encoded = doi.split('/')
    except ValueError:
        # assuming prefix was omitted
        encoded = doi
        prefix = None
    i = b32.decode(encoded, checksum=True)
    n = int(i / 37)
    c = i % 37
    intid = int(c + n * 32)
    batch = int(intid / 2e6)
    offset = int(batch * 2e6)
    intid = intid - offset
    return {'prefix': prefix, 'offset': offset, 'intid': intid}
Ejemplo n.º 5
0
def is_like_cid(cid):
    """ Raises ValueError if the cid given on input does not look like a genuine CID
    produced by zato.common.util.new_cid
    """
    if not isinstance(cid, string_types):
        raise ValueError('CID `{}` should be string like instead of `{}`'.format(cid, type(cid)))

    len_given = len(cid)
    len_expected = CID_LENGTH + 1 # CID_LENGTH doesn't count 'K' in

    if len_given != len_expected:
        raise ValueError('CID `{}` should have length `{}` instead of `{}`'.format(cid, len_expected, len_given))

    if not cid.startswith('K'):
        raise ValueError('CID `{}` should start with `K`'.format(cid))

    value = decode(cid[1:])
    if(value >> 128) != 0:
        raise ValueError('There aren\'t 128 bits in CID `{}`'.format(value))

    return True
Ejemplo n.º 6
0
    def run(cls, _input: Decodable, **kwargs) -> Optional[bytes]:
        """Decodes Base32 encoded bytes-like object or ASCII `data` string
        using the chars set and rules as defined by Douglas Crockford.

        See https://www.crockford.com/base32.html

        :param _input: Base64 encoded (bytes) string
        :param kwargs: Arbitrary keyword arguments
        :return: `None` if `data` couldn't be decoded, else decoded byte string
        """
        try:
            if not isinstance(_input, str):
                _input = str_from_bytes(_input)

            if (re.search(r"[^0123456789ABCDEFGHJKMNPQRSTVWXYZ]",
                          _input.upper()) is not None):
                return None
            decoded = base32_crockford.decode(_input, strict=True)
            return decoded.to_bytes((decoded.bit_length() + 7) // 8,
                                    byteorder="big")
        except:
            return None
Ejemplo n.º 7
0
def redirect(request, key):
    """
    Given the short URL key, update the statistics and redirect the
    user to the destination URL.
    """
    try:
        alias = ShortURLAlias.objects.get(alias=key.lower())
        key_id = alias.redirect_id
    except ShortURLAlias.DoesNotExist:
        try:
            key_id = base32_crockford.decode(key)
        except ValueError as e:
            logger.warning("Error decoding redirect: %s" % e)
            raise Http404

    redirect = get_object_or_404(ShortURL, pk=key_id)
    ShortURL.objects.increment_hits(redirect.pk)
    params = request.GET.dict()

    if redirect.is_tracking:
        return HttpResponsePermanentRedirect(redirect.target_url(params=params))
    else:
        return HttpResponseRedirect(redirect.target_url(params=params))
Ejemplo n.º 8
0
def redirect(request, key):
    """
    Given the short URL key, update the statistics and redirect the
    user to the destination URL.
    """
    try:
        alias = ShortURLAlias.objects.get(alias=key.lower())
        key_id = alias.redirect_id
    except ShortURLAlias.DoesNotExist:
        try:
            key_id = base32_crockford.decode(key)
        except ValueError as e:
            logger.warning("Error decoding redirect: %s" % e)
            raise Http404

    redirect = get_object_or_404(ShortURL, pk=key_id)
    ShortURL.objects.increment_hits(redirect.pk)
    params = request.GET.dict()

    if redirect.is_tracking:
        return HttpResponsePermanentRedirect(
            redirect.target_url(params=params))
    else:
        return HttpResponseRedirect(redirect.target_url(params=params))
def test_base32_crockford(input_int):
    assert base32_crockford.decode(
        base32_crockford.encode(input_int)) == input_int
Ejemplo n.º 10
0
 def from_str(cls, lid_str) -> "LID":
     ts_repr, rand_repr = lid_str.split("-")
     milliseconds = base32_crockford.decode(ts_repr)
     rand = base32_crockford.decode(rand_repr)
     return cls((milliseconds << 22) | rand)
Ejemplo n.º 11
0
def str26_to_int( string: str ) -> int:
    '''
    In: string, 26char encoding of uuid
    Returns: Int of uuid
    '''
    return decode( string )
Ejemplo n.º 12
0
 def test_decode_strict(self):
     self.assertRaises(ValueError, b32.decode, '16j', strict=True)
     self.assertEqual(b32.decode('16J', strict=True), 1234)
Ejemplo n.º 13
0
 def test_decode_checksum(self):
     self.assertEqual(b32.decode('16JD', checksum=True), 1234)
Ejemplo n.º 14
0
 def test_decode_unicode(self):
     self.assertEqual(b32.decode(u'16J'), 1234)
Ejemplo n.º 15
0
def _school(urn):
    if request.method == 'POST':
        mongo.db['school-address'].find_one_and_update({'school': urn}, {
            '$set': {
                'school': urn,
                'address': request.form['address'],
                'address-match': 'byhand'
            }
        },
                                                       upsert=True)
        return redirect("/school/" + urn, code=303)

    edubase = latest(mongo.db.edubase.find({'URN': urn}))
    if not edubase:
        return abort(404)
    key = uprn = postcode = ''
    address = street = {}
    addresses = parents = children = streets = []

    key = ''
    doc = mongo.db['school-address'].find_one({'school': urn})
    if doc:
        key = doc['address']

    if key != '':
        key = key.split(";")[0]
        uprn = decode(key)
        addresses = llist(mongo.db.address.find({'address': key}))
        address = latest(addresses)
        if address:
            street = latest(mongo.db.street.find({'street':
                                                  address['street']}))
            children = sorted_naturally(
                llist(mongo.db.address.find({'parent-address': key})))
            parents = address_parents(address)
            addresses = addresses + children + parents
            postcode = mongo.db['address-postcode'].find_one({'address':
                                                              key})['postcode']

    point = []
    if edubase['Easting']:
        lat, lon = pyproj.transform(osgb36, wgs84, edubase['Easting'],
                                    edubase['Northing'])
        point = [lon, lat]
        addresses = addresses + llist(
            mongo.db.address.find({
                'point':
                SON([('$nearSphere', [lat, lon]), ('$maxDistance', 0.00004)])
            }))
        streets = streets + llist(
            mongo.db.street.find({
                'point':
                SON([('$nearSphere', [lat, lon]), ('$maxDistance', 0.00004)])
            }))

    guesses = {}
    ignore = ['the']
    words = n7e(edubase['EstablishmentName'], ignore).split()
    words = words + [
        'school', 'academy', 'infant', 'junior', 'middle', 'college', 'jmi',
        'campus'
    ]
    for a in addresses:
        if set(words).intersection(set(n7e(a['name'], ignore).split())):
            guesses[a['address'] + ":" + a['name']] = a

    guesses = [guesses[k] for k in sorted(guesses)]

    return render_template("school.html",
                           edubase=edubase,
                           guesses=guesses,
                           point=point,
                           address=address,
                           addresses=addresses,
                           streets=streets,
                           street=street,
                           postcode=postcode,
                           uprn=uprn,
                           parents=parents,
                           children=children)