Skip to content

Commit

Permalink
Merge pull request #5 from certego/develop
Browse files Browse the repository at this point in the history
1.3.12
  • Loading branch information
0ssigeno authored Nov 20, 2023
2 parents f5ffbd5 + 9302a25 commit b2a6b0b
Show file tree
Hide file tree
Showing 86 changed files with 3,161 additions and 1,333 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pythonpublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ jobs:
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ tests/logs
Gemfile.lock

/docs/.jekyll-metadata
/docs/djongocs/assets/*
/docs/djongocs/assets/*
2 changes: 1 addition & 1 deletion djongo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
# * Renz Ladia
# * thestick613

__version__ = '1.3.11'
__version__ = '1.3.12'
2 changes: 2 additions & 0 deletions djongo/database.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# THIS FILE WAS CHANGED ON - 19 Aug 2022

from logging import getLogger
from pymongo import MongoClient

Expand Down
8 changes: 4 additions & 4 deletions djongo/models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ def _make_filter(self, *objs):
def add(self, *objs):
_filter = self._make_filter(*objs)
lh_field, rh_field = self.field.related_fields[0]
self.mongo_update(
self.mongo_update_one(
_filter,
{
'$addToSet': {
Expand Down Expand Up @@ -869,7 +869,7 @@ def add(self, *objs):
fks.update(new_fks)

db = router.db_for_write(self.instance.__class__, instance=self.instance)
self.instance_manager.db_manager(db).mongo_update(
self.instance_manager.db_manager(db).mongo_update_one(
self._make_filter(),
{
'$addToSet': {
Expand All @@ -895,7 +895,7 @@ def _remove(self, to_del):
fks = getattr(self.instance, self.field.attname)
fks.difference_update(to_del)
db = self._db or router.db_for_write(self.instance.__class__, instance=self.instance)
self.instance_manager.db_manager(db).mongo_update(
self.instance_manager.db_manager(db).mongo_update_one(
self._make_filter(),
{
'$pull': {
Expand All @@ -908,7 +908,7 @@ def _remove(self, to_del):

def clear(self):
db = router.db_for_write(self.instance.__class__, instance=self.instance)
self.instance_manager.db_manager(db).mongo_update(
self.instance_manager.db_manager(db).mongo_update_one(
self._make_filter(),
{
'$set': {
Expand Down
108 changes: 46 additions & 62 deletions djongo/sql2mongo/operators.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
# THIS FILE WAS CHANGED ON - 09 May 2022
# THIS FILE WAS CHANGED ON - 21 Nov 2022

import re
import typing
import json
from itertools import chain

import bson.regex
from sqlparse import tokens
from sqlparse.sql import Token, Parenthesis, Comparison, IdentifierList, Identifier

from ..exceptions import SQLDecodeError
from .sql_tokens import SQLToken, SQLStatement
from . import query

import logging
logger = logging.getLogger(__name__)


def re_index(value: str):
match = list(re.finditer(r'%\(([0-9]+)\)s', value, flags=re.IGNORECASE))
Expand Down Expand Up @@ -162,14 +160,14 @@ def negate(self):
self.is_negated = True


class LikeOp(_BinaryOp):

def __init__(self, *args, **kwargs):
super().__init__(name='LIKE', *args, **kwargs)
self._regex = None
self._make_regex(self.statement.next())
class LikeOp:
def __init__(self, to_match):
self.to_match = self.check_embedded(to_match)
self.field_ext = ''
self.regex = self.make_regex()

def check_embedded(self, to_match):
@staticmethod
def check_embedded(to_match):
try:
check_dict = to_match
replace_chars = "\\%'"
Expand All @@ -186,31 +184,17 @@ def check_embedded(self, to_match):
except Exception as e:
return to_match

def _make_regex(self, token):
index = SQLToken.placeholder_index(token)

to_match = self.params[index]
to_match = self.check_embedded(to_match)
if isinstance(to_match, str):
to_match = to_match.replace('%', '.*')
self._regex = '^' + to_match + '$'
elif isinstance(to_match, dict):
field_ext, to_match = next(iter(to_match.items()))
self._field += '.' + field_ext
self._regex = to_match
def make_regex(self):
if isinstance(self.to_match, str):
to_match = self.to_match.replace('%', '.*')
regex = '^' + to_match + '$'
elif isinstance(self.to_match, dict):
regex = self.to_match
field_ext, self.to_match = next(iter(self.to_match.items()))
self.field_ext += field_ext
else:
raise SQLDecodeError

def to_mongo(self):
return {self._field: {'$regex': self._regex}}


class iLikeOp(LikeOp):
def to_mongo(self):
return {self._field: {
'$regex': self._regex,
'$options': 'im'
}}
return regex


class IsOp(_BinaryOp):
Expand Down Expand Up @@ -391,51 +375,33 @@ def _token2op(self,
statement: SQLStatement) -> '_Op':
op = None
kw = {'statement': statement, 'query': self.query}
logger.debug(f"_token2op query {self.query}, token {tok}")
if tok.match(tokens.Keyword, 'AND'):
logger.debug(f"_token2op query {self.query}, token {tok} match AND")
op = AndOp(**kw)

elif tok.match(tokens.Keyword, 'OR'):
logger.debug(f"_token2op query {self.query}, token {tok} match OR")
op = OrOp(**kw)

elif tok.match(tokens.Keyword, 'IN'):
logger.debug(f"_token2op query {self.query}, token {tok} match IN")
op = InOp(**kw)

elif tok.match(tokens.Keyword, 'NOT'):
if statement.next_token.match(tokens.Keyword, 'IN'):
logger.debug(f"_token2op query {self.query}, token {tok} match NOT IN")
op = NotInOp(**kw)
statement.skip(1)
else:
logger.debug(f"_token2op query {self.query}, token {tok} match NOT")
op = NotOp(**kw)

elif tok.match(tokens.Keyword, 'LIKE'):
logger.debug(f"_token2op query {self.query}, token {tok} match LIKE")
op = LikeOp(**kw)

elif tok.match(tokens.Keyword, 'iLIKE'):
logger.debug(f"_token2op query {self.query}, token {tok} match iLIKE")
op = iLikeOp(**kw)

elif tok.match(tokens.Keyword, 'BETWEEN'):
logger.debug(f"_token2op query {self.query}, token {tok} match BETWEEN")
op = BetweenOp(**kw)
statement.skip(3)

elif tok.match(tokens.Keyword, 'IS'):
logger.debug(f"_token2op query {self.query}, token {tok} match IS")
op = IsOp(**kw)

elif isinstance(tok, Comparison):
logger.debug(f"_token2op query {self.query}, token {tok} match Comparison")
op = CmpOp(tok, self.query)

elif isinstance(tok, Parenthesis):
logger.debug(f"_token2op query {self.query}, token {tok} match Parenthesis")
if (tok[1].match(tokens.Name.Placeholder, '.*', regex=True)
or tok[1].match(tokens.Keyword, 'Null')
or isinstance(tok[1], IdentifierList)
Expand All @@ -446,11 +412,9 @@ def _token2op(self,
op = ParenthesisOp(SQLStatement(tok), self.query)

elif tok.match(tokens.Punctuation, (')', '(')):
logger.debug(f"_token2op query {self.query}, token {tok} match Punctuation")
pass

elif isinstance(tok, Identifier):
logger.debug(f"_token2op query {self.query}, token {tok} match Identifier")
pass
else:
raise SQLDecodeError
Expand Down Expand Up @@ -549,7 +513,8 @@ def __init__(self, *args, **kwargs):
if isinstance(self.statement.right, Identifier):
raise SQLDecodeError('Join using WHERE not supported')

self._operator = OPERATOR_MAP[self.statement.token_next(0)[1].value]
self._sql_operator = self.statement.token_next(0)[1].value
self._operator = OPERATOR_MAP[self._sql_operator]
index = re_index(self.statement.right.value)

if self._operator in NEW_OPERATORS:
Expand All @@ -558,8 +523,14 @@ def __init__(self, *args, **kwargs):
else:
self._constant = self.params[index] if index is not None else MAP_INDEX_NONE[self.statement.right.value]

if self._sql_operator in LIKE_OPERATOR_MAP.keys():
like_op = LikeOp(to_match=self._constant)
self._constant = like_op.regex

if isinstance(self._constant, dict):
self._field_ext, self._constant = next(iter(self._constant.items()))
elif self._sql_operator in LIKE_OPERATOR_MAP.keys():
self._field_ext = getattr(like_op, 'field_ext', None)
else:
self._field_ext = None

Expand All @@ -573,12 +544,26 @@ def to_mongo(self):
field = self._identifier.field
if self._field_ext:
field += '.' + self._field_ext

if not self.is_negated:
return {field: {self._operator: self._constant}}
mongo = {field: {self._operator: self._constant}}
if self._sql_operator == 'iLIKE':
mongo[field]['$options'] = 'im'
else:
return {field: {'$not': {self._operator: self._constant}}}
if self._sql_operator in LIKE_OPERATOR_MAP:
regex = bson.regex.Regex(self._constant, 'i')
if self._sql_operator == 'iLIKE':
regex = bson.regex.Regex(self._constant, 'i')
mongo = {field: {'$not': regex}}
else:
mongo = {field: {'$not': {self._operator: self._constant}}}

return mongo


LIKE_OPERATOR_MAP = {
'LIKE': '$regex',
'iLIKE': '$regex',
}

OPERATOR_MAP = {
'=': '$eq',
Expand All @@ -588,12 +573,11 @@ def to_mongo(self):
'<=': '$lte',
'IN': '$in',
'NOT IN': '$nin',
'iLIKE': '$eq'
**LIKE_OPERATOR_MAP,
}
OPERATOR_PRECEDENCE = {
'IS': 9,
'BETWEEN': 8,
'iLIKE': 7,
'IS': 8,
'BETWEEN': 7,
'LIKE': 6,
'IN': 5,
'NOT IN': 4,
Expand Down
2 changes: 1 addition & 1 deletion djongo/sql2mongo/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
SQL constructors.
"""

# THIS FILE WAS CHANGED ON - 19 Apr 2022
# THIS FILE WAS CHANGED ON - 19 Aug 2022

import abc
import re
Expand Down
2 changes: 2 additions & 0 deletions djongo/storage.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# THIS FILE WAS CHANGED ON - 19 Aug 2022

import os
from urllib.parse import urljoin

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7B227073704964223A2239373943394538343346343131343044463144313834343232393232313734313034353044314339464446394437384337313531303944334643463542433731222C2276657273696F6E223A312C22637265617465644F6E223A313536363233343735303036312C227369676E6174757265223A22333038303036303932613836343838366637306430313037303261303830333038303032303130313331306633303064303630393630383634383031363530333034303230313035303033303830303630393261383634383836663730643031303730313030303061303830333038323033653333303832303338386130303330323031303230323038346333303431343935313964353433363330306130363038326138363438636533643034303330323330376133313265333032633036303335353034303330633235343137303730366336353230343137303730366336393633363137343639366636653230343936653734363536373732363137343639366636653230343334313230326432303437333333313236333032343036303335353034306230633164343137303730366336353230343336353732373436393636363936333631373436393666366532303431373537343638366637323639373437393331313333303131303630333535303430613063306134313730373036633635323034393665363332653331306233303039303630333535303430363133303235353533333031653137306433313339333033353331333833303331333333323335333735613137306433323334333033353331333633303331333333323335333735613330356633313235333032333036303335353034303330633163363536333633326437333664373032643632373236663662363537323264373336393637366535663535343333343264353035323466343433313134333031323036303335353034306230633062363934663533323035333739373337343635366437333331313333303131303630333535303430613063306134313730373036633635323034393665363332653331306233303039303630333535303430363133303235353533333035393330313330363037326138363438636533643032303130363038326138363438636533643033303130373033343230303034633231353737656465626436633762323231386636386464373039306131323138646337623062643666326332383364383436303935643934616634613534313162383334323065643831316633343037653833333331663163353463336637656233323230643662616435643465666634393238393839336537633066313361333832303231313330383230323064333030633036303335353164313330313031666630343032333030303330316630363033353531643233303431383330313638303134323366323439633434663933653465663237653663346636323836633366613262626664326534623330343530363038326230363031303530353037303130313034333933303337333033353036303832623036303130353035303733303031383632393638373437343730336132663266366636333733373032653631373037303663363532653633366636643266366636333733373033303334326436313730373036633635363136393633363133333330333233303832303131643036303335353164323030343832303131343330383230313130333038323031306330363039326138363438383666373633363430353031333038316665333038316333303630383262303630313035303530373032303233303831623630633831623335323635366336393631366536333635323036663665323037343638363937333230363336353732373436393636363936333631373436353230363237393230363136653739323037303631373237343739323036313733373337353664363537333230363136333633363537303734363136653633363532303666363632303734363836353230373436383635366532303631373037303663363936333631363236633635323037333734363136653634363137323634323037343635373236643733323036313665363432303633366636653634363937343639366636653733323036663636323037353733363532633230363336353732373436393636363936333631373436353230373036663663363936333739323036313665363432303633363537323734363936363639363336313734363936663665323037303732363136333734363936333635323037333734363137343635366436353665373437333265333033363036303832623036303130353035303730323031313632613638373437343730336132663266373737373737326536313730373036633635326536333666366432663633363537323734363936363639363336313734363536313735373436383666373236393734373932663330333430363033353531643166303432643330326233303239613032376130323538363233363837343734373033613266326636333732366332653631373037303663363532653633366636643266363137303730366336353631363936333631333332653633373236633330316430363033353531643065303431363034313439343537646236666435373438313836383938393736326637653537383530376537396235383234333030653036303335353164306630313031666630343034303330323037383033303066303630393261383634383836663736333634303631643034303230353030333030613036303832613836343863653364303430333032303334393030333034363032323130306265303935373166653731653165373335623535653561666163623463373266656234343566333031383532323263373235313030326236316562643666353530323231303064313862333530613564643664643665623137343630333562313165623263653837636661336536616636636264383338303839306463383263646461613633333038323032656533303832303237356130303330323031303230323038343936643266626633613938646139373330306130363038326138363438636533643034303330323330363733313162333031393036303335353034303330633132343137303730366336353230353236663666373432303433343132303264323034373333333132363330323430363033353530343062306331643431373037303663363532303433363537323734363936363639363336313734363936663665323034313735373436383666373236393734373933313133333031313036303335353034306130633061343137303730366336353230343936653633326533313062333030393036303335353034303631333032353535333330316531373064333133343330333533303336333233333334333633333330356131373064333233393330333533303336333233333334333633333330356133303761333132653330326330363033353530343033306332353431373037303663363532303431373037303663363936333631373436393666366532303439366537343635363737323631373436393666366532303433343132303264323034373333333132363330323430363033353530343062306331643431373037303663363532303433363537323734363936363639363336313734363936663665323034313735373436383666373236393734373933313133333031313036303335353034306130633061343137303730366336353230343936653633326533313062333030393036303335353034303631333032353535333330353933303133303630373261383634386365336430323031303630383261383634386365336430333031303730333432303030346630313731313834313964373634383564353161356532353831303737366538383061326566646537626165346465303864666334623933653133333536643536363562333561653232643039373736306432323465376262613038666437363137636538386362373662623636373062656338653832393834666635343435613338316637333038316634333034363036303832623036303130353035303730313031303433613330333833303336303630383262303630313035303530373330303138363261363837343734373033613266326636663633373337303265363137303730366336353265363336663664326636663633373337303330333432643631373037303663363537323666366637343633363136373333333031643036303335353164306530343136303431343233663234396334346639336534656632376536633466363238366333666132626266643265346233303066303630333535316431333031303166663034303533303033303130316666333031663036303335353164323330343138333031363830313462626230646561313538333338383961613438613939646562656264656261666461636232346162333033373036303335353164316630343330333032653330326361303261613032383836323636383734373437303361326632663633373236633265363137303730366336353265363336663664326636313730373036633635373236663666373436333631363733333265363337323663333030653036303335353164306630313031666630343034303330323031303633303130303630613261383634383836663736333634303630323065303430323035303033303061303630383261383634386365336430343033303230333637303033303634303233303361636637323833353131363939623138366662333563333536636136326266663431376564643930663735346461323865626566313963383135653432623738396638393866373962353939663938643534313064386639646539633266653032333033323264643534343231623061333035373736633564663333383362393036376664313737633263323136643936346663363732363938323132366635346638376137643162393963623962303938393231363130363939306630393932316430303030333138323031386233303832303138373032303130313330383138363330376133313265333032633036303335353034303330633235343137303730366336353230343137303730366336393633363137343639366636653230343936653734363536373732363137343639366636653230343334313230326432303437333333313236333032343036303335353034306230633164343137303730366336353230343336353732373436393636363936333631373436393666366532303431373537343638366637323639373437393331313333303131303630333535303430613063306134313730373036633635323034393665363332653331306233303039303630333535303430363133303235353533303230383463333034313439353139643534333633303064303630393630383634383031363530333034303230313035303061303831393533303138303630393261383634383836663730643031303930333331306230363039326138363438383666373064303130373031333031633036303932613836343838366637306430313039303533313066313730643331333933303338333133393331333733313332333333303561333032613036303932613836343838366637306430313039333433313164333031623330306430363039363038363438303136353033303430323031303530306131306130363038326138363438636533643034303330323330326630363039326138363438383666373064303130393034333132323034323062303731303365313430613462386231376262613230316130336163643036396234653431366232613263383066383661383338313435633239373566633131333030613036303832613836343863653364303430333032303434363330343430323230343639306264636637626461663833636466343934396534633035313039656463663334373665303564373261313264376335666538633033303033343464663032323032363764353863393365626233353031333836363062353730373938613064643731313734316262353864626436613138363633353038353431656565393035303030303030303030303030227D
2 changes: 1 addition & 1 deletion docs/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
</div>
<div class="initial-content">
<div id="main" role="main">
<app-root></app-root>
<H1>Page not found</H1>
</div>
</div>
<script src="runtime.3e2bed8c3d2369259daf.js" defer></script><script src="polyfills.af5bb2ebea48c6645061.js" defer></script><script src="main.152dd70428ae550a6e32.js" defer></script>
Expand Down
2 changes: 1 addition & 1 deletion docs/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ source 'https://rubygems.org'
gem "jekyll-include-cache"

# run server
# gem "minimal-mistakes-jekyll"
gem "minimal-mistakes-jekyll"
# gem "jekyll", "~> 3.5"
# gem 'wdm'
Loading

0 comments on commit b2a6b0b

Please sign in to comment.