~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/apps/tutorial/__init__.py

  • Committer: Matt Giuca
  • Date: 2009-01-19 11:09:50 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: matt.giuca@gmail.com-20090119110950-a40040jv6clxrvej
tutorial: Replaced call to ivle.db.create_worksheet with local code (roughly
    the same algorithm) which uses Worksheet and Exercise.
    This expands the code a lot, but it's all relevant to the tutorial system,
    so it belongs here, and still shorter than db.create_worksheet.
ivle.database: Added new supporting methods for Worksheet and Exercise.
ivle.db: Removed create_worksheet.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
 
31
31
import os
32
32
import os.path
33
 
import datetime
 
33
from datetime import datetime
34
34
import cgi
35
35
import urllib
36
36
import re
310
310
    except:
311
311
        req.throw_error(req.HTTP_NOT_FOUND,
312
312
            "Worksheet file not found.")
313
 
    worksheetmtime = datetime.datetime.fromtimestamp(worksheetmtime)
 
313
    worksheetmtime = datetime.fromtimestamp(worksheetmtime)
314
314
 
315
315
    worksheetdom = minidom.parse(worksheetfile)
316
316
    worksheetfile.close()
637
637
    """
638
638
    worksheet = ivle.database.Worksheet.get_by_name(store, subject,
639
639
                                                    worksheetname)
640
 
    db_mtime = worksheet.mtime if worksheet is not None else None
641
 
    if db_mtime is None or file_mtime > db_mtime:
642
 
        db = ivle.db.DB()
643
 
        try:
644
 
            db.create_worksheet(subject, worksheetname, exercise_list,
645
 
                                assessable)
646
 
        finally:
647
 
            db.close()
 
640
 
 
641
    updated_database = False
 
642
    if worksheet is None:
 
643
        # If assessable is not supplied, default to False.
 
644
        if assessable is None:
 
645
            assessable = False
 
646
        # Create a new Worksheet
 
647
        worksheet = ivle.database.Worksheet(subject=subject,
 
648
            name=worksheetname, assessable=assessable, mtime=datetime.now())
 
649
        store.add(worksheet)
 
650
        updated_database = True
 
651
    else:
 
652
        if file_mtime > worksheet.mtime:
 
653
            # File on disk is newer than database. Need to update.
 
654
            worksheet.mtime = datetime.now()
 
655
            if exercise_list is not None:
 
656
                # exercise_list is supplied, so delete any existing problems
 
657
                worksheet.remove_all_exercises(store)
 
658
            if assessable is not None:
 
659
                worksheet.assessable = assessable
 
660
            updated_database = True
 
661
 
 
662
    if updated_database and exercise_list is not None:
 
663
        # Insert each exercise into the worksheet
 
664
        for exercise_tuple in exercise_list:
 
665
            if isinstance(exercise_tuple, tuple):
 
666
                exercise_name = exercise_tuple[0]
 
667
                try:
 
668
                    optional = exercise_tuple[1]
 
669
                except IndexError:
 
670
                    optional = False
 
671
            else:
 
672
                exercise_name = exercise_tuple
 
673
                optional = False
 
674
            # Get the Exercise from the DB
 
675
            # XXX What if this fails?
 
676
            exercise = ivle.database.Exercise.get_by_name(store,exercise_name)
 
677
            # Create a new binding between the worksheet and the exercise
 
678
            worksheetexercise = ivle.database.WorksheetExercise(
 
679
                    worksheet=worksheet, exercise=exercise, optional=optional)
 
680
 
 
681
    store.commit()