Skip to content

Commit

Permalink
improved gae support, still problem with IS_NOT_IN_DB
Browse files Browse the repository at this point in the history
  • Loading branch information
mdipierro committed Dec 7, 2024
1 parent 88930e2 commit b5a8c93
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
30 changes: 15 additions & 15 deletions pydal/adapters/google.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os
import re
import time
import uuid
from urllib.parse import parse_qs

from .._compat import pjoin
from .._globals import THREAD_LOCAL
from ..helpers.classes import SQLALL, FakeDriver, Reference, SQLCustomType
from ..helpers.classes import SQLALL, FakeDriver, Reference
from ..helpers.methods import use_common_filters, xorify
from ..migrator import InDBMigrator
from ..objects import Expression, Field, Query, Table
Expand All @@ -14,7 +14,6 @@
from .mysql import MySQL
from .postgres import PostgrePsyco


class GoogleMigratorMixin(object):
migrator_cls = InDBMigrator

Expand Down Expand Up @@ -149,24 +148,25 @@ def _find_work_folder(self):
class Firestore(NoSQLAdapter):
dbengine = "firestore"

REGEX_NAMESPACE = r".*//cred=(?P<cred>.+\.json)"

def _initialize_(self):

import firebase_admin
from firebase_admin import credentials, firestore

super(Firestore, self)._initialize_()
match = re.match(self.REGEX_NAMESPACE, self.uri)
if not match:
args = parse_qs(self.uri.split("//")[1]) if "//" in self.uri else {}

# for thread safety recycle the app in cache
if "cred" in args:
# not on google app engine
cred = credentials.Certificate(args["cred"][0])
else:
# on google app engine
cred = credentials.ApplicationDefault()
else:
# not on google app engine
cred = credentials.Certificate(match.group("cred"))

self._app = firebase_admin.initialize_app(cred)
self._client = firestore.client()
try:
app = firebase_admin.initialize_app(cred, args.get("name", [None])[0])
except ValueError:
app = firebase_admin.get_app()
self._client = firestore.client(app, args.get("id", [None])[0])

def find_driver(self):
return
Expand Down Expand Up @@ -208,7 +208,7 @@ def represent(self, obj, field_type, tablename=None):

def apply_filter(self, source, table, query):

from google.cloud.firestore_v1.base_query import FieldFilter
from google.cloud.firestore_v1.base_query import FieldFilter, BaseCompositeFilter

if isinstance(query, Query) and query.first is table._id:
if query.op.__name__ == "eq":
Expand Down
8 changes: 4 additions & 4 deletions pydal/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,10 +809,10 @@ def validate(self, value, record_id=None):
id = record_id or self.record_id
if isinstance(id, dict):
id = table(**id)
if not id is None:
query &= table._id != id
subset = self.dbset(query, ignore_common_filters=self.ignore_common_filters)
if subset.select(limitby=(0, 1)):
record = self.dbset(
query, ignore_common_filters=self.ignore_common_filters
).select(table._id, limitby=(0,1)).first()
if record and record[table._id.name] != id:
raise ValidationError(self.translator(self.error_message))
return value

Expand Down

0 comments on commit b5a8c93

Please sign in to comment.