Ejemplo n.º 1
0
def main():
    EVENT_DATA = json.loads(os.getenv("APPWRITE_FUNCTION_EVENT_DATA"))

    CREATED_AT_KEY = os.getenv("CREATED_AT_KEY", "createdAt")

    client = init_client()
    database = Database(client)

    document_id = EVENT_DATA["$id"]
    collection_id = EVENT_DATA["$collection"]

    try:
        result = database.update_document(
            collection_id,
            document_id,
            {CREATED_AT_KEY: math.floor(time.time())},
        )
        print(
            f"Updated {CREATED_AT_KEY} on document {document_id} to {result[CREATED_AT_KEY]}"
        )
    except AppwriteException as err:
        if err.code != 400 or CREATED_AT_KEY not in err.message:
            raise

        print(f"Collection {collection_id} does not have {CREATED_AT_KEY} key")
Ejemplo n.º 2
0
def add_doc():
    database = Database(client)
    print_green("Running Add Document API")
    response = database.create_document(collectionId, {
        'name': "Spider Man",
        'release_year': 1920,
    }, ['*'], ['*'])
    print(response)
Ejemplo n.º 3
0
def main():
    payload = json.loads(os.getenv("APPWRITE_FUNCTION_EVENT_DATA"))
    user_collection_id = os.getenv("APPWRITE_USER_COLLECTION_ID")

    userId = payload["$id"]
    userName = payload["name"]
    email = payload["email"]

    client = init_client()
    database = Database(client)

    database.create_document(user_collection_id, {
        'user_id': userId,
        'user_name': userName,
        'email': email
    },
                             read=['*'])
Ejemplo n.º 4
0
def create_collection():
    global collectionId
    database = Database(client)
    print_green("Running Create Collection API")
    response = database.create_collection(
        'Movies',
        ['*'],
        ['*'],
        [
            {'label': "Name", 'key': "name", 'type': "text",
             'default': "Empty Name", 'required': True, 'array': False},
            {'label': 'release_year', 'key': 'release_year', 'type': 'numeric',
             'default': 1970, 'required': True, 'array': False}
        ]
    )
    collectionId = response['$id']
    print(response)
Ejemplo n.º 5
0
def main():
    payload = json.loads(os.getenv("APPWRITE_FUNCTION_EVENT_DATA"))

    UPDATED_AT_KEY = "updatedAt"
    collectionId = payload["$collection"]
    documentId = payload["$id"]
    currentTimeStamp = time.time()

    if UPDATED_AT_KEY not in payload:
        return

    client = init_client()
    database = Database(client)

    database.update_document(
        collectionId,
        documentId,
        {UPDATED_AT_KEY: currentTimeStamp},
    )

    print("Updated the field updatedAt with current timestamp successfully.")
Ejemplo n.º 6
0
def create_collection():
    global collectionId
    database = Database(client)
    print_green("Running Create Collection API")
    response = database.create_collection(
        'movies',
        'Movies',
        'document'['role:all'],
        ['role:all'],
    )
    collectionId = response['$id']
    print(response)
    response = database.create_string_attribute(
        collectionId,
        'name',
        255,
        true,
    )
    print(response)
    response = database.create_integer_attribute(collectionId, 'release_year',
                                                 0, 9999, true)
    print(response)
Ejemplo n.º 7
0
def list_doc():
    database = Database(client)
    print_green("Running List Document API")
    response = database.list_documents(collectionId)
    print(response)
Ejemplo n.º 8
0
def list_collection():
    database = Database(client)
    print_green("Running List Collection API")
    response = database.list_collections()
    collection = response['collections'][0]
    print(collection)
Ejemplo n.º 9
0
# general imports
import os
from datetime import datetime

# dependencies
from appwrite.client import Client
from appwrite.services.database import Database

# Setup appwrite client
client = Client()
client.set_endpoint(os.environ["APPWRITE_ENDPOINT"])
client.set_project(
    os.environ["APPWRITE_FUNCTION_PROJECT_ID"])  # this is available by default
client.set_key(os.environ["APPWRITE_API_KEY"])

database = Database(client)

result_collections = database.list_collections()

# get seconds since epoch for the date from exactly 5 years ago for the filter
date_today = datetime.now()
date_old = datetime(date_today.year - 5, date_today.month, date_today.day,
                    date_today.hour, date_today.minute, date_today.second)
seconds_since_epoch = int(date_old.timestamp())

# get ids of all collections
collections = result_collections['collections']
collection_list = []
for i in range(result_collections["sum"]):
    collection_list.append(collections[i]['$id'])
Ejemplo n.º 10
0
            dbx.files_upload(f.read(), BACKUPPATH, mode=WriteMode('overwrite'))
        except ApiError as err:
            # This checks for the specific error where a user doesn't have
            # enough Dropbox space quota to upload this file
            if (err.error.is_path()
                    and err.error.get_path().reason.is_insufficient_space()):
                sys.exit("ERROR: Cannot back up; insufficient space.")
            elif err.user_message_text:
                print(err.user_message_text)
                sys.exit()
            else:
                print(err)
                sys.exit()


database = Database(client)
result = database.list_collections()

collections = result['collections']
collection_list = []
name_list = []
for i in range(result['sum']):
    collection_list.append(collections[i]['$id'])
    name_list.append(collections[i]['name'])


def all_stuff(file_id):
    fileID = file_id
    global PATH1

    limit = 100  # Max number of documents we can request at a time from appwrite.