예제 #1
0
def table():
    base_id = "appaPqizdsNHDvlEm"
    table_name = "TEST_TABLE"
    table = Table(os.environ["AIRTABLE_API_KEY"], base_id, table_name)
    yield table
    records = table.all()
    table.batch_delete([r["id"] for r in records])
예제 #2
0
 def __init__(self, config_json):
     ErsiliaBase.__init__(self, config_json=config_json)
     self.api_key = AIRTABLE_READONLY_API_KEY
     self.base_id = AIRTABLE_MODEL_HUB_BASE_ID
     self.table_name = AIRTABLE_MODEL_HUB_TABLE_NAME
     self.max_rows = AIRTABLE_MAX_ROWS
     self.page_size = AIRTABLE_PAGE_SIZE
     self.table = Table(self.api_key, self.base_id, self.table_name)
예제 #3
0
def test_integration_field_equals_with_quotes(table: Table, cols):
    VALUE = "Contact's Name {}".format(uuid4())
    rv_create = table.create({cols.TEXT: VALUE})
    rv_first = table.first(formula=fo.match({cols.TEXT: VALUE}))
    assert rv_first and rv_first["id"] == rv_create["id"]

    VALUE = 'Some "Quote"  {}'.format(uuid4())
    rv_create = table.create({cols.TEXT: VALUE})
    rv_first = table.first(formula=fo.match({cols.TEXT: VALUE}))
    assert rv_first and rv_first["id"] == rv_create["id"]
예제 #4
0
def test_integration_formula_date_filter(table: Table, cols):
    dt = datetime.utcnow()
    date = dt.date()
    date_str = fo.to_airtable_value(date)

    created = []
    for _ in range(2):
        rec = table.create({cols.DATETIME: fo.to_airtable_value(dt)})
        created.append(rec)

    formula = fo.FIND(fo.STR_VALUE(date_str), fo.FIELD(cols.DATETIME))
    rv_all = table.all(formula=formula)
    assert rv_all
    assert set([r["id"] for r in rv_all]) == set([r["id"] for r in created])
def batch_update_airtable_records(records_to_update, field_names):
    AIRTABLE_API_KEY = os.getenv('AIRTABLE_API_KEY')
    AIRTABLE_BASE_ID = os.getenv('AIRTABLE_BASE_ID')

    # Create table object
    table = Table(AIRTABLE_API_KEY, AIRTABLE_BASE_ID,
                  'Rapid Review: Estimates')

    def update_single_record(row):
        # For each record, create a dict where the key is the field name and the value is the new field value
        # airtable_fields_config converts a readable english column name into a codified column name
        # e.g. x = 'Adjusted sensitivity', airtable_fields_config[x] = 'adj_sensitivity', row['adj_sensitivity'] = 0.9
        # If the value is NaN, convert to None because NaN throws error with Airtable API
        fields = {
            x: row[full_airtable_fields[x]]
            if not pd.isna(row[full_airtable_fields[x]]) else None
            for x in field_names
        }
        fields = doubleQuoteDict(fields)
        id = row['airtable_record_id']
        table.update(id, fields)
        return

    records_to_update.apply(lambda x: update_single_record(x), axis=1)
    return
예제 #6
0
class AirtableCard(ErsiliaBase):
    def __init__(self, config_json):
        ErsiliaBase.__init__(self, config_json=config_json)
        self.api_key = AIRTABLE_READONLY_API_KEY
        self.base_id = AIRTABLE_MODEL_HUB_BASE_ID
        self.table_name = AIRTABLE_MODEL_HUB_TABLE_NAME
        self.max_rows = AIRTABLE_MAX_ROWS
        self.page_size = AIRTABLE_PAGE_SIZE
        self.table = Table(self.api_key, self.base_id, self.table_name)

    def _find_card(self, text, field):
        card = None
        for records in self.table.iterate(
            page_size=self.page_size, max_records=self.max_rows
        ):
            for record in records:
                fields = record["fields"]
                if field not in fields:
                    continue
                if text == record["fields"][field]:
                    card = record["fields"]
        return card

    def find_card_by_model_id(self, model_id):
        return self._find_card(model_id, "Identifier")

    def find_card_by_slug(self, slug):
        return self._find_card(slug, "Slug")

    def get(self, model_id):
        return self.find_card_by_model_id(model_id)
예제 #7
0
def test_integration_formula_composition(table: Table, cols):
    text = "Mike's Thing {}".format(uuid4())
    num = 1
    bool_ = True
    rv_create = table.create({
        cols.TEXT: text,
        cols.NUM: num,
        cols.BOOL: bool_
    })

    formula = fo.AND(
        fo.EQUAL(fo.FIELD(cols.TEXT), fo.to_airtable_value(text)),
        fo.EQUAL(fo.FIELD(cols.NUM), fo.to_airtable_value(num)),
        fo.EQUAL(fo.FIELD(cols.BOOL),
                 fo.to_airtable_value(bool_)),  # not needs to be int()
    )
    rv_first = table.first(formula=formula)

    assert rv_first["id"] == rv_create["id"]
예제 #8
0
class QuoteApi:
    def __init__(self, key, base_id):
        self.table = Table(key, base_id, 'Quotes')

    def get_all_quotes(self):
        out_data = ""
        for q in self.table.all():
            fields = q['fields']
            out_data += fields['Quote']
            if 'Author' in fields:
                out_data += ' - ' + fields['Author']
            out_data += '\n'
        return out_data

    def get_quote(self):
        fields = choice(self.table.all())['fields']
        quote = fields['Quote']
        if 'Author' in fields:
            quote += '\n\t- ' + fields['Author']
        return quote
예제 #9
0
def init(request):
    # connection with Airtable
    formula = match({"course_number": "25 набор", "project_presented": 1})
    table = Table(settings.AIRTABLE_API_KEY, settings.AIRTABLE_BASE_ID,
                  'current_course')
    # data processing
    for script in table.all(formula=formula):
        name = script['fields']['first_name']
        surname = script['fields']['last_name']
        cert_date = date(2022, 4, 16)
        locale.setlocale(locale.LC_TIME, "ru_RU")
        cert_date = datetime.strftime(cert_date, "%d %B %Y")
        html = render(request, 'diploma_ru.html', {
            'name': name,
            'surname': surname,
            'date': cert_date
        })
        result = (html.content).decode('utf-8')
        config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')
        options = {
            'dpi': 300,
            'page-size': 'A4',
            'orientation': 'landscape',
            'margin-top': '0',
            'margin-right': '0.',
            'margin-bottom': '0.',
            'margin-left': '0',
            'encoding': "UTF-8",
            'custom-header': [('Accept-Encoding', 'gzip')],
            'no-outline': None
        }
        pdfkit.from_string(result,
                           'result/' + '{}-{}.pdf'.format(name, surname),
                           configuration=config,
                           options=options)
    return render(request, 'diploma_ru.html')
예제 #10
0
def airtable_to_df(
    air_base_id,
    air_table_name,
    id_name="__id",
    rename_fields=None,
    column_prefix=None,
    api_key=None,
):
    """Download an airtable as a DataFrame.

    Note that airtable records have rows structured as follows:
        [{"id", "fields": {colname: value, ...}, ...]

    This function applies renames in the following order.

        1. rename id
        2. rename fields
        3. apply column prefix (to columns not renamed by 1 or 2)
    """

    api_key = os.environ["CALITP_AIRTABLE_API_KEY"] if not api_key else api_key

    if rename_fields:
        if not isinstance(rename_fields, dict):
            raise TypeError("rename fields must be a dictionary")
    else:
        rename_fields = {}

    print(f"Downloading airtable data for {air_base_id}.{air_table_name}")
    all_rows = Table(api_key, air_base_id, air_table_name).all()
    raw_df = pd.DataFrame([{id_name: row["id"], **row["fields"]} for row in all_rows])

    # rename fields follows format new_name: old_name
    final_df = raw_df.rename(columns={k: v for k, v in rename_fields.items()}).pipe(
        to_snakecase
    )

    if column_prefix:
        new_field_names = rename_fields.values()
        return final_df.rename(
            columns=lambda s: s
            if (s in new_field_names or s == id_name)
            else f"{column_prefix}{s}"
        )

    return final_df
import secrets
from pyairtable import Table

user_table = Table(secrets.airtable_Api_key, secrets.airtable_base, 'user')
subreddit_table = Table(secrets.airtable_Api_key, secrets.airtable_base,
                        'Subreddit')


#user table create update
def checkIfusername_exists(user):
    for j in user_table.all():
        if (j['fields']['Username'] == user):
            return j['id'], j['fields']['usage_count']


def update_existing_user(record_id, current_usage):
    return (user_table.update(record_id, {"usage_count": current_usage + 1}))


def Create_update_user(username):
    result = checkIfusername_exists(username)
    if (result == None):
        return (user_table.create({'Username': username, 'usage_count': 1}))
    else:
        update_existing_user(record_id=result[0], current_usage=result[1])


#subreddit table create update


def checkifSubredditExists(subreddit_name):
예제 #12
0
 def __init__(self, key, base_id):
     self.table = Table(key, base_id, 'Quotes')
예제 #13
0
def test_url(base_id, table_name, table_url_suffix):
    table = Table("apikey", base_id, table_name)
    assert table.table_url == "{0}/{1}".format(table.API_URL, table_url_suffix)
예제 #14
0
def test_integration_formula_datetime(table: Table, cols):
    VALUE = datetime.utcnow()
    str_value = fo.to_airtable_value(VALUE)
    rv_create = table.create({cols.DATETIME: str_value})
    rv_first = table.first(formula=fo.match({cols.DATETIME: str_value}))
    assert rv_first and rv_first["id"] == rv_create["id"]
예제 #15
0
def test_integration_field_equals(table: Table, cols):
    VALUE = "Test {}".format(uuid4())
    rv_create = table.create({cols.TEXT: VALUE})
    rv_first = table.first(formula=fo.match({cols.TEXT: VALUE}))
    assert rv_first and rv_first["id"] == rv_create["id"]