def supply(request, page_name):
    """ supply view_objects for widget rendering."""
    _ = request
    _ = page_name
    levels = DesignerLevel.objects.all()
    if len(levels) == 0:  # need to create default level
        l = DesignerLevel()
        l.name = "Level 1"  # no name
        l.slug = "default"
        l.unlock_condition = "True"
        l.unlock_condition_text = "Unlocked"
        l.save()

#    print smartgrid_mgr.get_designer_grid()
    return {
        'levels': levels,
        'columns': LibraryColumnName.objects.all(),
        'activities': LibraryActivity.objects.all(),
        'commitments': LibraryCommitment.objects.all(),
        'events': LibraryEvent.objects.all(),
        'fillers': Filler.objects.all(),
        'reset_form': RevertToSmartgridForm(),
        'publish_form': DeployToSmartgridForm(),
        'example_grid_form': ExampleGridsForm(),
        'add_level_form': AddLevelForm(),
        'delete_level_form': DeleteLevelForm(),
        'event_date_form': EventDateForm(),
        'palette': smartgrid_mgr.get_designer_palette(),
        'designer_grid': smartgrid_mgr.get_designer_grid(),
        'smart_grid_actions': smartgrid_mgr.get_designer_action_slugs(),
        'smart_grid_columns': smartgrid_mgr.get_designer_column_name_slugs(),
            }
Example #2
0
 def __create_cams_draft(self):
     """Creates Cam's Draft smartgrid with two levels, three columns, three actions"""
     try:
         draft = get_object_or_404(Draft, slug='cam')
     except Http404:
         draft = Draft(name="Cam", slug="cam")
         draft.save()
     level1 = DesignerLevel(draft=draft, name="Foo", slug="foo", priority=1)
     level1.save()
     level2 = DesignerLevel(draft=draft, name="Bar", slug="bar", priority=2)
     level2.save()
     column1 = DesignerColumnName(draft=draft, name="Baz", slug='baz')
     column1.save()
     column2 = DesignerColumnName(draft=draft, name="Qux", slug='qux')
     column2.save()
     column3 = DesignerColumnName(draft=draft, name="Zob", slug='zob')
     column3.save()
     col_loc = DesignerColumnGrid(draft=draft, level=level1, column=5, name=column1)
     col_loc.save()
     col_loc = DesignerColumnGrid(draft=draft, level=level1, column=2, name=column2)
     col_loc.save()
     col_loc = DesignerColumnGrid(draft=draft, level=level2, column=1, name=column3)
     col_loc.save()
     action = smartgrid_mgr.instantiate_designer_action_from_library(draft, \
                                                                     'play-outside-cafe-1')
     grid_loc = DesignerGrid(draft=draft, level=level1, column=5, row=2, action=action)
     grid_loc.save()
     action = smartgrid_mgr.instantiate_designer_action_from_library(draft, \
                                                                     'use-stairs')
     grid_loc = DesignerGrid(draft=draft, level=level1, column=2, row=1, action=action)
     grid_loc.save()
     action = smartgrid_mgr.instantiate_designer_action_from_library(draft, \
                                                                     'energy-city')
     grid_loc = DesignerGrid(draft=draft, level=level2, column=5, row=5, action=action)
     grid_loc.save()
Example #3
0
 def __create_cams_draft(self):
     """Creates Cam's Draft smartgrid with two levels, three columns, three actions"""
     try:
         draft = get_object_or_404(Draft, slug='cam')
     except Http404:
         draft = Draft(name="Cam", slug="cam")
         draft.save()
     level1 = DesignerLevel(draft=draft, name="Foo", slug="foo", priority=1)
     level1.save()
     level2 = DesignerLevel(draft=draft, name="Bar", slug="bar", priority=2)
     level2.save()
     column1 = DesignerColumnName(draft=draft, name="Baz", slug='baz')
     column1.save()
     column2 = DesignerColumnName(draft=draft, name="Qux", slug='qux')
     column2.save()
     column3 = DesignerColumnName(draft=draft, name="Zob", slug='zob')
     column3.save()
     col_loc = DesignerColumnGrid(draft=draft,
                                  level=level1,
                                  column=5,
                                  name=column1)
     col_loc.save()
     col_loc = DesignerColumnGrid(draft=draft,
                                  level=level1,
                                  column=2,
                                  name=column2)
     col_loc.save()
     col_loc = DesignerColumnGrid(draft=draft,
                                  level=level2,
                                  column=1,
                                  name=column3)
     col_loc.save()
     action = smartgrid_mgr.instantiate_designer_action_from_library(draft, \
                                                                     'play-outside-cafe-1')
     grid_loc = DesignerGrid(draft=draft,
                             level=level1,
                             column=5,
                             row=2,
                             action=action)
     grid_loc.save()
     action = smartgrid_mgr.instantiate_designer_action_from_library(draft, \
                                                                     'use-stairs')
     grid_loc = DesignerGrid(draft=draft,
                             level=level1,
                             column=2,
                             row=1,
                             action=action)
     grid_loc.save()
     action = smartgrid_mgr.instantiate_designer_action_from_library(draft, \
                                                                     'energy-city')
     grid_loc = DesignerGrid(draft=draft,
                             level=level2,
                             column=5,
                             row=5,
                             action=action)
     grid_loc.save()
def add_level(request):
    """Creates a new level."""
    if request.method == 'POST':
        form = AddLevelForm(request.POST)
        if form.is_valid():
            max_priority = 0
            for level in DesignerLevel.objects.all():
                if max_priority < level.priority:
                    max_priority = level.priority
            max_priority += 1
            slug = slugify(form.cleaned_data['level_name'])
            level = DesignerLevel(name=form.cleaned_data['level_name'], slug=slug, \
                                  priority=max_priority)
            level.save()
    response = HttpResponseRedirect("/sgg_designer/")
    return response
Example #5
0
def copy_draft(from_draft, to_draft):
    """Copies all the items in from_draft to copy_draft."""
#     print "copy_draft(%s, %s)" % (from_draft, to_draft)
    clear_designer(to_draft)
    # levels
    for level in DesignerLevel.objects.filter(draft=from_draft):
        copy = DesignerLevel(draft=to_draft)
        _copy_fields_no_foriegn_keys(level, copy)
        copy.save()
    # ColumnNames
    for column in DesignerColumnName.objects.filter(draft=from_draft):
        copy = DesignerColumnName(draft=to_draft)
        _copy_fields_no_foriegn_keys(column, copy)
        copy.save()
    # DesignerColumnGrid
    for loc in DesignerColumnGrid.objects.filter(draft=from_draft):
        level = get_designer_level(to_draft, loc.level.slug)
        column = get_designer_column_name(to_draft, loc.name.slug)
        copy = DesignerColumnGrid(draft=to_draft, name=column, level=level)
        _copy_fields_no_foriegn_keys(loc, copy)
        copy.save()
    # DesignerActions
    for action in DesignerAction.objects.filter(draft=from_draft):
        action = get_designer_action(from_draft, action.slug)
        if action.type == 'activity':
            copy = DesignerActivity(draft=to_draft)
        elif action.type == 'commitment':
            copy = DesignerCommitment(draft=to_draft)
        elif action.type == 'event':
            copy = DesignerEvent(draft=to_draft)
        _copy_fields_no_foriegn_keys(action, copy)
        copy.save()
        # Copy all the DesignerTextPropmtQuestions
        for question in DesignerTextPromptQuestion.objects.filter(action=action, draft=from_draft):
            des_obj = DesignerTextPromptQuestion(action=copy, draft=to_draft)
            _copy_fields_no_foriegn_keys(question, des_obj)
            des_obj.save()
    # DesignerGrid
    for loc in DesignerGrid.objects.filter(draft=from_draft):
        level = get_designer_level(to_draft, loc.level.slug)
        action = get_designer_action(to_draft, loc.action.slug)
        copy = DesignerGrid(level=level, draft=to_draft, action=action)
        _copy_fields_no_foriegn_keys(loc, copy)
        copy.save()
    return to_draft
Example #6
0
def copy_draft(from_draft, to_draft):
    """Copies all the items in from_draft to copy_draft."""
#     print "copy_draft(%s, %s)" % (from_draft, to_draft)
    clear_designer(to_draft)
    # levels
    for level in DesignerLevel.objects.filter(draft=from_draft):
        copy = DesignerLevel(draft=to_draft)
        _copy_fields_no_foriegn_keys(level, copy)
        copy.save()
    # ColumnNames
    for column in DesignerColumnName.objects.filter(draft=from_draft):
        copy = DesignerColumnName(draft=to_draft)
        _copy_fields_no_foriegn_keys(column, copy)
        copy.save()
    # DesignerColumnGrid
    for loc in DesignerColumnGrid.objects.filter(draft=from_draft):
        level = get_designer_level(to_draft, loc.level.slug)
        column = get_designer_column_name(to_draft, loc.name.slug)
        copy = DesignerColumnGrid(draft=to_draft, name=column, level=level)
        _copy_fields_no_foriegn_keys(loc, copy)
        copy.save()
    # DesignerActions
    for action in DesignerAction.objects.filter(draft=from_draft):
        action = get_designer_action(from_draft, action.slug)
        if action.type == 'activity':
            copy = DesignerActivity(draft=to_draft)
        elif action.type == 'commitment':
            copy = DesignerCommitment(draft=to_draft)
        elif action.type == 'event':
            copy = DesignerEvent(draft=to_draft)
        _copy_fields_no_foriegn_keys(action, copy)
        copy.save()
        # Copy all the DesignerTextPropmtQuestions
        for question in DesignerTextPromptQuestion.objects.filter(action=action, draft=from_draft):
            des_obj = DesignerTextPromptQuestion(action=copy, draft=to_draft)
            _copy_fields_no_foriegn_keys(question, des_obj)
            des_obj.save()
    # DesignerGrid
    for loc in DesignerGrid.objects.filter(draft=from_draft):
        level = get_designer_level(to_draft, loc.level.slug)
        action = get_designer_action(to_draft, loc.action.slug)
        copy = DesignerGrid(level=level, draft=to_draft, action=action)
        _copy_fields_no_foriegn_keys(loc, copy)
        copy.save()
    return to_draft
Example #7
0
    def setUp(self):
        """Sets up the test evironment for the Designer views."""
        self.user = test_utils.setup_superuser(username="******", password="******")

        challenge_mgr.register_page_widget("sgg_designer", "smartgrid_design")
        from apps.managers.cache_mgr import cache_mgr
        cache_mgr.clear()

        self.client.login(username="******", password="******")
        try:
            draft = smartgrid_mgr.get_designer_draft(self.draft_slug)
        except Http404:  # shouldn't happen Default draft is defined in base_settings
            draft = Draft(name='Default', slug='default')
            draft.save()
        try:
            level = smartgrid_mgr.get_designer_level(draft, self.level_slug)
        except Http404:  # ensure there is a DesignerLevel
            level = DesignerLevel(name="Level 1", slug=self.level_slug, priority=1, draft=draft)
            level.save()
Example #8
0
def add_level(request, draft_slug):
    """Creates a new level."""
    draft = smartgrid_mgr.get_designer_draft(draft_slug)
    if request.method == 'POST':
        form = AddLevelForm(request.POST)
        if form.is_valid():
            max_priority = 0
            for level in DesignerLevel.objects.filter(draft=draft):
                if max_priority < level.priority:
                    max_priority = level.priority
            max_priority += 1
            slug = slugify(form.cleaned_data['level_name'])
            level = DesignerLevel(name=form.cleaned_data['level_name'], slug=slug, \
                                  priority=max_priority, draft=draft)
            predicate = form.cleaned_data['unlock_condition']
            if not predicate_mgr.validate_predicates(predicate):
                level.unlock_condition = predicate
            else:
                level.unlock_condition = 'False'  # What is the correct default value?
            level.save()
    response = HttpResponseRedirect("/sgg_designer/?draft=%s" % draft.slug)
    return response
Example #9
0
def add_level(request, draft_slug):
    """Creates a new level."""
    draft = smartgrid_mgr.get_designer_draft(draft_slug)
    if request.method == 'POST':
        form = AddLevelForm(request.POST)
        if form.is_valid():
            max_priority = 0
            for level in DesignerLevel.objects.filter(draft=draft):
                if max_priority < level.priority:
                    max_priority = level.priority
            max_priority += 1
            slug = slugify(form.cleaned_data['level_name'])
            level = DesignerLevel(name=form.cleaned_data['level_name'], slug=slug, \
                                  priority=max_priority, draft=draft)
            predicate = form.cleaned_data['unlock_condition']
            if not predicate_mgr.validate_predicates(predicate):
                level.unlock_condition = predicate
            else:
                level.unlock_condition = 'False'  # What is the correct default value?
            level.save()
    response = HttpResponseRedirect("/sgg_designer/?draft=%s" % draft.slug)
    return response
Example #10
0
def supply(request, page_name):
    """ supply view_objects for widget rendering."""
    _ = page_name
    user = request.user
    gcc_settings, _ = GccSettings.objects.get_or_create(user=user)
    draft_choices = Draft.objects.all()
    draft = None
    tree_list = None
    levels = []
    if len(draft_choices) != 0:
        try:
            draft_slug = request.REQUEST['draft']
        except KeyError:
            try:
                draft_slug = request.COOKIES['current-designer-draft']
            except KeyError:
                draft_slug = draft_choices[0].slug
        try:
            draft = smartgrid_mgr.get_designer_draft(draft_slug)
        except Http404:
            draft = draft_choices[0]
        levels = DesignerLevel.objects.filter(draft=draft)
        if len(levels) == 0:  # need to create default level
            l = DesignerLevel()
            l.name = "Level 1"  # no name
            l.slug = "default"
            l.unlock_condition = "True"
            l.unlock_condition_text = "Unlocked"
            l.draft = draft
            l.save()
        levels = DesignerLevel.objects.filter(draft=draft)
        ts = action_dependency.build_designer_grid_trees(draft)
        tree_list = []
        for k in list(ts):
            tree_list.append(ts[k].tohtmlstring())
    results = gcc.run_designer_checks(draft, gcc_settings)

    return {
        'draft': draft,
        'draft_choices': draft_choices,
        'levels': levels,
        'columns': LibraryColumnName.objects.all(),
        'activities': LibraryActivity.objects.all(),
        'commitments': LibraryCommitment.objects.all(),
        'events': LibraryEvent.objects.all(),
        'fillers': Filler.objects.all(),
        'reset_form': RevertToSmartgridForm(),
        'publish_form': DeployToSmartgridForm(),
        'example_grid_form': ExampleGridsForm(),
        'add_level_form': AddLevelForm(),
        'delete_level_form': DeleteLevelForm(),
        'event_date_form': EventDateForm(),
        'new_draft_form': NewDraftForm(),
        'delete_draft_form': DeleteDraftForm(),
        'load_template_form': LoadTemplateForm(),
        'gcc_settings_form': GccSettingsForm(instance=gcc_settings),
        'palette': smartgrid_mgr.get_designer_palette(draft),
        'designer_grid': smartgrid_mgr.get_designer_grid(draft),
        'designer_actions': smartgrid_mgr.get_designer_action_slugs(draft),
        'designer_columns':
        smartgrid_mgr.get_designer_column_name_slugs(draft),
        'errors': results[_ERRORS],
        'warnings': results[_WARNINGS],
        'trees': tree_list,
    }
Example #11
0
class Test(TransactionTestCase):
    """Test cases for smartgrid_mgr."""
    fixtures = ['smartgrid_library', 'base_settings.json', 'test_smartgrid.json', \
                'test_designer.json']

    def __create_cams_draft(self):
        """Creates Cam's Draft smartgrid with two levels, three columns, three actions"""
        try:
            draft = get_object_or_404(Draft, slug='cam')
        except Http404:
            draft = Draft(name="Cam", slug="cam")
            draft.save()
        level1 = DesignerLevel(draft=draft, name="Foo", slug="foo", priority=1)
        level1.save()
        level2 = DesignerLevel(draft=draft, name="Bar", slug="bar", priority=2)
        level2.save()
        column1 = DesignerColumnName(draft=draft, name="Baz", slug='baz')
        column1.save()
        column2 = DesignerColumnName(draft=draft, name="Qux", slug='qux')
        column2.save()
        column3 = DesignerColumnName(draft=draft, name="Zob", slug='zob')
        column3.save()
        col_loc = DesignerColumnGrid(draft=draft,
                                     level=level1,
                                     column=5,
                                     name=column1)
        col_loc.save()
        col_loc = DesignerColumnGrid(draft=draft,
                                     level=level1,
                                     column=2,
                                     name=column2)
        col_loc.save()
        col_loc = DesignerColumnGrid(draft=draft,
                                     level=level2,
                                     column=1,
                                     name=column3)
        col_loc.save()
        action = smartgrid_mgr.instantiate_designer_action_from_library(draft, \
                                                                        'play-outside-cafe-1')
        grid_loc = DesignerGrid(draft=draft,
                                level=level1,
                                column=5,
                                row=2,
                                action=action)
        grid_loc.save()
        action = smartgrid_mgr.instantiate_designer_action_from_library(draft, \
                                                                        'use-stairs')
        grid_loc = DesignerGrid(draft=draft,
                                level=level1,
                                column=2,
                                row=1,
                                action=action)
        grid_loc.save()
        action = smartgrid_mgr.instantiate_designer_action_from_library(draft, \
                                                                        'energy-city')
        grid_loc = DesignerGrid(draft=draft,
                                level=level2,
                                column=5,
                                row=5,
                                action=action)
        grid_loc.save()

    def __clean_cams_draft(self):
        """removes Cam's draft."""
        draft = get_object_or_404(Draft, slug='cam')
        for level in DesignerLevel.objects.filter(draft=draft):
            level.delete()
        for col in DesignerColumnName.objects.filter(draft=draft):
            col.delete()
        for action in DesignerAction.objects.filter(draft=draft):
            action.delete()

    def setUp(self):
        self.__create_cams_draft()
        self.action_slug = 'intro-video'
        self.col_slug = 'get-started'
        try:
            self.draft = get_object_or_404(Draft, slug="default")
        except Http404:
            self.draft = Draft(name="Default", slug="default")
            self.draft.save()
        try:
            self.designer_level = get_object_or_404(DesignerLevel,
                                                    slug='level-1')
        except Http404:
            self.designer_level = DesignerLevel(name="Level 1",
                                                slug="level-1",
                                                priority=1)
            self.designer_level.draft = self.draft
            self.designer_level.save()

    def tearDown(self):
        self.__clean_cams_draft()
        try:
            draft = get_object_or_404(Draft, slug='temp')
            draft.delete()
        except Http404:
            pass

    def testLibraryToDesigner(self):
        """Tests instantiating a DesignerAction from a LibraryAction."""
        # LibraryAction -> DesignerAction
        lib_action = smartgrid_mgr.get_library_action(self.action_slug)
        des_action = smartgrid_mgr.instantiate_designer_action_from_library(self.draft, \
                                                                            lib_action.slug)
        self.assertTrue(
            des_action,
            "Couldn't instantiate designer action %s" % lib_action.slug)
        self.assertTrue(smartgrid_mgr.get_designer_action(self.draft, self.action_slug), \
                        "Couldn't retrieve the designer action %s" % self.action_slug)
        # ensure the TextPropmptQuestions are there
        for tpq in LibraryTextPromptQuestion.objects.filter(
                libraryaction=lib_action):
            try:
                des_tpq = get_object_or_404(DesignerTextPromptQuestion, draft=self.draft, \
                                            question=tpq.question, answer=tpq.answer, \
                                            action=des_action)
                self.assertTrue(des_tpq,
                                "Couldn't get DesignerTextPromptQuestion")
            except Http404:
                self.fail("Couldn't find DesignerTextPromptQuestion for %s" %
                          tpq)
        # LibraryColumnNames -> DesignerColumnNames
        lib_column = smartgrid_mgr.get_library_column_name(self.col_slug)
        self.assertTrue(lib_column,
                        "Couldn't get LibraryColumnName %s" % self.col_slug)
        des_column = smartgrid_mgr.instantiate_designer_column_from_library(self.draft, \
                                                                            self.col_slug)
        self.assertTrue(des_column,
                        "Couldn't get DesignerColumnName %s" % self.col_slug)
        self.assertEqual(self.draft, des_column.draft, "Drafts are not equal.")

    def testGridToDesigner(self):
        """Tests instantiating a DesignerAction from an Action."""
        # clear the existing DesignerActions, if any.
        for des_act in DesignerAction.objects.filter(draft=self.draft,
                                                     slug=self.action_slug):
            des_act.delete()
        action = smartgrid_mgr.get_smartgrid_action(self.action_slug)
        des_action = smartgrid_mgr.instantiate_designer_action_from_smartgrid(self.draft, \
                                                                              slug=action.slug)
        self.assertTrue(
            des_action,
            "Couldn't instantiate designer action %s" % action.slug)
        self.assertTrue(smartgrid_mgr.get_designer_action(self.draft, self.action_slug), \
                        "Couldn't retrieve the designer action %s" % self.action_slug)
        # ensure the TextPropmptQuestions are there
        for tpq in TextPromptQuestion.objects.filter(action=action):
            try:
                des_tpq = get_object_or_404(DesignerTextPromptQuestion, draft=self.draft, \
                                            question=tpq.question, answer=tpq.answer, \
                                            action=des_action)
                self.assertTrue(des_tpq,
                                "Couldn't get DesignerTextPromptQuestion")
            except Http404:
                self.fail("Couldn't find DesignerTextPromptQuestion for %s" %
                          tpq)

    def testDeploy(self):
        """Tests deploying a draft Designer Grid to the Smart Grid Game."""
        draft = get_object_or_404(Draft, slug='cam')
        smartgrid_mgr.deploy_designer_to_smartgrid(draft=draft,
                                                   use_filler=False)
        # two levels
        self.assertEqual(len(Level.objects.all()), 2, "Expceting 2 levels got %s" % \
                         len(Level.objects.all()))
        self.assertTrue(Level.objects.get(slug='foo'), "Didn't get level foo")
        self.assertTrue(Level.objects.get(slug='bar'), "Didn't get level bar")
        # Three ColumnNames
        self.assertEqual(len(ColumnName.objects.all()), 3, "Expecting 3 ColumnNames got %s" % \
                         len(ColumnName.objects.all()))
        self.assertTrue(ColumnName.objects.get(slug='baz'),
                        "Didn't get Column baz")
        self.assertTrue(ColumnName.objects.get(slug='qux'),
                        "Didn't get Column qux")
        self.assertTrue(ColumnName.objects.get(slug='zob'),
                        "Didn't get Column zob")
        # Three Actions in grid
        self.assertEqual(len(Grid.objects.all()), 3, "Expecting 3 actions in grid got %s" % \
                         len(Grid.objects.all()))

    def testRevert(self):
        """Tests reverting to the current Smartgrid."""
        try:
            draft = get_object_or_404(Draft, slug='temp')
        except Http404:
            draft = Draft(name='Temp', slug='temp')
            draft.save()
        smartgrid_mgr.copy_smartgrid_to_designer(draft)
        self.assertEqual(len(DesignerLevel.objects.filter(draft=draft)), 4, \
                         "Expecting 4 levels got %s" % \
                         len(DesignerLevel.objects.filter(draft=draft)))
        self.assertEqual(len(DesignerColumnName.objects.filter(draft=draft)), 30, \
                         "Expecting 30 ColumnNames got %s" % \
                         len(DesignerColumnName.objects.filter(draft=draft)))
        self.assertEqual(len(DesignerAction.objects.filter(draft=draft)), 84, \
                         "Expecting 84 Actions got %s" % \
                         len(DesignerAction.objects.filter(draft=draft)))
Example #12
0
class Test(TransactionTestCase):
    """Test cases for smartgrid_mgr."""
    fixtures = ['smartgrid_library', 'base_settings.json', 'test_smartgrid.json', \
                'test_designer.json']

    def __create_cams_draft(self):
        """Creates Cam's Draft smartgrid with two levels, three columns, three actions"""
        try:
            draft = get_object_or_404(Draft, slug='cam')
        except Http404:
            draft = Draft(name="Cam", slug="cam")
            draft.save()
        level1 = DesignerLevel(draft=draft, name="Foo", slug="foo", priority=1)
        level1.save()
        level2 = DesignerLevel(draft=draft, name="Bar", slug="bar", priority=2)
        level2.save()
        column1 = DesignerColumnName(draft=draft, name="Baz", slug='baz')
        column1.save()
        column2 = DesignerColumnName(draft=draft, name="Qux", slug='qux')
        column2.save()
        column3 = DesignerColumnName(draft=draft, name="Zob", slug='zob')
        column3.save()
        col_loc = DesignerColumnGrid(draft=draft, level=level1, column=5, name=column1)
        col_loc.save()
        col_loc = DesignerColumnGrid(draft=draft, level=level1, column=2, name=column2)
        col_loc.save()
        col_loc = DesignerColumnGrid(draft=draft, level=level2, column=1, name=column3)
        col_loc.save()
        action = smartgrid_mgr.instantiate_designer_action_from_library(draft, \
                                                                        'play-outside-cafe-1')
        grid_loc = DesignerGrid(draft=draft, level=level1, column=5, row=2, action=action)
        grid_loc.save()
        action = smartgrid_mgr.instantiate_designer_action_from_library(draft, \
                                                                        'use-stairs')
        grid_loc = DesignerGrid(draft=draft, level=level1, column=2, row=1, action=action)
        grid_loc.save()
        action = smartgrid_mgr.instantiate_designer_action_from_library(draft, \
                                                                        'energy-city')
        grid_loc = DesignerGrid(draft=draft, level=level2, column=5, row=5, action=action)
        grid_loc.save()

    def __clean_cams_draft(self):
        """removes Cam's draft."""
        draft = get_object_or_404(Draft, slug='cam')
        for level in DesignerLevel.objects.filter(draft=draft):
            level.delete()
        for col in DesignerColumnName.objects.filter(draft=draft):
            col.delete()
        for action in DesignerAction.objects.filter(draft=draft):
            action.delete()

    def setUp(self):
        self.__create_cams_draft()
        self.action_slug = 'intro-video'
        self.col_slug = 'get-started'
        try:
            self.draft = get_object_or_404(Draft, slug="default")
        except Http404:
            self.draft = Draft(name="Default", slug="default")
            self.draft.save()
        try:
            self.designer_level = get_object_or_404(DesignerLevel, slug='level-1')
        except Http404:
            self.designer_level = DesignerLevel(name="Level 1", slug="level-1", priority=1)
            self.designer_level.draft = self.draft
            self.designer_level.save()

    def tearDown(self):
        self.__clean_cams_draft()
        try:
            draft = get_object_or_404(Draft, slug='temp')
            draft.delete()
        except Http404:
            pass

    def testLibraryToDesigner(self):
        """Tests instantiating a DesignerAction from a LibraryAction."""
        # LibraryAction -> DesignerAction
        lib_action = smartgrid_mgr.get_library_action(self.action_slug)
        des_action = smartgrid_mgr.instantiate_designer_action_from_library(self.draft, \
                                                                            lib_action.slug)
        self.assertTrue(des_action, "Couldn't instantiate designer action %s" % lib_action.slug)
        self.assertTrue(smartgrid_mgr.get_designer_action(self.draft, self.action_slug), \
                        "Couldn't retrieve the designer action %s" % self.action_slug)
        # ensure the TextPropmptQuestions are there
        for tpq in LibraryTextPromptQuestion.objects.filter(libraryaction=lib_action):
            try:
                des_tpq = get_object_or_404(DesignerTextPromptQuestion, draft=self.draft, \
                                            question=tpq.question, answer=tpq.answer, \
                                            action=des_action)
                self.assertTrue(des_tpq, "Couldn't get DesignerTextPromptQuestion")
            except Http404:
                self.fail("Couldn't find DesignerTextPromptQuestion for %s" % tpq)
        # LibraryColumnNames -> DesignerColumnNames
        lib_column = smartgrid_mgr.get_library_column_name(self.col_slug)
        self.assertTrue(lib_column, "Couldn't get LibraryColumnName %s" % self.col_slug)
        des_column = smartgrid_mgr.instantiate_designer_column_from_library(self.draft, \
                                                                            self.col_slug)
        self.assertTrue(des_column, "Couldn't get DesignerColumnName %s" % self.col_slug)
        self.assertEqual(self.draft, des_column.draft, "Drafts are not equal.")

    def testGridToDesigner(self):
        """Tests instantiating a DesignerAction from an Action."""
        # clear the existing DesignerActions, if any.
        for des_act in DesignerAction.objects.filter(draft=self.draft, slug=self.action_slug):
            des_act.delete()
        action = smartgrid_mgr.get_smartgrid_action(self.action_slug)
        des_action = smartgrid_mgr.instantiate_designer_action_from_smartgrid(self.draft, \
                                                                              slug=action.slug)
        self.assertTrue(des_action, "Couldn't instantiate designer action %s" % action.slug)
        self.assertTrue(smartgrid_mgr.get_designer_action(self.draft, self.action_slug), \
                        "Couldn't retrieve the designer action %s" % self.action_slug)
        # ensure the TextPropmptQuestions are there
        for tpq in TextPromptQuestion.objects.filter(action=action):
            try:
                des_tpq = get_object_or_404(DesignerTextPromptQuestion, draft=self.draft, \
                                            question=tpq.question, answer=tpq.answer, \
                                            action=des_action)
                self.assertTrue(des_tpq, "Couldn't get DesignerTextPromptQuestion")
            except Http404:
                self.fail("Couldn't find DesignerTextPromptQuestion for %s" % tpq)

    def testDeploy(self):
        """Tests deploying a draft Designer Grid to the Smart Grid Game."""
        draft = get_object_or_404(Draft, slug='cam')
        smartgrid_mgr.deploy_designer_to_smartgrid(draft=draft, use_filler=False)
        # two levels
        self.assertEqual(len(Level.objects.all()), 2, "Expceting 2 levels got %s" % \
                         len(Level.objects.all()))
        self.assertTrue(Level.objects.get(slug='foo'), "Didn't get level foo")
        self.assertTrue(Level.objects.get(slug='bar'), "Didn't get level bar")
        # Three ColumnNames
        self.assertEqual(len(ColumnName.objects.all()), 3, "Expecting 3 ColumnNames got %s" % \
                         len(ColumnName.objects.all()))
        self.assertTrue(ColumnName.objects.get(slug='baz'), "Didn't get Column baz")
        self.assertTrue(ColumnName.objects.get(slug='qux'), "Didn't get Column qux")
        self.assertTrue(ColumnName.objects.get(slug='zob'), "Didn't get Column zob")
        # Three Actions in grid
        self.assertEqual(len(Grid.objects.all()), 3, "Expecting 3 actions in grid got %s" % \
                         len(Grid.objects.all()))

    def testRevert(self):
        """Tests reverting to the current Smartgrid."""
        try:
            draft = get_object_or_404(Draft, slug='temp')
        except Http404:
            draft = Draft(name='Temp', slug='temp')
            draft.save()
        smartgrid_mgr.copy_smartgrid_to_designer(draft)
        self.assertEqual(len(DesignerLevel.objects.filter(draft=draft)), 4, \
                         "Expecting 4 levels got %s" % \
                         len(DesignerLevel.objects.filter(draft=draft)))
        self.assertEqual(len(DesignerColumnName.objects.filter(draft=draft)), 30, \
                         "Expecting 30 ColumnNames got %s" % \
                         len(DesignerColumnName.objects.filter(draft=draft)))
        self.assertEqual(len(DesignerAction.objects.filter(draft=draft)), 84, \
                         "Expecting 84 Actions got %s" % \
                         len(DesignerAction.objects.filter(draft=draft)))

    def testLoadExampleGrid(self):
        """Tests load_example_grid(draft, example_name)."""
        try:
            draft = get_object_or_404(Draft, slug='temp2')
        except Http404:
            draft = Draft(name='Temp', slug='temp2')
            draft.save()
        smartgrid_mgr.load_example_grid(draft, 'test')
        self.assertEqual(len(DesignerLevel.objects.filter(draft=draft)), 4, \
                         "Expecting 4 levels got %s" % \
                         len(DesignerLevel.objects.filter(draft=draft)))
        self.assertEqual(len(DesignerColumnName.objects.filter(draft=draft)), 30, \
                         "Expecting 30 ColumnNames got %s" % \
                         len(DesignerColumnName.objects.filter(draft=draft)))
        self.assertEqual(len(DesignerAction.objects.filter(draft=draft)), 84, \
                         "Expecting 84 Actions got %s" % \
                         len(DesignerAction.objects.filter(draft=draft)))
        val = len(DesignerGrid.objects.filter(draft=draft))
        ans = 84
        self.assertEqual(val, ans, "Expecting %s got %s" % (ans, val))
        try:
            draft = get_object_or_404(Draft, slug='temp3')
        except Http404:
            draft = Draft(name='Temp3', slug='temp3')
            draft.save()
Example #13
0
def supply(request, page_name):
    """ supply view_objects for widget rendering."""
    _ = page_name
    user = request.user
    gcc_settings, _ = GccSettings.objects.get_or_create(user=user)
    draft_choices = Draft.objects.all()
    draft = None
    tree_list = None
    levels = []
    if len(draft_choices) != 0:
        try:
            draft_slug = request.REQUEST['draft']
        except KeyError:
            try:
                draft_slug = request.COOKIES['current-designer-draft']
            except KeyError:
                draft_slug = draft_choices[0].slug
        try:
            draft = smartgrid_mgr.get_designer_draft(draft_slug)
        except Http404:
            draft = draft_choices[0]
        levels = DesignerLevel.objects.filter(draft=draft)
        if len(levels) == 0:  # need to create default level
            l = DesignerLevel()
            l.name = "Level 1"  # no name
            l.slug = "default"
            l.unlock_condition = "True"
            l.unlock_condition_text = "Unlocked"
            l.draft = draft
            l.save()
        levels = DesignerLevel.objects.filter(draft=draft)
        ts = action_dependency.build_designer_grid_trees(draft)
        tree_list = []
        for k in  list(ts):
            tree_list.append(ts[k].tohtmlstring())
    results = gcc.run_designer_checks(draft, gcc_settings)

    return {
        'draft': draft,
        'draft_choices': draft_choices,
        'levels': levels,
        'columns': LibraryColumnName.objects.all(),
        'activities': LibraryActivity.objects.all(),
        'commitments': LibraryCommitment.objects.all(),
        'events': LibraryEvent.objects.all(),
        'fillers': Filler.objects.all(),
        'reset_form': RevertToSmartgridForm(),
        'publish_form': DeployToSmartgridForm(),
        'example_grid_form': ExampleGridsForm(),
        'add_level_form': AddLevelForm(),
        'delete_level_form': DeleteLevelForm(),
        'event_date_form': EventDateForm(),
        'new_draft_form': NewDraftForm(),
        'delete_draft_form': DeleteDraftForm(),
        'load_template_form': LoadTemplateForm(),
        'gcc_settings_form': GccSettingsForm(instance=gcc_settings),
        'palette': smartgrid_mgr.get_designer_palette(draft),
        'designer_grid': smartgrid_mgr.get_designer_grid(draft),
        'designer_actions': smartgrid_mgr.get_designer_action_slugs(draft),
        'designer_columns': smartgrid_mgr.get_designer_column_name_slugs(draft),
        'errors': results[_ERRORS],
        'warnings': results[_WARNINGS],
        'trees': tree_list,
            }