Commit f157eb56 by Kálmán Viktor

request: hook for request

parent 94ab65de
...@@ -556,3 +556,4 @@ ADMIN_ENABLED = False ...@@ -556,3 +556,4 @@ ADMIN_ENABLED = False
BLACKLIST_PASSWORD = get_env_variable("BLACKLIST_PASSWORD", "") BLACKLIST_PASSWORD = get_env_variable("BLACKLIST_PASSWORD", "")
BLACKLIST_HOOK_URL = get_env_variable("BLACKLIST_HOOK_URL", "") BLACKLIST_HOOK_URL = get_env_variable("BLACKLIST_HOOK_URL", "")
REQUEST_HOOK_URL = get_env_variable("REQUEST_HOOK_URL", "")
...@@ -16,8 +16,8 @@ ...@@ -16,8 +16,8 @@
# with CIRCLE. If not, see <http://www.gnu.org/licenses/>. # with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
from django.forms import ( from django.forms import (
ModelForm, ModelChoiceField, ChoiceField, Form, CharField, RadioSelect, ModelForm, ModelChoiceField, ChoiceField, Form, CharField, RadioSelect,
Textarea,
) )
from django import forms
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.template import RequestContext from django.template import RequestContext
from django.template.loader import render_to_string from django.template.loader import render_to_string
...@@ -74,19 +74,19 @@ class TemplateRequestForm(InitialFromFileMixin, Form): ...@@ -74,19 +74,19 @@ class TemplateRequestForm(InitialFromFileMixin, Form):
label=_("Template share")) label=_("Template share"))
level = ChoiceField(TemplateAccessAction.LEVELS, widget=RadioSelect, level = ChoiceField(TemplateAccessAction.LEVELS, widget=RadioSelect,
initial=TemplateAccessAction.LEVELS.user) initial=TemplateAccessAction.LEVELS.user)
message = CharField(widget=forms.Textarea, label=_("Message")) message = CharField(widget=Textarea, label=_("Message"))
initial_template = "request/initials/template.html" initial_template = "request/initials/template.html"
class LeaseRequestForm(InitialFromFileMixin, Form): class LeaseRequestForm(InitialFromFileMixin, Form):
lease = ModelChoiceField(LeaseType.objects.all(), label=_("Lease")) lease = ModelChoiceField(LeaseType.objects.all(), label=_("Lease"))
message = CharField(widget=forms.Textarea) message = CharField(widget=Textarea)
initial_template = "request/initials/lease.html" initial_template = "request/initials/lease.html"
class ResourceRequestForm(InitialFromFileMixin, VmResourcesForm): class ResourceRequestForm(InitialFromFileMixin, VmResourcesForm):
message = CharField(widget=forms.Textarea) message = CharField(widget=Textarea)
initial_template = "request/initials/resources.html" initial_template = "request/initials/resources.html"
...@@ -14,10 +14,14 @@ ...@@ -14,10 +14,14 @@
# #
# You should have received a copy of the GNU General Public License along # You should have received a copy of the GNU General Public License along
# with CIRCLE. If not, see <http://www.gnu.org/licenses/>. # with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
import json
import logging
from django.db.models import ( from django.db.models import (
Model, CharField, IntegerField, TextField, ForeignKey, ManyToManyField, Model, CharField, IntegerField, TextField, ForeignKey, ManyToManyField,
) )
from django.db.models.signals import post_save from django.db.models.signals import post_save
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User from django.contrib.auth.models import User
...@@ -27,11 +31,14 @@ from django.utils.translation import ( ...@@ -27,11 +31,14 @@ from django.utils.translation import (
) )
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
import requests
from model_utils.models import TimeStampedModel from model_utils.models import TimeStampedModel
from model_utils import Choices from model_utils import Choices
from vm.models import Instance, InstanceTemplate, Lease from vm.models import Instance, InstanceTemplate, Lease
logger = logging.getLogger(__name__)
class RequestAction(Model): class RequestAction(Model):
...@@ -236,7 +243,7 @@ class TemplateAccessAction(RequestAction): ...@@ -236,7 +243,7 @@ class TemplateAccessAction(RequestAction):
) % ", ".join([x.name for x in self.template_type.templates.all()]) ) % ", ".join([x.name for x in self.template_type.templates.all()])
def send_notification_to_superusers(sender, instance, created, **kwargs): def send_notifications(sender, instance, created, **kwargs):
if not created: if not created:
return return
...@@ -247,7 +254,7 @@ def send_notification_to_superusers(sender, instance, created, **kwargs): ...@@ -247,7 +254,7 @@ def send_notification_to_superusers(sender, instance, created, **kwargs):
'display_name': instance.user.profile.get_display_name(), 'display_name': instance.user.profile.get_display_name(),
'user_url': instance.user.profile.get_absolute_url(), 'user_url': instance.user.profile.get_absolute_url(),
'request_url': instance.get_absolute_url(), 'request_url': instance.get_absolute_url(),
'request_type': instance.get_readable_type() 'request_type': u"%s" % instance.get_readable_type()
} }
for u in User.objects.filter(is_superuser=True): for u in User.objects.filter(is_superuser=True):
...@@ -261,5 +268,21 @@ def send_notification_to_superusers(sender, instance, created, **kwargs): ...@@ -261,5 +268,21 @@ def send_notification_to_superusers(sender, instance, created, **kwargs):
'<a href="%(request_url)s">link</a>.'), context '<a href="%(request_url)s">link</a>.'), context
) )
if settings.REQUEST_HOOK_URL:
post_save.connect(send_notification_to_superusers, sender=Request) context.update({
'object_kind': "request",
'site_url': settings.DJANGO_URL,
})
try:
r = requests.post(settings.REQUEST_HOOK_URL, timeout=3,
data=json.dumps(context, indent=2))
r.raise_for_status()
except requests.RequestException as e:
logger.warning("Error in HTTP POST: %s. url: %s params: %s",
str(e), settings.REQUEST_HOOK_URL, context)
else:
logger.info("Successful HTTP POST. url: %s params: %s",
settings.REQUEST_HOOK_URL, context)
post_save.connect(send_notifications, sender=Request)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment