Skip to content

Commit

Permalink
Consider all hierarchies when compiling questions.
Browse files Browse the repository at this point in the history
  • Loading branch information
sdreher committed May 20, 2013
1 parent 854b907 commit ed58364
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 32 deletions.
84 changes: 84 additions & 0 deletions responseblock/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# flake8: noqa
# encoding: utf-8
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models

class Migration(SchemaMigration):

def forwards(self, orm):

# Adding model 'Response'
db.create_table('responseblock_response', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('question', self.gf('django.db.models.fields.related.ForeignKey')(related_name='question', to=orm['quizblock.Question'])),
))
db.send_create_signal('responseblock', ['Response'])


def backwards(self, orm):

# Deleting model 'Response'
db.delete_table('responseblock_response')


models = {
'contenttypes.contenttype': {
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
},
'pagetree.hierarchy': {
'Meta': {'object_name': 'Hierarchy'},
'base_url': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '256'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '256'})
},
'pagetree.pageblock': {
'Meta': {'ordering': "('section', 'ordinality')", 'object_name': 'PageBlock'},
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
'css_extra': ('django.db.models.fields.CharField', [], {'max_length': '256', 'null': 'True', 'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'label': ('django.db.models.fields.CharField', [], {'max_length': '256', 'null': 'True', 'blank': 'True'}),
'object_id': ('django.db.models.fields.PositiveIntegerField', [], {}),
'ordinality': ('django.db.models.fields.PositiveIntegerField', [], {'default': '1'}),
'section': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['pagetree.Section']"})
},
'pagetree.section': {
'Meta': {'object_name': 'Section'},
'depth': ('django.db.models.fields.PositiveIntegerField', [], {}),
'hierarchy': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['pagetree.Hierarchy']"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'label': ('django.db.models.fields.CharField', [], {'max_length': '256'}),
'numchild': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
'path': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'}),
'slug': ('django.db.models.fields.SlugField', [], {'max_length': '50', 'db_index': 'True'})
},
'quizblock.question': {
'Meta': {'ordering': "('_order',)", 'object_name': 'Question'},
'_order': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
'explanation': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'intro_text': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
'question_type': ('django.db.models.fields.CharField', [], {'max_length': '256'}),
'quiz': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['quizblock.Quiz']"}),
'text': ('django.db.models.fields.TextField', [], {})
},
'quizblock.quiz': {
'Meta': {'object_name': 'Quiz'},
'allow_redo': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'rhetorical': ('django.db.models.fields.BooleanField', [], {'default': 'False'})
},
'responseblock.response': {
'Meta': {'object_name': 'Response'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'question': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'question'", 'to': "orm['quizblock.Question']"})
}
}

complete_apps = ['responseblock']
Empty file.
58 changes: 27 additions & 31 deletions responseblock/models.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
from django.db import models
from pagetree.models import PageBlock, Hierarchy, Section
from django.contrib.auth.models import User
from pagetree.models import PageBlock, Hierarchy
from django.contrib.contenttypes import generic
from django import forms
from datetime import datetime
from django.core.urlresolvers import reverse
from quizblock.models import Quiz,Question
from quizblock.models import Question


def all_questions():
# TODO: this will break in sites with more than one hierarchy
h = Hierarchy.objects.all()[0]
quizzes = []
for s in h.get_root().get_descendants():
for p in s.pageblock_set.all():
if hasattr(p.block(),'needs_submit') and p.block().needs_submit():
quizzes.append(p)
for h in Hierarchy.objects.all():
for s in h.get_root().get_descendants():
for p in s.pageblock_set.all():
if (hasattr(p.block(), 'needs_submit') and
p.block().needs_submit()):
quizzes.append(p)

questions = []
for qz in quizzes:
for q in qz.block().question_set.all():
if hasattr(q,'quiz'):
if hasattr(q, 'quiz'):
yield q


class Response(models.Model):
pageblocks = generic.GenericRelation(PageBlock)
question = models.ForeignKey(Question,related_name="question")
question = models.ForeignKey(Question, related_name="question")
template_file = "responseblock/responseblock.html"

display_name = "Response"
Expand All @@ -38,41 +36,39 @@ def __unicode__(self):
return unicode(self.pageblock())

def edit_label(self):
return "%s: %s" % (self.display_name,str(self.question))
return "%s: %s" % (self.display_name, str(self.question))

def edit_form(self):
question_choices = [
(q.id,"%s%s/%s" % (q.quiz.pageblock().section.get_absolute_url(),
q.quiz.pageblock().label,
q.text)) for q in all_questions()
]
question_choices = [(q.id, "%s%s/%s" %
(q.quiz.pageblock().section.get_absolute_url(),
q.quiz.pageblock().label,
q.text)) for q in all_questions()]

class EditForm(forms.Form):
question = forms.ChoiceField(label="Select Question",
choices=question_choices,
initial=self.question.id,
)
initial=self.question.id)
return EditForm()

@classmethod
def add_form(self):
question_choices = [
(q.id,"%s%s/%s" % (q.quiz.pageblock().section.get_absolute_url(),
q.quiz.pageblock().label,
q.text)) for q in all_questions()
]
question_choices = [(q.id, "%s%s/%s" %
(q.quiz.pageblock().section.get_absolute_url(),
q.quiz.pageblock().label,
q.text)) for q in all_questions()]

class AddForm(forms.Form):
question = forms.ChoiceField(label="Select Question",
choices=question_choices,
)
return AddForm()

@classmethod
def create(self,request):
question = Question.objects.get(id=request.POST.get('question',''))
def create(self, request):
question = Question.objects.get(id=request.POST.get('question', ''))
return Response.objects.create(question=question)

def edit(self,vals,files):
question = Question.objects.get(id=vals.get('question',''))
def edit(self, vals, files):
question = Question.objects.get(id=vals.get('question', ''))
self.question = question
self.save()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

setup(
name="django-responseblock",
version="0.1.4",
version="0.1.5",
author="Anders Pearson",
author_email="anders@columbia.edu",
url="",
Expand Down

0 comments on commit ed58364

Please sign in to comment.