Skip to content

Instantly share code, notes, and snippets.

@lapshinmr
Created August 28, 2020 09:05
Show Gist options
  • Select an option

  • Save lapshinmr/b8e85676c17b13f14d10fd91f00ded31 to your computer and use it in GitHub Desktop.

Select an option

Save lapshinmr/b8e85676c17b13f14d10fd91f00ded31 to your computer and use it in GitHub Desktop.
Mongoengine $where
def create_user_other_parcels(self, parcel_dashboard: ParcelDashboard, user: PortalUser):
# expression is needed to normalize phones for all parcels and fine parcels with the same phone
expression = """
function () {
const userPhone = '<user_phone>';
let phone = this.receiver_phone;
if (phone === undefined || phone === null) {
return false;
};
phone = phone.replace(/[+() -]/, '');
if (phone.startsWith('7') || phone.startsWith('8')) {
phone = phone.substr(1);
};
if (isNaN(phone) || !phone.startsWith('9') || phone.length !== 10) {
return false;
};
return phone === userPhone;
}
"""
expression = expression.replace('<user_phone>', user.phone)
# TO DO: Remove parcels limitation after testing. 5 - MAX USER's PARCELS
parcels = ParcelDashboard.objects(
__raw__={
'$and': [
{'id': {'$ne': parcel_dashboard.id}},
{'$where': expression}
]
}
)[:5]
for parcel in parcels:
self.get_or_create_user_parcel(parcel, user)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment