Esempio n. 1
0
def edit(request, gid=""):
    g = get_object_or_404(Graphs, pk=gid)
    s = get_object_or_404(Studies, pk=gid)
    pids = ', '.join([p.pid for p in s.participants_set.all()])

    f = {}

    if request.method == 'POST':
        f['graph'] = GraphForm(request.POST, instance=g, prefix="graph")
        f['study'] = StudyForm(request.POST,
                               instance=s,
                               prefix="study",
                               initial={'pids': pids})
        f['graph'].fields["json_input"].widget = HiddenInput()

        if f['graph'].is_valid() and (
                not f['graph'].cleaned_data["study_active"]
                or f['study'].is_valid()):
            # woo all good, save the graph and build its concepts
            g = f['graph'].save()
            g.build(f['graph'].cleaned_data["json_data"])

            # insert study data if applicable
            if f['graph'].cleaned_data["study_active"]:
                s = f['study'].save()

                if 'pids' in f['study'].changed_data:
                    # you asked for it! delete all participants
                    Participants.objects.filter(study=s).delete()
                    Spectators.objects.filter(study=s).delete()
                    buildPIDs(s, f['study'].cleaned_data["pids"])
            else:
                # delete all participants
                Participants.objects.filter(study=s).delete()
                Spectators.objects.filter(study=s).delete()

            return HttpResponseRedirect(
                reverse("maps:display", kwargs={"gid": g.pk}))
        f['error'] = f['graph'].errors.get('json_data')
    else:
        # prepare content; most data is provided by models
        gi = {'json_data': str(g)}
        f['graph'] = GraphForm(instance=g, prefix="graph", initial=gi)
        f['graph'].fields["json_input"].widget = HiddenInput()

        si = {'pids': pids}
        f['study'] = StudyForm(instance=s, prefix="study", initial=si)
        f['study'].fields[
            'pids'].help_text = "<strong>Click above to edit. Note that editing the participant list will cause all participants to be deleted and re-created on the server.</strong><br />%s" % f[
                'study'].fields['pids'].help_text

    return render(request, "maps-form.html", {'forms': f, 'gid': gid})
Esempio n. 2
0
def new_graph(request):
    f = {}
    if request.method == 'POST':
        # form submission
        f['graph'] = GraphForm(request.POST, prefix="graph")
        f['study'] = StudyForm(request.POST, prefix="study")

        if f['graph'].is_valid() and (
                not f['graph'].cleaned_data["study_active"]
                or f['study'].is_valid()):
            # woo all good
            g = f['graph'].save(commit=False)

            # hash graph's key
            g.secret = make_password(f['graph'].cleaned_data["secret"])
            g.save()

            # build the graph's concepts
            g.build(f['graph'].cleaned_data["json_data"])

            # insert study data if applicable
            if f['graph'].cleaned_data["study_active"]:
                s = f['study'].save(commit=False)
                s.graph = g
                s.save()
                buildPIDs(s, f['study'].cleaned_data["pids"])
            else:
                # insert blank study
                Studies(graph=g).save()

            # all saved, provide edit access and forward to map
            setEdit(request, g.pk)
            return HttpResponseRedirect(
                reverse("maps:display", kwargs={"gid": g.pk}))
        f['error'] = f['graph'].errors.get('json_data')
    else:
        secrets = {
            'secret': generateSecret(),
            'lti_key': generateSecret(8),
            'lti_secret': generateSecret()
        }
        f['graph'] = GraphForm(initial=secrets, prefix="graph")
        f['study'] = StudyForm(prefix="study")

    return render(request, "maps-form.html", {'forms': f})
Esempio n. 3
0
def edit(request, gid=""):
    g = get_object_or_404(Graphs, pk=gid)
    s = get_object_or_404(Studies, pk=gid)
    pids = ', '.join([p.pid for p in s.participants_set.all()])

    f = {}

    if request.method == 'POST':
        f['graph'] = GraphForm(request.POST, instance=g, prefix="graph")
        f['study'] = StudyForm(request.POST, instance=s, prefix="study", initial={'pids':pids})
        f['graph'].fields["json_input"].widget = HiddenInput()

        if f['graph'].is_valid() and (not f['graph'].cleaned_data["study_active"] or f['study'].is_valid()):
            # woo all good, save the graph and build its concepts
            g = f['graph'].save()
            g.build(f['graph'].cleaned_data["json_data"])

            # insert study data if applicable
            if f['graph'].cleaned_data["study_active"]:
                s = f['study'].save()

                if 'pids' in f['study'].changed_data:
                    # you asked for it! delete all participants
                    Participants.objects.filter(study=s).delete()
                    Spectators.objects.filter(study=s).delete()
                    buildPIDs(s, f['study'].cleaned_data["pids"])
            else:
                # delete all participants
                Participants.objects.filter(study=s).delete()
                Spectators.objects.filter(study=s).delete()

            return HttpResponseRedirect(reverse("maps:display", kwargs={"gid":g.pk}))
        f['error'] = f['graph'].errors.get('json_data')
    else:
        # prepare content; most data is provided by models
        gi = {'json_data':str(g)}
        f['graph'] = GraphForm(instance=g, prefix="graph", initial=gi)
        f['graph'].fields["json_input"].widget = HiddenInput()

        si = {'pids':pids}
        f['study'] = StudyForm(instance=s, prefix="study", initial=si)
        f['study'].fields['pids'].help_text = "<strong>Click above to edit. Note that editing the participant list will cause all participants to be deleted and re-created on the server.</strong><br />%s" % f['study'].fields['pids'].help_text;

    return render(request, "maps-form.html", {'forms':f, 'gid':gid})
Esempio n. 4
0
def new_graph(request):
    f = {}
    if request.method == 'POST':
        # form submission
        f['graph'] = GraphForm(request.POST, prefix="graph")
        f['study'] = StudyForm(request.POST, prefix="study")

        if f['graph'].is_valid() and (not f['graph'].cleaned_data["study_active"] or f['study'].is_valid()):
            # woo all good
            g = f['graph'].save(commit=False)

            # hash graph's key
            g.secret = make_password(f['graph'].cleaned_data["secret"]);
            g.save()

            # build the graph's concepts
            g.build(f['graph'].cleaned_data["json_data"])

            # insert study data if applicable
            if f['graph'].cleaned_data["study_active"]:
                s = f['study'].save(commit=False)
                s.graph = g
                s.save()
                buildPIDs(s, f['study'].cleaned_data["pids"])
            else:
                # insert blank study
                Studies(graph=g).save()

            # all saved, provide edit access and forward to map
            setEdit(request, g.pk)
            return HttpResponseRedirect(reverse("maps:display", kwargs={"gid":g.pk}))
        f['error'] = f['graph'].errors.get('json_data')
    else:
        secrets = {'secret':generateSecret(),
                   'lti_key':generateSecret(8),
                   'lti_secret':generateSecret()}
        f['graph'] = GraphForm(initial=secrets, prefix="graph")
        f['study'] = StudyForm(prefix="study")

    return render(request, "maps-form.html", {'forms':f})