Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolas committed Dec 4, 2023
1 parent c48aec6 commit a67586c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
29 changes: 29 additions & 0 deletions econplayground/assignment/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from graphviz import Graph

from econplayground.assignment.models import AssessmentRule


Expand Down Expand Up @@ -29,3 +31,30 @@ def make_rules(request: object, question: object) -> None:
media_unfulfilled=request.POST.get(
'rule_media_unfulfilled_{}'.format(i))
)


def render_assignment_graph(root: object) -> str:
"""
Given an assignment's root node, render this to an SVG string
with graphviz.
"""
graph = Graph(format='svg')
# graph.graph_attr['rankdir'] = 'LR'
steps = root.get('children')

for x in range(len(steps)):
step = steps[x]
print(step)

step_label = 'Step {}'.format(step.get('id'))
if x > 0:
prev_step = steps[x-1]
prev_step_label = 'Step {}'.format(prev_step.get('id'))
graph.edge(prev_step_label, step_label)

for substep in step.get('children'):
substep_label = 'Sub-step {}'.format(str(substep.get('id')))
graph.edge(step_label, substep_label)

# Output to string
return graph.pipe().decode('utf-8')
8 changes: 7 additions & 1 deletion econplayground/assignment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
CreateView, UpdateView, DeleteView
)
from django.shortcuts import get_object_or_404
from econplayground.assignment.utils import make_rules
from econplayground.assignment.utils import make_rules, render_assignment_graph
from econplayground.main.views import EnsureCsrfCookieMixin
from econplayground.main.utils import user_is_instructor
from econplayground.assignment.models import (
Expand Down Expand Up @@ -124,6 +124,11 @@ class AssignmentDetailStudentView(LoginRequiredMixin, DetailView):
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)

root = self.object.get_root()
print(root.numchild)
bulk_tree = Step.dump_bulk(parent=root)
root = bulk_tree[0]

score_path = None
try:
score_path = ScorePath.objects.get(
Expand All @@ -139,6 +144,7 @@ def get_context_data(self, **kwargs):

ctx.update({
'steps': steps,
'graph': render_assignment_graph(root),
})
return ctx

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ <h1>{{ object.title }}</h1>
Welcome to assignment {{object.pk}}!
</div>

<div class="ep-assignment-graph">
{{ graph | safe }}
</div>

Your path:

<div class="ep-score-path">
<div class="ep-score-path w-50">
{% for step in steps %}
<div class="assignment-step">
{{forloop.counter}}.
<a href="{% url 'step_detail' object.pk step.pk %}">
Step {{step.pk}}
</a>
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,5 @@ pyparsing==3.1.1
django-s3sign==0.4.0

django-treebeard==4.7

graphviz==0.20.1

0 comments on commit a67586c

Please sign in to comment.