Skip to content

Commit

Permalink
Enable triggering of archiving mapreduce job from the UI.
Browse files Browse the repository at this point in the history
  • Loading branch information
hellojwilde committed Sep 16, 2012
1 parent df98009 commit ed4ceda
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 7 deletions.
38 changes: 36 additions & 2 deletions handlers/quiz/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.ext.db import djangoforms
from google.appengine.ext.mapreduce.control import start_map
from django import forms

import os
Expand Down Expand Up @@ -74,7 +75,8 @@ def get(self, id):
self.values['prompt'] = \
'Are you absolutely sure that you would like to delete the ' \
'quiz, "%s," forever?' % self.quiz_entity.title
self.values['back'] = os.environ['HTTP_REFERER']
self.values['back'] = os.environ.get('HTTP_REFERER',
links.Quiz.edit(int(id)))
self.output('confirm.html')

def post(self, id):
Expand All @@ -83,9 +85,40 @@ def post(self, id):
self.quiz_entity.put()

# TODO: Delete quiz, questions, attempts, scores, attempts, snapshots
# via a couple of mapreduce jobs from cleanup.py

self.redirect('/')

class ArchiveHandler(QuizHandler):
def get(self, id):
self.fetch(id)
self.values['action'] = links.Quiz.archive(int(id))
self.values['prompt'] = \
'Are you sure that you would like archive all scores for the ' \
'quiz, "%s," forever?' % self.quiz_entity.title
self.values['back'] = os.environ.get('HTTP_REFERER',
links.Quiz.roster(int(id)))
self.output('confirm.html')

def post(self, id):
start_map('Archive scores',
'jobs.cleanup.archive',
'google.appengine.ext.mapreduce.input_readers.DatastoreInputReader',
{
'entity_kind': 'models.Score',
'quiz_id': int(id)
});

start_map('Archive attempts',
'jobs.cleanup.archive',
'google.appengine.ext.mapreduce.input_readers.DatastoreInputReader',
{
'entity_kind': 'models.Attempt',
'quiz_id': int(id)
});

self.redirect(links.Quiz.roster(int(id)))

class RosterHandler(QuizHandler):
def get(self, id):
self.fetch(id)
Expand All @@ -102,7 +135,8 @@ def main():
('/quiz/add', AddHandler),
(r'/quiz/edit/(.*)', EditHandler),
(r'/quiz/delete/(.*)', DeleteHandler),
(r'/quiz/roster/(.*)', RosterHandler)
(r'/quiz/roster/(.*)', RosterHandler),
(r'/quiz/archive/(.*)', ArchiveHandler)
], debug=True)
util.run_wsgi_app(application)

Expand Down
2 changes: 1 addition & 1 deletion handlers/quiz/take.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def get(self, id):
self.error(403)
return

if attempt.is_archived:
if (not self.is_admin) and attempt.is_archived:
self.error(403)
return

Expand Down
2 changes: 1 addition & 1 deletion jobs/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ def archive(entity):
quiz_id = int(params['quiz_id'])
if entity.quiz.key().id() == quiz_id:
entity.is_archived = True
yield operation.db.Put(entity)
yield operation.db.Put(entity)
6 changes: 5 additions & 1 deletion links.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ def my_score(id):
@staticmethod
def delete(id):
return '/quiz/delete/%d' % int(id)


@staticmethod
def archive(id):
return '/quiz/archive/%d' % int(id)

@staticmethod
def add():
return '/quiz/add'
Expand Down
4 changes: 2 additions & 2 deletions mapreduce.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mapreduce:
- name: Archive attempts for quiz
- name: Archive entities
mapper:
input_reader: google.appengine.ext.mapreduce.input_readers.DatastoreInputReader
handler: jobs.cleanup.archive
params:
- name: entity_kind
default: models.Attempt
- name: quiz_id
default:
default:
2 changes: 2 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def related_score(self, user=None):
user = users.get_current_user();

query = db.Query(Score)
query.filter('is_archived =', False)
query.filter('quiz =', self)
query.filter('user =', user)
self._score = query.get()
Expand Down Expand Up @@ -150,6 +151,7 @@ class Score(db.Model):
updated = db.DateTimeProperty(auto_now=True)
first_name = db.StringProperty()
last_name = db.StringProperty()
is_archived = db.BooleanProperty(default=False)

@staticmethod
def link_my_scores():
Expand Down

0 comments on commit ed4ceda

Please sign in to comment.