-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create log api endpoints Signed-off-by: Mairi Dulaney <[email protected]> * Change TypeError to ValueError Signed-off-by: Mairi Dulaney <[email protected]> * Change to using setting rather than database for log api permission Signed-off-by: Mairi Dulaney <[email protected]> * switch to using arrow Signed-off-by: Mairi Dulaney <[email protected]> * Updates to api output Signed-off-by: Mairi Dulaney <[email protected]> * Tweak filter Signed-off-by: Mairi Dulaney <[email protected]> * Better permissions Signed-off-by: Mairi Dulaney <[email protected]> * change LOG_API_ACCESS_PROJECT_ID to OHLOG_PROJECT_ID Signed-off-by: Mairi Dulaney <[email protected]> * Updat docstrings Signed-off-by: Mairi Dulaney <[email protected]>
- Loading branch information
Showing
7 changed files
with
183 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import datetime | ||
|
||
import arrow | ||
|
||
from rest_framework.filters import BaseFilterBackend | ||
|
||
|
||
class AccessLogFilter(BaseFilterBackend): | ||
""" | ||
Used for filtering data returned by the custom API for OHLOG_PROJECT_ID. | ||
""" | ||
|
||
def filter_queryset(self, request, queryset, view): | ||
|
||
start_date = request.query_params.get("start_date", None) | ||
end_date = request.query_params.get("end_date", None) | ||
if start_date: | ||
try: | ||
start_date = arrow.get(start_date).datetime | ||
except (TypeError, ValueError): | ||
start_date = None | ||
if end_date: | ||
try: | ||
end_date = arrow.get(end_date).datetime | ||
# Special check if start_date and end_date is the same | ||
# If this is the case, assume that a 24 hour period is meant, and set end_time accordingly | ||
if start_date == end_date: | ||
end_date = end_date + datetime.timedelta( | ||
hours=23, minutes=59, seconds=59 | ||
) | ||
except (TypeError, ValueError): | ||
end_date = None | ||
if queryset.model.__name__ == "AWSDataFileAccessLog": | ||
# AWS uses 'time' for the timestamp rather than 'date' | ||
if start_date: | ||
queryset = queryset.filter(time__gte=start_date) | ||
if end_date: | ||
queryset = queryset.filter(time__lte=end_date) | ||
else: | ||
if start_date: | ||
queryset = queryset.filter(date__gte=start_date) | ||
if end_date: | ||
queryset = queryset.filter(date__lte=end_date) | ||
|
||
datafile_id = request.query_params.get("datafile_id", None) | ||
if datafile_id: | ||
try: | ||
datafile_id = int(datafile_id) | ||
queryset = queryset.filter(serialized_data_file__id=datafile_id) | ||
except ValueError: | ||
pass | ||
|
||
return queryset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from django.conf import settings | ||
|
||
from rest_framework.permissions import BasePermission | ||
|
||
|
||
class LogAPIAccessAllowed(BasePermission): | ||
""" | ||
Return True if the request is from OHLOG_PROJECT_ID. | ||
""" | ||
|
||
def has_permission(self, request, view): | ||
if settings.OHLOG_PROJECT_ID: | ||
if request.auth.id == int(settings.OHLOG_PROJECT_ID): | ||
return True | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ Markdown | |
mock | ||
Pillow # for sorl-thumbnail | ||
pyparsing | ||
python-dateutil | ||
raven | ||
redis | ||
requests | ||
|