views.py 7.15 KB
Newer Older
Janez K's avatar
Janez K committed
1 2 3 4 5 6 7 8 9
# helperji, context stvari
from django.shortcuts import render, get_object_or_404, redirect
from django.http import Http404, HttpResponse
from django.contrib import messages

# auth fore
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login, logout

10
from workflows.models import Workflow, Connection
Janez K's avatar
Janez K committed
11

Janez K's avatar
Janez K committed
12 13
from streams.models import Stream

Janez K's avatar
Janez K committed
14 15 16 17 18 19 20 21
#settings
from mothra.settings import DEBUG, PROJECT_FOLDER

#ostalo
from django.template.loader import get_template
from django.template import TemplateDoesNotExist
import os

22 23 24 25
import json

from website.forms import ImportForm

Janez K's avatar
Janez K committed
26 27
def index(request):
    return render(request, 'website/index.html')
28

Janez K's avatar
Janez K committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
def reset_stream(request,stream_id):
    s = get_object_or_404(Stream, pk=stream_id)
    if s.user != request.user:
        raise Http404
    s.reset()
    s.save()
    return redirect(s.get_absolute_url())

def deactivate_stream(request,stream_id):
    s = get_object_or_404(Stream, pk=stream_id)
    if s.user != request.user:
        raise Http404
    s.active = False
    s.save()
    return redirect(s.get_absolute_url())

def activate_stream(request,stream_id):
    s = get_object_or_404(Stream, pk=stream_id)
    if s.user != request.user:
        raise Http404
    s.active = True
    s.save()
    return redirect(s.get_absolute_url())

def start_stream(request,workflow_id):
    w = get_object_or_404(Workflow, pk=workflow_id)
    if w.user != request.user:
        raise Http404
    s = Stream(workflow=w,user=request.user,active=True)
    s.save()
    return redirect(s.get_absolute_url())

Janez K's avatar
Janez K committed
61 62 63 64 65 66
def stream(request,stream_id):
    stream = get_object_or_404(Stream,pk=stream_id)
    if stream.workflow.user != request.user:
        raise Http404
    return render(request, 'website/stream.html', {'stream':stream})

Janez K's avatar
Janez K committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
def workflow_information(request,workflow_id):
    w = Workflow.objects.get(pk=workflow_id)
    if not w.public:
        raise Http404
    min_x = 10000
    min_y = 10000
    max_x = 0
    max_y = 0
    max_width = 300
    max_height = 200
    normalized_values = {}
    w.normalized_widgets = w.widgets.all()
    w.unique_connections = []
    w.pairs = []
    for widget in w.normalized_widgets:
        if widget.x > max_x:
            max_x = widget.x
        if widget.x < min_x:
            min_x = widget.x
        if widget.y > max_y:
            max_y = widget.y
        if widget.y < min_y:
            min_y = widget.y
    for widget in w.normalized_widgets:
        x = (widget.x - min_x)*1.0
        y = (widget.y - min_y)*1.0
        normalized_max_x = max_x-min_x
        if x == 0:
            x = 1
        if y == 0:
            y = 1
        if normalized_max_x == 0:
            normalized_max_x = x*2
        normalized_max_y = max_y-min_y
        if normalized_max_y == 0:
            normalized_max_y = y*2
        widget.norm_x = (x/normalized_max_x)*max_width
        widget.norm_y = (y/normalized_max_y)*max_height
        normalized_values[widget.id]=(widget.norm_x,widget.norm_y)
Janez K's avatar
Janez K committed
106
    for c in w.connections.select_related("output","input").defer("output__value","input__value").all():
107 108
        if not (c.output.widget_id,c.input.widget_id) in w.pairs:
            w.pairs.append((c.output.widget_id,c.input.widget_id))
Janez K's avatar
Janez K committed
109 110 111 112 113 114 115
    for pair in w.pairs:
        conn = {}
        conn['x1'] = normalized_values[pair[0]][0]+40
        conn['y1'] = normalized_values[pair[0]][1]+15
        conn['x2'] = normalized_values[pair[1]][0]-10
        conn['y2'] = normalized_values[pair[1]][1]+15
        w.unique_connections.append(conn)
Janez K's avatar
Janez K committed
116
    return render(request, 'website/existing.html', {'workflows':[w,]})
117

118
@login_required
119
def your_workflows(request):
Janez K's avatar
bugfix  
Janez K committed
120
    return render(request, 'website/yourworkflows.html', {'workflows':request.user.workflows.filter(widget=None)})
121

122
@login_required
123 124 125 126 127 128 129
def make_public(request,workflow_id):
    workflow = get_object_or_404(Workflow,pk=workflow_id)
    if request.user == workflow.user:
        workflow.public = True
        workflow.save()
    return redirect('your workflows')

130
@login_required
131 132 133 134 135 136 137
def make_private(request,workflow_id):
    workflow = get_object_or_404(Workflow,pk=workflow_id)
    if request.user == workflow.user:
        workflow.public = False
        workflow.save()
    return redirect('your workflows')

Janez K's avatar
Janez K committed
138 139 140 141 142 143 144 145 146
def workflows(request):
    wflows = Workflow.objects.filter(public=True)
    min_x = 10000
    min_y = 10000
    max_x = 0
    max_y = 0
    max_width = 300
    max_height = 200
    normalized_values = {}
147
    cons = list(Connection.objects.select_related("output","input").defer("output__value","input__value").filter(workflow__public=True))
Janez K's avatar
Janez K committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    for w in wflows:
        w.normalized_widgets = w.widgets.all()
        w.unique_connections = []
        w.pairs = []
        for widget in w.normalized_widgets:
            if widget.x > max_x:
                max_x = widget.x
            if widget.x < min_x:
                min_x = widget.x
            if widget.y > max_y:
                max_y = widget.y
            if widget.y < min_y:
                min_y = widget.y
        for widget in w.normalized_widgets:
            x = (widget.x - min_x)*1.0
            y = (widget.y - min_y)*1.0
            normalized_max_x = max_x-min_x
            if x == 0:
                x = 1
            if y == 0:
                y = 1
            if normalized_max_x == 0:
                normalized_max_x = x*2
            normalized_max_y = max_y-min_y
            if normalized_max_y == 0:
                normalized_max_y = y*2
            widget.norm_x = (x/normalized_max_x)*max_width
            widget.norm_y = (y/normalized_max_y)*max_height
            normalized_values[widget.id]=(widget.norm_x,widget.norm_y)
177 178 179 180
        connection_list = filter(lambda e: e.workflow_id == w.id, cons)
        for c in connection_list:
            if not (c.output.widget_id,c.input.widget_id) in w.pairs:
                w.pairs.append((c.output.widget_id,c.input.widget_id))
Janez K's avatar
Janez K committed
181 182 183 184 185 186 187
        for pair in w.pairs:
            conn = {}
            conn['x1'] = normalized_values[pair[0]][0]+40
            conn['y1'] = normalized_values[pair[0]][1]+15
            conn['x2'] = normalized_values[pair[1]][0]-10
            conn['y2'] = normalized_values[pair[1]][1]+15
            w.unique_connections.append(conn)
Janez K's avatar
Janez K committed
188
    return render(request, 'website/existing.html', {'workflows':wflows})
Janez K's avatar
Janez K committed
189 190 191

@login_required
def editor(request):
Janez K's avatar
Janez K committed
192 193 194 195 196
    if request.GET.get('tutorial','0')=='1':
        tutorial = True
    else:
        tutorial = False
    return render(request, 'website/editor.html', {'tutorial':tutorial})
Janez K's avatar
Janez K committed
197

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
@login_required
def export_workflow(request,workflow_id):
    w = get_object_or_404(Workflow, pk=workflow_id)
    exported_data = json.dumps(w.export(),indent=2)
    return render(request,'website/export_workflow.html',{"workflow":w,'exported_data':exported_data})

@login_required
def import_workflow(request):
    if request.method == "POST":
        form = ImportForm(request.POST)
        if form.is_valid():
            new_workflow = Workflow()
            new_workflow.user = request.user
            new_workflow.import_from_json(json.loads(form.cleaned_data['data']),{},{})
            return redirect(new_workflow.get_absolute_url())
    else:
        form = ImportForm()
    return render(request,'website/import_workflow.html',{"form":form})