2022-08-15 10:24:22 -03:00
|
|
|
import datetime
|
2023-03-23 15:51:17 +03:30
|
|
|
import re
|
2022-07-12 15:42:20 -06:00
|
|
|
from urllib.parse import urljoin
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
import requests
|
|
|
|
|
from django.conf import settings
|
2023-03-23 15:51:17 +03:30
|
|
|
from django.core.validators import URLValidator
|
2022-08-15 10:24:22 -03:00
|
|
|
from django.utils import dateparse, timezone
|
2023-03-23 15:51:17 +03:30
|
|
|
from django.utils.regex_helper import _lazy_re_compile
|
2022-06-03 08:09:47 -06:00
|
|
|
from icalendar import Calendar
|
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
2023-02-22 07:29:59 +01:00
|
|
|
from apps.schedules.ical_utils import fetch_ical_file
|
2022-08-15 10:24:22 -03:00
|
|
|
from common.api_helpers.exceptions import BadRequest
|
2022-12-01 14:13:39 +01:00
|
|
|
from common.timezones import raise_exception_if_not_valid_timezone
|
2022-08-15 10:24:22 -03:00
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
class CurrentOrganizationDefault:
|
|
|
|
|
"""
|
|
|
|
|
Utility class to get the current organization right from the serializer field.
|
|
|
|
|
In pair with serializers.HiddenField gives an ability to create objects
|
|
|
|
|
without overriding perform_create on the model, while respecting unique_together constraints.
|
|
|
|
|
Example: organization = serializers.HiddenField(default=CurrentOrganizationDefault())
|
|
|
|
|
"""
|
|
|
|
|
|
Fix warnings when running backend tests (#2079)
# What this PR does
- update `make test` to always use `settings.ci-test`. Right now it will
use whatever the value of `DJANGO_SETTINGS_MODULE` is in
`./dev/.env.dev`, which causes ~45 tests to fail
- Fix several Python warnings that we see when running the tests
```bash
RemovedInDjango40Warning: The providing_args argument is deprecated. As it is purely documentational, it has no replacement. If you rely on this argument as documentation, you can move the text to a code comment or docstring.
alert_create_signal = django.dispatch.Signal(
```
```bash
PytestCollectionWarning: cannot collect test class 'TestOnlyBackend' because it has a __init__ constructor (from: apps/api/tests/test_alert_receive_channel_template.py)
class TestOnlyBackend(BaseMessagingBackend):
```
```bash
DeprecationWarning: The parameter 'use_aliases' in emoji.emojize() is deprecated and will be removed in version 2.0.0. Use language='alias' instead.
To hide this warning, pin/downgrade the package to 'emoji~=1.6.3'
return emoji.emojize(self.verbal_name, use_aliases=True)
```
```bash
DateTimeField CustomOnCallShift.start received a naive datetime (2023-06-01 12:53:12) while time zone support is active.
warnings.warn("DateTimeField %s received a naive datetime (%s)"
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_resolve_by_phone
/etc/app/apps/twilioapp/tests/test_phone_calls.py:173: DeprecationWarning: The 'text' argument to find()-type methods is deprecated. Use 'string' instead.
content = BeautifulSoup(content, features="html.parser").findAll(text=True)
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_resolve_by_phone
apps/twilioapp/tests/test_phone_calls.py::test_wrong_pressed_digit
/usr/local/lib/python3.11/site-packages/bs4/builder/__init__.py:545: XMLParsedAsHTMLWarning: It looks like you're parsing an XML document using an HTML parser. If this really is an HTML document (maybe it's XHTML?), you can ignore or filter this warning. If it's XML, you should know that using an XML parser will be more reliable. To parse this document as XML, make sure you have the lxml package installed, and pass the keyword argument `features="xml"` into the BeautifulSoup constructor.
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_forbidden_requests
/usr/local/lib/python3.11/site-packages/social_django/urls.py:15: RemovedInDjango40Warning: django.conf.urls.url() is deprecated in favor of django.urls.re_path().
url(r'^login/(?P<backend>[^/]+){0}$'.format(extra), views.auth,
```
```bash
apps/twilioapp/tests/test_phone_calls.py: 66 warnings
/usr/local/lib/python3.11/site-packages/debug_toolbar/utils.py:255: DeprecationWarning: currentThread() is deprecated, use current_thread() instead
thread = threading.currentThread()
```
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [ ] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
2023-06-06 20:38:00 +02:00
|
|
|
requires_context = True
|
2022-06-03 08:09:47 -06:00
|
|
|
|
Fix warnings when running backend tests (#2079)
# What this PR does
- update `make test` to always use `settings.ci-test`. Right now it will
use whatever the value of `DJANGO_SETTINGS_MODULE` is in
`./dev/.env.dev`, which causes ~45 tests to fail
- Fix several Python warnings that we see when running the tests
```bash
RemovedInDjango40Warning: The providing_args argument is deprecated. As it is purely documentational, it has no replacement. If you rely on this argument as documentation, you can move the text to a code comment or docstring.
alert_create_signal = django.dispatch.Signal(
```
```bash
PytestCollectionWarning: cannot collect test class 'TestOnlyBackend' because it has a __init__ constructor (from: apps/api/tests/test_alert_receive_channel_template.py)
class TestOnlyBackend(BaseMessagingBackend):
```
```bash
DeprecationWarning: The parameter 'use_aliases' in emoji.emojize() is deprecated and will be removed in version 2.0.0. Use language='alias' instead.
To hide this warning, pin/downgrade the package to 'emoji~=1.6.3'
return emoji.emojize(self.verbal_name, use_aliases=True)
```
```bash
DateTimeField CustomOnCallShift.start received a naive datetime (2023-06-01 12:53:12) while time zone support is active.
warnings.warn("DateTimeField %s received a naive datetime (%s)"
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_resolve_by_phone
/etc/app/apps/twilioapp/tests/test_phone_calls.py:173: DeprecationWarning: The 'text' argument to find()-type methods is deprecated. Use 'string' instead.
content = BeautifulSoup(content, features="html.parser").findAll(text=True)
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_resolve_by_phone
apps/twilioapp/tests/test_phone_calls.py::test_wrong_pressed_digit
/usr/local/lib/python3.11/site-packages/bs4/builder/__init__.py:545: XMLParsedAsHTMLWarning: It looks like you're parsing an XML document using an HTML parser. If this really is an HTML document (maybe it's XHTML?), you can ignore or filter this warning. If it's XML, you should know that using an XML parser will be more reliable. To parse this document as XML, make sure you have the lxml package installed, and pass the keyword argument `features="xml"` into the BeautifulSoup constructor.
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_forbidden_requests
/usr/local/lib/python3.11/site-packages/social_django/urls.py:15: RemovedInDjango40Warning: django.conf.urls.url() is deprecated in favor of django.urls.re_path().
url(r'^login/(?P<backend>[^/]+){0}$'.format(extra), views.auth,
```
```bash
apps/twilioapp/tests/test_phone_calls.py: 66 warnings
/usr/local/lib/python3.11/site-packages/debug_toolbar/utils.py:255: DeprecationWarning: currentThread() is deprecated, use current_thread() instead
thread = threading.currentThread()
```
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [ ] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
2023-06-06 20:38:00 +02:00
|
|
|
def __call__(self, serializer_field):
|
|
|
|
|
self.organization = serializer_field.context["request"].auth.organization
|
2022-06-03 08:09:47 -06:00
|
|
|
return self.organization
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
return "%s()" % self.__class__.__name__
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CurrentTeamDefault:
|
|
|
|
|
"""
|
|
|
|
|
Utility class to get the current team right from the serializer field.
|
|
|
|
|
"""
|
|
|
|
|
|
Fix warnings when running backend tests (#2079)
# What this PR does
- update `make test` to always use `settings.ci-test`. Right now it will
use whatever the value of `DJANGO_SETTINGS_MODULE` is in
`./dev/.env.dev`, which causes ~45 tests to fail
- Fix several Python warnings that we see when running the tests
```bash
RemovedInDjango40Warning: The providing_args argument is deprecated. As it is purely documentational, it has no replacement. If you rely on this argument as documentation, you can move the text to a code comment or docstring.
alert_create_signal = django.dispatch.Signal(
```
```bash
PytestCollectionWarning: cannot collect test class 'TestOnlyBackend' because it has a __init__ constructor (from: apps/api/tests/test_alert_receive_channel_template.py)
class TestOnlyBackend(BaseMessagingBackend):
```
```bash
DeprecationWarning: The parameter 'use_aliases' in emoji.emojize() is deprecated and will be removed in version 2.0.0. Use language='alias' instead.
To hide this warning, pin/downgrade the package to 'emoji~=1.6.3'
return emoji.emojize(self.verbal_name, use_aliases=True)
```
```bash
DateTimeField CustomOnCallShift.start received a naive datetime (2023-06-01 12:53:12) while time zone support is active.
warnings.warn("DateTimeField %s received a naive datetime (%s)"
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_resolve_by_phone
/etc/app/apps/twilioapp/tests/test_phone_calls.py:173: DeprecationWarning: The 'text' argument to find()-type methods is deprecated. Use 'string' instead.
content = BeautifulSoup(content, features="html.parser").findAll(text=True)
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_resolve_by_phone
apps/twilioapp/tests/test_phone_calls.py::test_wrong_pressed_digit
/usr/local/lib/python3.11/site-packages/bs4/builder/__init__.py:545: XMLParsedAsHTMLWarning: It looks like you're parsing an XML document using an HTML parser. If this really is an HTML document (maybe it's XHTML?), you can ignore or filter this warning. If it's XML, you should know that using an XML parser will be more reliable. To parse this document as XML, make sure you have the lxml package installed, and pass the keyword argument `features="xml"` into the BeautifulSoup constructor.
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_forbidden_requests
/usr/local/lib/python3.11/site-packages/social_django/urls.py:15: RemovedInDjango40Warning: django.conf.urls.url() is deprecated in favor of django.urls.re_path().
url(r'^login/(?P<backend>[^/]+){0}$'.format(extra), views.auth,
```
```bash
apps/twilioapp/tests/test_phone_calls.py: 66 warnings
/usr/local/lib/python3.11/site-packages/debug_toolbar/utils.py:255: DeprecationWarning: currentThread() is deprecated, use current_thread() instead
thread = threading.currentThread()
```
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [ ] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
2023-06-06 20:38:00 +02:00
|
|
|
requires_context = True
|
2022-06-03 08:09:47 -06:00
|
|
|
|
Fix warnings when running backend tests (#2079)
# What this PR does
- update `make test` to always use `settings.ci-test`. Right now it will
use whatever the value of `DJANGO_SETTINGS_MODULE` is in
`./dev/.env.dev`, which causes ~45 tests to fail
- Fix several Python warnings that we see when running the tests
```bash
RemovedInDjango40Warning: The providing_args argument is deprecated. As it is purely documentational, it has no replacement. If you rely on this argument as documentation, you can move the text to a code comment or docstring.
alert_create_signal = django.dispatch.Signal(
```
```bash
PytestCollectionWarning: cannot collect test class 'TestOnlyBackend' because it has a __init__ constructor (from: apps/api/tests/test_alert_receive_channel_template.py)
class TestOnlyBackend(BaseMessagingBackend):
```
```bash
DeprecationWarning: The parameter 'use_aliases' in emoji.emojize() is deprecated and will be removed in version 2.0.0. Use language='alias' instead.
To hide this warning, pin/downgrade the package to 'emoji~=1.6.3'
return emoji.emojize(self.verbal_name, use_aliases=True)
```
```bash
DateTimeField CustomOnCallShift.start received a naive datetime (2023-06-01 12:53:12) while time zone support is active.
warnings.warn("DateTimeField %s received a naive datetime (%s)"
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_resolve_by_phone
/etc/app/apps/twilioapp/tests/test_phone_calls.py:173: DeprecationWarning: The 'text' argument to find()-type methods is deprecated. Use 'string' instead.
content = BeautifulSoup(content, features="html.parser").findAll(text=True)
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_resolve_by_phone
apps/twilioapp/tests/test_phone_calls.py::test_wrong_pressed_digit
/usr/local/lib/python3.11/site-packages/bs4/builder/__init__.py:545: XMLParsedAsHTMLWarning: It looks like you're parsing an XML document using an HTML parser. If this really is an HTML document (maybe it's XHTML?), you can ignore or filter this warning. If it's XML, you should know that using an XML parser will be more reliable. To parse this document as XML, make sure you have the lxml package installed, and pass the keyword argument `features="xml"` into the BeautifulSoup constructor.
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_forbidden_requests
/usr/local/lib/python3.11/site-packages/social_django/urls.py:15: RemovedInDjango40Warning: django.conf.urls.url() is deprecated in favor of django.urls.re_path().
url(r'^login/(?P<backend>[^/]+){0}$'.format(extra), views.auth,
```
```bash
apps/twilioapp/tests/test_phone_calls.py: 66 warnings
/usr/local/lib/python3.11/site-packages/debug_toolbar/utils.py:255: DeprecationWarning: currentThread() is deprecated, use current_thread() instead
thread = threading.currentThread()
```
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [ ] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
2023-06-06 20:38:00 +02:00
|
|
|
def __call__(self, serializer_field):
|
|
|
|
|
self.team = serializer_field.context["request"].user.current_team
|
2022-06-03 08:09:47 -06:00
|
|
|
return self.team
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
return "%s()" % self.__class__.__name__
|
|
|
|
|
|
|
|
|
|
|
2023-03-23 15:51:17 +03:30
|
|
|
class URLValidatorWithoutTLD(URLValidator):
|
|
|
|
|
"""
|
|
|
|
|
Overrides Django URLValidator Regex. It removes the tld part because
|
|
|
|
|
most of the time, containers don't have any TLD in their urls and such outgoing webhooks
|
|
|
|
|
can't be registered.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
host_re = (
|
|
|
|
|
"("
|
|
|
|
|
+ URLValidator.hostname_re
|
|
|
|
|
+ URLValidator.domain_re
|
|
|
|
|
+ URLValidator.tld_re
|
|
|
|
|
+ "|"
|
|
|
|
|
+ URLValidator.hostname_re
|
|
|
|
|
+ "|localhost)"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
regex = _lazy_re_compile(
|
|
|
|
|
r"^(?:[a-z0-9.+-]*)://" # scheme is validated separately
|
|
|
|
|
r"(?:[^\s:@/]+(?::[^\s:@/]*)?@)?" # user:pass authentication
|
|
|
|
|
r"(?:" + URLValidator.ipv4_re + "|" + URLValidator.ipv6_re + "|" + host_re + ")"
|
|
|
|
|
r"(?::[0-9]{1,5})?" # port
|
|
|
|
|
r"(?:[/?#][^\s]*)?" # resource path
|
|
|
|
|
r"\Z",
|
|
|
|
|
re.IGNORECASE,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2023-03-10 14:00:06 -03:00
|
|
|
class CurrentUserDefault:
|
|
|
|
|
"""
|
|
|
|
|
Utility class to get the current user right from the serializer field.
|
|
|
|
|
"""
|
|
|
|
|
|
Fix warnings when running backend tests (#2079)
# What this PR does
- update `make test` to always use `settings.ci-test`. Right now it will
use whatever the value of `DJANGO_SETTINGS_MODULE` is in
`./dev/.env.dev`, which causes ~45 tests to fail
- Fix several Python warnings that we see when running the tests
```bash
RemovedInDjango40Warning: The providing_args argument is deprecated. As it is purely documentational, it has no replacement. If you rely on this argument as documentation, you can move the text to a code comment or docstring.
alert_create_signal = django.dispatch.Signal(
```
```bash
PytestCollectionWarning: cannot collect test class 'TestOnlyBackend' because it has a __init__ constructor (from: apps/api/tests/test_alert_receive_channel_template.py)
class TestOnlyBackend(BaseMessagingBackend):
```
```bash
DeprecationWarning: The parameter 'use_aliases' in emoji.emojize() is deprecated and will be removed in version 2.0.0. Use language='alias' instead.
To hide this warning, pin/downgrade the package to 'emoji~=1.6.3'
return emoji.emojize(self.verbal_name, use_aliases=True)
```
```bash
DateTimeField CustomOnCallShift.start received a naive datetime (2023-06-01 12:53:12) while time zone support is active.
warnings.warn("DateTimeField %s received a naive datetime (%s)"
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_resolve_by_phone
/etc/app/apps/twilioapp/tests/test_phone_calls.py:173: DeprecationWarning: The 'text' argument to find()-type methods is deprecated. Use 'string' instead.
content = BeautifulSoup(content, features="html.parser").findAll(text=True)
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_resolve_by_phone
apps/twilioapp/tests/test_phone_calls.py::test_wrong_pressed_digit
/usr/local/lib/python3.11/site-packages/bs4/builder/__init__.py:545: XMLParsedAsHTMLWarning: It looks like you're parsing an XML document using an HTML parser. If this really is an HTML document (maybe it's XHTML?), you can ignore or filter this warning. If it's XML, you should know that using an XML parser will be more reliable. To parse this document as XML, make sure you have the lxml package installed, and pass the keyword argument `features="xml"` into the BeautifulSoup constructor.
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_forbidden_requests
/usr/local/lib/python3.11/site-packages/social_django/urls.py:15: RemovedInDjango40Warning: django.conf.urls.url() is deprecated in favor of django.urls.re_path().
url(r'^login/(?P<backend>[^/]+){0}$'.format(extra), views.auth,
```
```bash
apps/twilioapp/tests/test_phone_calls.py: 66 warnings
/usr/local/lib/python3.11/site-packages/debug_toolbar/utils.py:255: DeprecationWarning: currentThread() is deprecated, use current_thread() instead
thread = threading.currentThread()
```
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [ ] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
2023-06-06 20:38:00 +02:00
|
|
|
requires_context = True
|
2023-03-10 14:00:06 -03:00
|
|
|
|
Fix warnings when running backend tests (#2079)
# What this PR does
- update `make test` to always use `settings.ci-test`. Right now it will
use whatever the value of `DJANGO_SETTINGS_MODULE` is in
`./dev/.env.dev`, which causes ~45 tests to fail
- Fix several Python warnings that we see when running the tests
```bash
RemovedInDjango40Warning: The providing_args argument is deprecated. As it is purely documentational, it has no replacement. If you rely on this argument as documentation, you can move the text to a code comment or docstring.
alert_create_signal = django.dispatch.Signal(
```
```bash
PytestCollectionWarning: cannot collect test class 'TestOnlyBackend' because it has a __init__ constructor (from: apps/api/tests/test_alert_receive_channel_template.py)
class TestOnlyBackend(BaseMessagingBackend):
```
```bash
DeprecationWarning: The parameter 'use_aliases' in emoji.emojize() is deprecated and will be removed in version 2.0.0. Use language='alias' instead.
To hide this warning, pin/downgrade the package to 'emoji~=1.6.3'
return emoji.emojize(self.verbal_name, use_aliases=True)
```
```bash
DateTimeField CustomOnCallShift.start received a naive datetime (2023-06-01 12:53:12) while time zone support is active.
warnings.warn("DateTimeField %s received a naive datetime (%s)"
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_resolve_by_phone
/etc/app/apps/twilioapp/tests/test_phone_calls.py:173: DeprecationWarning: The 'text' argument to find()-type methods is deprecated. Use 'string' instead.
content = BeautifulSoup(content, features="html.parser").findAll(text=True)
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_resolve_by_phone
apps/twilioapp/tests/test_phone_calls.py::test_wrong_pressed_digit
/usr/local/lib/python3.11/site-packages/bs4/builder/__init__.py:545: XMLParsedAsHTMLWarning: It looks like you're parsing an XML document using an HTML parser. If this really is an HTML document (maybe it's XHTML?), you can ignore or filter this warning. If it's XML, you should know that using an XML parser will be more reliable. To parse this document as XML, make sure you have the lxml package installed, and pass the keyword argument `features="xml"` into the BeautifulSoup constructor.
```
```bash
apps/twilioapp/tests/test_phone_calls.py::test_forbidden_requests
/usr/local/lib/python3.11/site-packages/social_django/urls.py:15: RemovedInDjango40Warning: django.conf.urls.url() is deprecated in favor of django.urls.re_path().
url(r'^login/(?P<backend>[^/]+){0}$'.format(extra), views.auth,
```
```bash
apps/twilioapp/tests/test_phone_calls.py: 66 warnings
/usr/local/lib/python3.11/site-packages/debug_toolbar/utils.py:255: DeprecationWarning: currentThread() is deprecated, use current_thread() instead
thread = threading.currentThread()
```
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [ ] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
2023-06-06 20:38:00 +02:00
|
|
|
def __call__(self, serializer_field):
|
|
|
|
|
self.user = serializer_field.context["request"].user
|
2023-03-10 14:00:06 -03:00
|
|
|
return self.user
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
return "%s()" % self.__class__.__name__
|
|
|
|
|
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
def validate_ical_url(url):
|
|
|
|
|
if url:
|
|
|
|
|
if settings.BASE_URL in url:
|
|
|
|
|
raise serializers.ValidationError("Potential self-reference")
|
|
|
|
|
try:
|
2023-02-22 07:29:59 +01:00
|
|
|
ical_file = fetch_ical_file(url)
|
2022-06-03 08:09:47 -06:00
|
|
|
Calendar.from_ical(ical_file)
|
|
|
|
|
except requests.exceptions.RequestException:
|
|
|
|
|
raise serializers.ValidationError("Ical download failed")
|
|
|
|
|
except ValueError:
|
|
|
|
|
raise serializers.ValidationError("Ical parse failed")
|
|
|
|
|
return url
|
|
|
|
|
return None
|
2022-07-12 15:42:20 -06:00
|
|
|
|
|
|
|
|
|
2022-07-25 09:12:50 -06:00
|
|
|
"""
|
|
|
|
|
This utility function is for building a URL when we don't know if the base URL
|
|
|
|
|
has been given a trailing / such as reading from environment variable or user
|
|
|
|
|
input. If the base URL is coming from a validated model field urljoin can be used
|
|
|
|
|
instead. Do not use this function to append query parameters since a / is added
|
|
|
|
|
to the end of the base URL if there isn't one.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
2022-07-22 10:02:00 -06:00
|
|
|
def create_engine_url(path, override_base=None):
|
2022-07-12 15:42:20 -06:00
|
|
|
base = settings.BASE_URL
|
2022-07-22 10:02:00 -06:00
|
|
|
if override_base:
|
|
|
|
|
base = override_base
|
2022-07-12 15:42:20 -06:00
|
|
|
if not base.endswith("/"):
|
|
|
|
|
base += "/"
|
|
|
|
|
trimmed_path = path.lstrip("/")
|
|
|
|
|
return urljoin(base, trimmed_path)
|
2022-08-15 10:24:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_date_range_from_request(request):
|
|
|
|
|
"""Extract timezone, starting date and number of days params from request.
|
|
|
|
|
|
|
|
|
|
Used mainly for schedules and shifts API.
|
|
|
|
|
"""
|
|
|
|
|
user_tz = request.query_params.get("user_tz", "UTC")
|
2022-12-01 14:13:39 +01:00
|
|
|
raise_exception_if_not_valid_timezone(user_tz)
|
2022-08-15 10:24:22 -03:00
|
|
|
|
|
|
|
|
date = timezone.now().date()
|
|
|
|
|
date_param = request.query_params.get("date")
|
|
|
|
|
if date_param is not None:
|
|
|
|
|
try:
|
|
|
|
|
date = dateparse.parse_date(date_param)
|
|
|
|
|
except ValueError:
|
|
|
|
|
raise BadRequest(detail="Invalid date format")
|
|
|
|
|
else:
|
|
|
|
|
if date is None:
|
|
|
|
|
raise BadRequest(detail="Invalid date format")
|
|
|
|
|
|
|
|
|
|
starting_date = date if request.query_params.get("date") else None
|
|
|
|
|
if starting_date is None:
|
|
|
|
|
# default to current week start
|
|
|
|
|
starting_date = date - datetime.timedelta(days=date.weekday())
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
days = int(request.query_params.get("days", 7)) # fallback to a week
|
|
|
|
|
except ValueError:
|
|
|
|
|
raise BadRequest(detail="Invalid days format")
|
|
|
|
|
|
|
|
|
|
return user_tz, starting_date, days
|
2023-05-24 14:27:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_phone_number_is_valid(phone_number):
|
|
|
|
|
return re.match(r"^\+\d{8,15}$", phone_number) is not None
|