diff options
author | maxbr <maxbr@mi.fu-berlin.de> | 2016-07-29 12:43:43 +0200 |
---|---|---|
committer | Max Breitenfeldt <max.breitenfeldt@gmail.com> | 2016-07-29 11:25:20 +0000 |
commit | 639cd5db77064c275253828780c17ae59551d95c (patch) | |
tree | fca05cf81b3f068ff25c5db46ddd7c27fd1986af /pharos-dashboard/dashboard/views | |
parent | 0f7eae0c112b39c071d41a09ad1caf509b70ac32 (diff) |
import pharos dashboard code
JIRA: RELENG-12
The last commit was missing some JS/CSS dependencies of the site. This
happened because they are in folders that are named 'build' or 'dist'.
This commit adds a bower.json file, that specifies dependencies.
Dependencies can now be installed by running 'bower install' in the
dashboard/static folder.
Change-Id: I054f319c66771f767e97711cb678d79d3bd6bee4
Signed-off-by: maxbr <maxbr@mi.fu-berlin.de>
Diffstat (limited to 'pharos-dashboard/dashboard/views')
-rw-r--r-- | pharos-dashboard/dashboard/views/booking.py | 69 | ||||
-rw-r--r-- | pharos-dashboard/dashboard/views/registration.py | 16 | ||||
-rw-r--r-- | pharos-dashboard/dashboard/views/table_views.py | 62 |
3 files changed, 147 insertions, 0 deletions
diff --git a/pharos-dashboard/dashboard/views/booking.py b/pharos-dashboard/dashboard/views/booking.py new file mode 100644 index 0000000..1499edb --- /dev/null +++ b/pharos-dashboard/dashboard/views/booking.py @@ -0,0 +1,69 @@ +from dashboard.forms.booking_form import BookingForm +from dashboard.models import Resource, Booking +from dashboard.views.registration import BookingUserTestMixin +from django.contrib import messages +from django.contrib.auth.decorators import login_required +from django.http import Http404, JsonResponse +from django.shortcuts import get_object_or_404 +from django.utils.decorators import method_decorator +from django.views.generic import FormView, View + + +@method_decorator(login_required, name='dispatch') +class BookingCalendarView(BookingUserTestMixin, FormView): + template_name = "dashboard/booking_calendar.html" + form_class = BookingForm + + # set instance variables + def dispatch(self, request, *args, **kwargs): + self.foo = request.GET.get('foo', False) + self.resource = get_object_or_404(Resource, resource_id=self.kwargs['resource_id']) + return super(BookingCalendarView, self).dispatch(request, *args, **kwargs) + + def form_valid(self, form): + self.success_url = self.request.path + booking = None + # change existing booking + if form.cleaned_data['booking_id'] is not None: + booking = get_object_or_404(Booking, booking_id=form.cleaned_data['booking_id']) + # create new booking + else: + booking = Booking() + booking.resource = self.resource + booking.user = self.request.user + booking.start_date_time = form.cleaned_data['start_date_time'] + booking.end_date_time = form.cleaned_data['end_date_time'] + booking.purpose = form.cleaned_data['purpose'] + try: + booking.save() + except ValueError: + messages.add_message(self.request, messages.ERROR, + 'This booking overlaps with another booking') + return super(BookingCalendarView, self).form_invalid(form) + messages.add_message(self.request, messages.SUCCESS, + 'Booking saved') + return super(BookingCalendarView, self).form_valid(form) + + def get_context_data(self, **kwargs): + title = 'Booking: ' + self.resource.name + context = super(BookingCalendarView, self).get_context_data(**kwargs) + context.update({'title': title, 'resource': self.resource}) + return context + + +@method_decorator(login_required, name='dispatch') +class ResourceBookingsView(BookingUserTestMixin, View): + def get(self, request, *args, **kwargs): + resource = Resource.objects.get(resource_id=self.kwargs['resource_id']) + bookings = resource.booking_set.get_queryset().values( + 'booking_id', 'user__username', 'user__email', + 'start_date_time', 'end_date_time', 'purpose') + return JsonResponse({'bookings': list(bookings)}) + + +@method_decorator(login_required, name='dispatch') +class DeleteBookingView(BookingUserTestMixin, View): + def post(self, request, *args, **kwargs): + booking = get_object_or_404(Booking, booking_id=self.kwargs['booking_id']) + booking.delete() + return JsonResponse({True: self.kwargs['booking_id']}) diff --git a/pharos-dashboard/dashboard/views/registration.py b/pharos-dashboard/dashboard/views/registration.py new file mode 100644 index 0000000..516fb29 --- /dev/null +++ b/pharos-dashboard/dashboard/views/registration.py @@ -0,0 +1,16 @@ +from django.contrib.auth.mixins import UserPassesTestMixin + + +class BookingUserTestMixin(UserPassesTestMixin): + # Test if a user has permission to book this Pod + def test_func(self): + user = self.request.user + # Check if User is troubleshooter / admin + if user.has_perm(('dashboard.add_booking')): + return True + # Check if User owns this resource + user_resources = user.userresource_set.get_queryset() + for user_resource in user_resources: + if user_resource.resource_id == self.resource.resource_id: + return True + return False diff --git a/pharos-dashboard/dashboard/views/table_views.py b/pharos-dashboard/dashboard/views/table_views.py new file mode 100644 index 0000000..4404234 --- /dev/null +++ b/pharos-dashboard/dashboard/views/table_views.py @@ -0,0 +1,62 @@ +import dashboard.jenkins.jenkins_util as jenkins_util + +import dashboard.jenkins.jenkins_adapter as jenkins +from dashboard.models import Resource, Booking +from django.utils import timezone +from django.views.generic import TemplateView + + +class JenkinsSlavesView(TemplateView): + template_name = "tables/jenkins_slaves.html" + + def get_context_data(self, **kwargs): + slaves = jenkins.get_all_slaves() + for slave in slaves: + jenkins_util.parse_slave_data(slave, slave) + + context = super(JenkinsSlavesView, self).get_context_data(**kwargs) + context.update({'title': "Jenkins Slaves", 'slaves': slaves}) + return context + + +class CIPodsView(TemplateView): + template_name = "tables/ci_pods.html" + + def get_context_data(self, **kwargs): + resources = Resource.objects.filter().values() # get resources as a set of dicts + ci_pods = [] + for resource in resources: + if not jenkins.is_ci_slave(resource['slavename']): + continue + ci_slave = jenkins.get_slave(resource['slavename']) + jenkins_util.parse_slave_data(resource, ci_slave) + ci_pods.append(resource) + + context = super(CIPodsView, self).get_context_data(**kwargs) + context.update({'title': "CI Pods", 'ci_pods': ci_pods}) + return context + + +class DevelopmentPodsView(TemplateView): + template_name = "tables/dev_pods.html" + + def get_context_data(self, **kwargs): + resources = Resource.objects.filter().values() # get resources as a set of dicts + dev_pods = [] + + current_bookings = Booking.objects.filter(start_date_time__lte=timezone.now()) + current_bookings = current_bookings.filter(end_date_time__gt=timezone.now()) + + for resource in resources: + if not jenkins.is_dev_pod(resource['slavename']): + continue + dev_pod = jenkins.get_slave(resource['slavename']) + jenkins_util.parse_slave_data(resource, dev_pod) + for booking in current_bookings: + if booking.resource.slavename == resource['slavename']: + resource['current_booking'] = booking + dev_pods.append(resource) + + context = super(DevelopmentPodsView, self).get_context_data(**kwargs) + context.update({'title': "Development Pods", 'dev_pods': dev_pods}) + return context |