GithubHelp home page GithubHelp logo

vilarneto / pyrtable Goto Github PK

View Code? Open in Web Editor NEW
9.0 1.0 1.0 250 KB

Django-inspired, Python 3.x library to interface with Airtable

License: MIT License

Python 100.00%
python airtable airtable-api python-3 python3 orm python38 python-3-8 python-3-9 python39

pyrtable's People

Contributors

dependabot[bot] avatar vilarneto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

bellyfat

pyrtable's Issues

Error on update (pyrtable==0.7.10)

save method works but I'm getting an error on update:

class Devices(MyBaseRecord):
    class Meta:
        table_id = 'Devices'

    name = StringField('Name')
    description = StringField('Description')


class Ips(MyBaseRecord):
    class Meta:
        table_id = 'Ips'

    id = IntegerField('Id')
    internal = StringField('Internal')
    external = StringField('External')
    device = SingleRecordLinkField('Device', linked_class=Devices)


device = Devices(name="pc", description="My pc")
device.save()

ip = Ips(internal="192.168.100.15", external="181.143.142.160", device=device)
ip.save() # It works

ip.internal = "192.168.100.16"
ip.save() # Error

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-50-7a5a8299798b> in <module>
----> 1 ip.save()

/usr/local/lib/python3.9/site-packages/pyrtable/record.py in save(self)
    214         """
    215         from pyrtable.context import get_default_context
--> 216         get_default_context().save(self.__class__, self)
    217
    218     def encode_to_airtable(self, include_non_dirty_fields=False) -> Dict[str, Any]:

/usr/local/lib/python3.9/site-packages/pyrtable/context/base.py in save(self, record_cls, record)
    158             self._create(record_cls, record)
    159         else:
--> 160             self._update(record_cls, record)
    161
    162     def delete_id(self, *,

/usr/local/lib/python3.9/site-packages/pyrtable/context/base.py in _update(self, record_cls, record)
    141             if 400 <= response.status_code < 500:
    142                 error = response.json().get('error', {})
--> 143                 error_message = error.get('message', '')
    144                 error_type = error.get('type', '')
    145

AttributeError: 'str' object has no attribute 'get'

Batch record creation/updating/deleting

Currently BaseRecord.save() and BaseRecord.delete() operations operate on single records.

It's interesting to offer an alternative form to these operations that take advantage of the batching capabilities of Airtable API for these operations (currently limited to 10 records per request).

Python 3.9 DeprecationWarning for Python 3.10

While using pyrtable in 3.9 the following warning is issued. Python 3.10 was just released and, as it states, this will not work.

python3.9/site-packages/pyrtable/fields/valueset.py:12: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working
    class ValueSet(collections.MutableSet, collections.Iterable, Generic[T]):

P.S. really enjoy using this in our integration projects with Airtable!

Escaping apostrophes in Table.objects.filter(stringColumnName="a string with ' apostrophes in it" may not be necessary

In filterutils.py, the quote value routine. I have apostrophes in the data of one of my string columns in one of my AirTable
tables. The return '"%s"' % re.sub(r'(["\'\\])', lambda ch: '\\' + ch.group(0), value) is escaping apostrophes.

An example is "augmentin '400'" becomes "augmentin \'400\' before being sent in the filterByFormula querystring field
to AirTable and then MyTable.objects.filter(stringColumnName="augmentin '400'") yields no results. I don't know if the newer AirTable API automatically escapes apostrophes itself so you don't have to. Looks like we are using pyrtable==0.7.11 from pip freeze.

def quote_value(value) -> str:
    import datetime

    if isinstance(value, str):
        return '"%s"' % re.sub(r'(["\'\\])', lambda ch: '\\' + ch.group(0), value)
    if isinstance(value, bool):
        return 'TRUE()' if value else 'FALSE()'
    if isinstance(value, (int, float)):
        return str(value)
    if isinstance(value, Enum):
        return quote_value(value.value)
    if isinstance(value, datetime.datetime):
        return format(value, '"%Y-%m-%dT%H:%M:%S.%fZ"')
    if isinstance(value, datetime.date):
        return format(value, '"%Y-%m-%d"')
    raise ValueError(value)

I'm doing this absolutely filthy patch of the code below for the moment to exclude apostrophes from being escaped. When you have some time, perhaps you could create a small Airtable with a single column and put a record in there with an apostrophe in the string to see if your objects.filter function also fails to find the record with an apostrophe in it.

    def my_quote_value(value) -> str:
        import datetime
        import re
        from enum import Enum
        if isinstance(value, str):
            return '"%s"' % re.sub(r'(["\\])', lambda ch: '\\' + ch.group(0), value)
        if isinstance(value, bool):
            return 'TRUE()' if value else 'FALSE()'
        if isinstance(value, (int, float)):
            return str(value)
        if isinstance(value, Enum):
            return my_quote_value(value.value)
        if isinstance(value, datetime.datetime):
            return format(value, '"%Y-%m-%dT%H:%M:%S.%fZ"')
        if isinstance(value, datetime.date):
            return format(value, '"%Y-%m-%d"')
        raise ValueError(value)
    import pyrtable.filterutils
    pyrtable.filterutils.quote_value=my_quote_value

ValueError: Cannot use base_id

Hey, I'm following your example but I'm getting the following error:

    266                     function = functools.partial(function, base_id=base_id)
    267                 else:
--> 268                     raise ValueError('Cannot use base_id')
    269
    270             result['Authorization'] = 'Bearer %s' % function()

ValueError: Cannot use base_id

I'm sure the API key and base id are correct because I tried the same using curl

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.