oncall-engine/engine/common/api_helpers/utils.py
Michael Derynck 6b40f95033 World, meet OnCall!
Co-authored-by: Eve832 <eve.meelan@grafana.com>
    Co-authored-by: Francisco Montes de Oca <nevermind89x@gmail.com>
    Co-authored-by: Ildar Iskhakov <ildar.iskhakov@grafana.com>
    Co-authored-by: Innokentii Konstantinov <innokenty.konstantinov@grafana.com>
    Co-authored-by: Julia <ferril.darkdiver@gmail.com>
    Co-authored-by: maskin25 <kengurek@gmail.com>
    Co-authored-by: Matias Bordese <mbordese@gmail.com>
    Co-authored-by: Matvey Kukuy <motakuk@gmail.com>
    Co-authored-by: Michael Derynck <michael.derynck@grafana.com>
    Co-authored-by: Richard Hartmann <richih@richih.org>
    Co-authored-by: Robby Milo <robbymilo@fastmail.com>
    Co-authored-by: Timur Olzhabayev <timur.olzhabayev@grafana.com>
    Co-authored-by: Vadim Stepanov <vadimkerr@gmail.com>
    Co-authored-by: Yulia Shanyrova <yulia.shanyrova@grafana.com>
2022-06-03 08:09:47 -06:00

52 lines
1.6 KiB
Python

import requests
from django.conf import settings
from icalendar import Calendar
from rest_framework import serializers
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())
"""
def set_context(self, serializer_field):
self.organization = serializer_field.context["request"].auth.organization
def __call__(self):
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.
"""
def set_context(self, serializer_field):
self.team = serializer_field.context["request"].user.current_team
def __call__(self):
return self.team
def __repr__(self):
return "%s()" % self.__class__.__name__
def validate_ical_url(url):
if url:
if settings.BASE_URL in url:
raise serializers.ValidationError("Potential self-reference")
try:
ical_file = requests.get(url).text
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