Esempio n. 1
0
def load_first_template(request):
    """Loads the first template into the Designer."""
    print "load_first_template()"
    if request.method == 'POST':
        form = LoadTemplateForm(request.POST)
        if form.is_valid():
            # load the largest so wont overwrite objects when we load real one.
            draft_name = form.cleaned_data['draft_name']
            template_name = form.cleaned_data['template']
            draft_slug = slugify(draft_name)
            try:
                draft = smartgrid_mgr.get_designer_draft(draft_slug)
            except Http404:
                draft = Draft(name=draft_name, slug=draft_slug)
                draft.save()
            delete_me = Draft(name="delete-me-soon12341",
                              slug='delete-me-soon12341')
            delete_me.save()
            smartgrid_mgr.load_example_grid(draft=delete_me,
                                            example_name='uh12')
            smartgrid_mgr.clear_designer(draft)
            if template_name != 'empty':
                smartgrid_mgr.clear_designer(draft=None)
                smartgrid_mgr.load_example_grid(draft, template_name)
            delete_me.delete()
    response = HttpResponseRedirect("/sgg_designer/?draft=%s" % draft.slug)
    return response
Esempio n. 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()
Esempio n. 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()
Esempio n. 4
0
def new_draft(request):
    """Creates a new Draft from the given draft name if the Draft doesn't already exist."""
    if request.method == 'POST':
        form = NewDraftForm(request.POST)
        if form.is_valid():
            draft_name = form.cleaned_data['draft_name']
            draft_slug = slugify(draft_name)
            try:
                draft = smartgrid_mgr.get_designer_draft(draft_slug)
            except Http404:
                draft = Draft(name=draft_name, slug=draft_slug)
                draft.save()
    response = HttpResponseRedirect("/sgg_designer/?draft=%s" % draft.slug)
    return response
Esempio n. 5
0
def new_draft(request):
    """Creates a new Draft from the given draft name if the Draft doesn't already exist."""
    if request.method == 'POST':
        form = NewDraftForm(request.POST)
        if form.is_valid():
            draft_name = form.cleaned_data['draft_name']
            draft_slug = slugify(draft_name)
            try:
                draft = smartgrid_mgr.get_designer_draft(draft_slug)
            except Http404:
                draft = Draft(name=draft_name, slug=draft_slug)
                draft.save()
    response = HttpResponseRedirect("/sgg_designer/?draft=%s" % draft.slug)
    return response
Esempio n. 6
0
def load_first_template(request):
    """Loads the first template into the Designer."""
    print "load_first_template()"
    if request.method == 'POST':
        form = LoadTemplateForm(request.POST)
        if form.is_valid():
            # load the largest so wont overwrite objects when we load real one.
            draft_name = form.cleaned_data['draft_name']
            template_name = form.cleaned_data['template']
            draft_slug = slugify(draft_name)
            try:
                draft = smartgrid_mgr.get_designer_draft(draft_slug)
            except Http404:
                draft = Draft(name=draft_name, slug=draft_slug)
                draft.save()
            delete_me = Draft(name="delete-me-soon12341", slug='delete-me-soon12341')
            delete_me.save()
            smartgrid_mgr.load_example_grid(draft=delete_me, example_name='uh12')
            smartgrid_mgr.clear_designer(draft)
            if template_name != 'empty':
                smartgrid_mgr.clear_designer(draft=None)
                smartgrid_mgr.load_example_grid(draft, template_name)
            delete_me.delete()
    response = HttpResponseRedirect("/sgg_designer/?draft=%s" % draft.slug)
    return response
Esempio n. 7
0
 def setUp(self):
     """Sets up the test environment."""
     self.user = test_utils.setup_user("user", "changeme")
     self.draft = Draft(name="test", slug="test")
     test_utils.set_competition_round()
     self.team = self.user.profile.team
     self.predicates = [
         "completed_action('intro-video')",
         "submitted_all_of_type(action_type='commitment')",
         "allocated_raffle_ticket()", "badge_awarded('daily-visitor')",
         "completed_some_of_level(level_priority=1, count=2)",
         "submitted_some_of_resource(resource='energy', count=2)",
         "submitted_all_of_level(1)",
         "approved_all_of_resource(resource='energy')",
         "approved_some_of_resource(resource='water', count=3)",
         "set_profile_pic()", "referring_count(4)", "daily_visit_count(10)",
         "unlock_on_date('13-07-16')",
         "unlock_on_event(event_slug='kick-off', days=-3, lock_after_days=5)",
         "approved_some_of_level(level_priority=3, count=3)",
         "submitted_level(3)", "completed_some_of('event', 3)",
         "daily_energy_goal_count(5)", "submitted_action('like-cup')",
         "approved_all_of_type('commitment')", "approved_all_of_level(4)",
         "posted_to_wall()", "submitted_some_of_level(3, 4)",
         "submitted_some_of_type('commitment', 2)",
         "submitted_some_full_spectrum(3)", "change_theme()",
         "changed_theme()", "approved_some_full_spectrum(4)",
         "reached_round('Round 2')", "social_bonus_count(30)",
         "team_member_point_percent(30, 25)", "approved_some(5)",
         "completed_level(3)", "approved_action('like-cup')",
         "has_points(10)", "submitted_all_of_resource('water')",
         "submitted_some(34)", "game_enabled('foobar')", "is_admin()",
         "approved_some_of_type('activity', 5)"
     ]
Esempio n. 8
0
 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)))
Esempio n. 9
0
 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)))
Esempio n. 10
0
 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()
Esempio n. 11
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()
Esempio n. 12
0
def load_template(request):
    """Loads a template into the given draft."""
    print "load_template()"
    if request.method == 'POST':
        form = LoadTemplateForm(request.POST)
        if form.is_valid():
            draft_name = form.cleaned_data['draft_name']
            template_name = form.cleaned_data['template']
            draft_slug = slugify(draft_name)
            try:
                draft = smartgrid_mgr.get_designer_draft(draft_slug)
            except Http404:
                draft = Draft(name=draft_name, slug=draft_slug)
                draft.save()
            smartgrid_mgr.clear_designer(draft)
            if template_name != 'empty':
                smartgrid_mgr.clear_designer(draft=None)
                smartgrid_mgr.load_example_grid(draft, template_name)
    response = HttpResponseRedirect("/sgg_designer/?draft=%s" % draft.slug)
    return response
Esempio n. 13
0
def load_template(request):
    """Loads a template into the given draft."""
    print "load_template()"
    if request.method == 'POST':
        form = LoadTemplateForm(request.POST)
        if form.is_valid():
            draft_name = form.cleaned_data['draft_name']
            template_name = form.cleaned_data['template']
            draft_slug = slugify(draft_name)
            try:
                draft = smartgrid_mgr.get_designer_draft(draft_slug)
            except Http404:
                draft = Draft(name=draft_name, slug=draft_slug)
                draft.save()
            smartgrid_mgr.clear_designer(draft)
            if template_name != 'empty':
                smartgrid_mgr.clear_designer(draft=None)
                smartgrid_mgr.load_example_grid(draft, template_name)
    response = HttpResponseRedirect("/sgg_designer/?draft=%s" % draft.slug)
    return response
Esempio n. 14
0
 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()
Esempio n. 15
0
 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()
Esempio n. 16
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()
Esempio n. 17
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)))