Matteo Scandolo | 94042de | 2016-02-29 15:46:55 -0800 | [diff] [blame] | 1 | from django.http import HttpResponse |
| 2 | from django.views.generic import TemplateView, View |
| 3 | from django import template |
| 4 | from django.shortcuts import render |
| 5 | from core.models import * |
| 6 | import json |
| 7 | import os |
| 8 | import time |
| 9 | import tempfile |
| 10 | |
| 11 | |
| 12 | class ServiceGridView(TemplateView): |
| 13 | head_template = r"""{% extends "admin/dashboard/dashboard_base.html" %} |
| 14 | {% load admin_static %} |
| 15 | {% block content %} |
| 16 | """ |
| 17 | |
| 18 | # I hate myself for doing this |
| 19 | script = """ |
| 20 | <script type="text/javascript"> |
| 21 | $(window).ready(function(){ |
| 22 | $('.kind-container').on('click', function(){ |
| 23 | $(this).toggleClass('active') |
| 24 | }); |
| 25 | }) |
| 26 | </script> |
| 27 | """ |
| 28 | |
| 29 | tail_template = r"{% endblock %}" |
| 30 | |
| 31 | def get(self, request, name="root", *args, **kwargs): |
| 32 | head_template = self.head_template |
| 33 | tail_template = self.tail_template |
| 34 | html = self.script |
| 35 | html = html + '<div class="col-xs-12 m-cord">' |
| 36 | |
| 37 | # Select the unique kind of services |
| 38 | for kind in Service.objects.values('kind').distinct(): |
| 39 | |
| 40 | html = html + '<div class="kind-container row">' |
| 41 | html = html + '<div class="col-xs-12"><h2>%s</h2></div>' % (kind["kind"]) |
| 42 | |
| 43 | # for each kind select services |
| 44 | for service in Service.objects.filter(kind=kind["kind"]): |
| 45 | image_url = service.icon_url |
| 46 | if (not image_url): |
Matteo Scandolo | c9f0447 | 2016-02-29 17:12:49 -0800 | [diff] [blame] | 47 | image_url = "/static/mCordServices/service_common.png" |
teone | 8ef088b | 2016-03-03 10:45:45 -0800 | [diff] [blame] | 48 | #if service.view_url.startswith("http"): |
| 49 | # target = 'target="_blank"' |
| 50 | #else: |
| 51 | # target = '' |
| 52 | target = '' |
Matteo Scandolo | 2d48309 | 2016-02-29 16:42:57 -0800 | [diff] [blame] | 53 | |
Matteo Scandolo | 94042de | 2016-02-29 15:46:55 -0800 | [diff] [blame] | 54 | html = html + '<div class="col-xs-4 text-center service-container">' |
Matteo Scandolo | 2d48309 | 2016-02-29 16:42:57 -0800 | [diff] [blame] | 55 | html = html + '<a href="%s" %s>' % (service.view_url, target) |
Matteo Scandolo | 94042de | 2016-02-29 15:46:55 -0800 | [diff] [blame] | 56 | html = html + '<img class="img-responsive" src="%s">' % (image_url) |
| 57 | html = html + "<h4>" + service.name + "</h4>" |
| 58 | html = html + '</a>' |
| 59 | html = html + "</div>" |
| 60 | |
| 61 | html = html + "</div>" |
| 62 | |
| 63 | html = html + "</div>" |
| 64 | t = template.Template(head_template + html + self.tail_template) |
| 65 | |
| 66 | response_kwargs = {} |
| 67 | response_kwargs.setdefault('content_type', self.content_type) |
| 68 | return self.response_class( |
| 69 | request=request, |
| 70 | template=t, |
| 71 | **response_kwargs |
| 72 | ) |