<commit_before>import bpy
import bgl
import blf
from math import floor
from bgl import *
from bpy_extras import view3d_utils
from mathutils import Vector, Matrix
from . import sprytile_utils

class SprytileGui(bpy.types.Operator):
    bl_idname = "sprytile.gui_win"
    bl_label = "Sprytile GUI"

    # ================
    # Modal functions
    # ================
    @classmethod
    def poll(cls, context):
        return context.area.type == 'VIEW_3D'

    def modal(self, context, event):
        if context.scene.sprytile_data.is_running is False:
            SprytileGui.handler_remove(self, context)
            context.area.tag_redraw()
            return {'CANCELLED'}

        # Check if current_grid is different from current sprytile grid
        if context.object.sprytile_gridid != SprytileGui.current_grid:
            # Setup the offscreen texture for the new grid
            setup_off_return = SprytileGui.setup_offscreen(self, context)
            if setup_off_return is not None:
                return setup_off_return
            # Skip redrawing on this frame
            return {'PASS_THROUGH'}

        self.handle_ui(context, event)

        context.area.tag_redraw()
        return {'PASS_THROUGH'}

    def invoke(self, context, event):
        if context.space_data.type == 'VIEW_3D':
            if context.scene.sprytile_data.is_running is False:
                return {'CANCELLED'}

            # Try to setup offscreen
            setup_off_return = SprytileGui.setup_offscreen(self, context)
            if setup_off_return is not None:
                return setup_off_return

            self.handle_ui(context, event)

            SprytileGui.handler_add(self, context)
            if context.area:
                context.area.tag_redraw()
            context.window_manager.modal_handler_add(self)
            return {'RUNNING_MODAL'}
        else:
            return {'CANCELLED'}

    def handle_ui(self, context, event):
        if event.type in {'LEFTMOUSE', 'MOUSEMOVE'}:
            self.mouse_pt = Vector((event.mouse_region_x, event.mouse_region_y))

        mouse_pt = self.mouse_pt

        region = context.region
        object = context.object

        tilegrid = context.scene.sprytile_grids[object.sprytile_gridid]

        texture = SprytileGui.texture_grid
        tex_size = Vector((texture.size[0], texture.size[1]))

        display_size = 256
        display_pad = 5
        min = Vector((region.width - (display_size + display_pad), display_pad))
        max = Vector((region.width - display_pad, (display_size + display_pad)))

        self.gui_min = min
        self.gui_max = max

        reject_region = context.space_data.type != 'VIEW_3D' or region.type != 'WINDOW'
        if event is None or reject_region:
            return

        if event.type in {'MOUSEMOVE'}:
            mouse_within_x = mouse_pt.x >= min.x and mouse_pt.x <= max.x
            mouse_within_y = mouse_pt.y >= min.y and mouse_pt.y <= max.y
            context.scene.sprytile_data.gui_use_mouse = mouse_within_x and mouse_within_y

        if context.scene.sprytile_data.gui_use_mouse is False:
            # print("Rejecting on event", event.type)
            return

        # if event.type == 'SELECTMOUSE':
        #     print('Select Mouse', event.value)
        if event.type in {'LEFTMOUSE', 'MOUSEMOVE'}:
            click_pos = Vector((mouse_pt.x - min.x, mouse_pt.y - min.y))
            ratio_pos = Vector((click_pos.x / display_size, click_pos.y / display_size))
            tex_pos = Vector((ratio_pos.x * tex_size.x, ratio_pos.y * tex_size.y))
            # Inverse matrix tex_pos
            grid_pos = Vector((tex_pos.x / tilegrid.grid[0], tex_pos.y / tilegrid.grid[1]))
            grid_pos.x = floor(grid_pos.x)
            grid_pos.y = floor(grid_pos.y)
            SprytileGui.cursor_grid_pos = grid_pos

        if event.type == 'LEFTMOUSE' and event.value == 'PRESS':
            tilegrid.tile_selection[0] = grid_pos.x
            tilegrid.tile_selection[1] = grid_pos.y
            print("%d, %d" % grid_pos[:])

    # ==================
    # Actual GUI drawing
    # ==================
    @staticmethod
    def setup_offscreen(self, context):
        SprytileGui.offscreen = SprytileGui.setup_gpu_offscreen(self, context)
        if SprytileGui.offscreen:
            SprytileGui.texture = SprytileGui.offscreen.color_texture
        else:
            self.report({'ERROR'}, "Error initializing offscreen buffer. More details in the console")
            return {'CANCELLED'}
        return None

    @staticmethod
    def setup_gpu_offscreen(self, context):
        import gpu
        scene = context.scene
        object = context.object

        grid_id = object.sprytile_gridid

        # Get the current tile grid, to fetch the texture size to render to
        tilegrid = scene.sprytile_grids[grid_id]
        target_img = sprytile_utils.get_grid_texture(tilegrid)
        # Couldn't get the texture outta here
        if target_img is None:
            SprytileGui.clear_offscreen(self)
            return None

        import gpu
        try:
            offscreen = gpu.offscreen.new(target_img.size[0], target_img.size[1])
        except Exception as e:
            print(e)
            SprytileGui.clear_offscreen(self)
            offscreen = None

        SprytileGui.texture_grid = target_img
        SprytileGui.current_grid = grid_id
        SprytileGui.loaded_grid = tilegrid
        return offscreen

    @staticmethod
    def clear_offscreen(self):
        SprytileGui.texture = None

    @staticmethod
    def handler_add(self, context):
        SprytileGui.draw_callback = bpy.types.SpaceView3D.draw_handler_add(self.draw_callback_handler, (self, context), 'WINDOW', 'POST_PIXEL')

    @staticmethod
    def handler_remove(self, context):
        if SprytileGui.draw_callback is not None:
            bpy.types.SpaceView3D.draw_handler_remove(SprytileGui.draw_callback, 'WINDOW')
        SprytileGui.draw_callback = None

    @staticmethod
    def draw_callback_handler(self, context):
        """Callback handler"""
        SprytileGui.draw_offscreen(self, context)
        SprytileGui.draw_to_viewport(self.gui_min, self.gui_max)

    @staticmethod
    def draw_offscreen(self, context):
        """Draw the GUI into the offscreen texture"""
        offscreen = SprytileGui.offscreen
        target_img = SprytileGui.texture_grid
        size = Vector((target_img.size[0], target_img.size[1]))

        offscreen.bind()
        glDisable(GL_DEPTH_TEST)
        glDisable(GL_LIGHTING)
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(0, size.x, 0, size.y)
        target_img.gl_load(0, GL_NEAREST, GL_NEAREST)
        glBindTexture(GL_TEXTURE_2D, target_img.bindcode[0])
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glEnable(GL_TEXTURE_2D)
        glEnable(GL_BLEND)

        glColor4f(1.0, 1.0, 1.0, 1.0)

        texco = [(0, 0), (0, 1), (1, 1), (1, 0)]
        verco = [(0, 0), (0, size.y), (size.x, size.y), (size.x, 0)]
        glBegin(bgl.GL_QUADS)
        # first draw the texture
        for i in range(4):
            glTexCoord2f(texco[i][0], texco[i][1])
            glVertex2f(verco[i][0], verco[i][1])
        glEnd()

        # Translate the gl context by grid matrix
        glDisable(GL_TEXTURE_2D)
        glLineWidth(2)

        def draw_selection(min, max):
            sel_vtx = [
                (min[0], min[1]),
                (max[0], min[1]),
                (max[0], max[1]),
                (min[0], max[1])
            ]
            glBegin(GL_LINE_STRIP)
            for vtx in sel_vtx:
                glVertex2f(vtx[0], vtx[1])
            glVertex2f(sel_vtx[0][0], sel_vtx[0][1])
            glEnd()

        grid_size = SprytileGui.loaded_grid.grid
        curr_sel = SprytileGui.loaded_grid.tile_selection
        curr_sel_min = (grid_size[0] * curr_sel[0]), (grid_size[1] * curr_sel[1])
        curr_sel_max = [
            (curr_sel_min[0] + grid_size[0] * curr_sel[2]),
            (curr_sel_min[1] + grid_size[1] * curr_sel[3])
        ]
        draw_selection(curr_sel_min, curr_sel_max)

        if context.scene.sprytile_data.gui_use_mouse is True:
            glColor4f(1.0, 0.0, 0.0, 1.0)
            glLineWidth(1)
            cursor_pos = SprytileGui.cursor_grid_pos
            cursor_min = int(cursor_pos.x * grid_size[0]), int(cursor_pos.y * grid_size[1])
            cursor_max = [
                cursor_min[0] + grid_size[0],
                cursor_min[1] + grid_size[1],
            ]
            draw_selection(cursor_min, cursor_max)

        offscreen.unbind()

    @staticmethod
    def draw_to_viewport(min, max):
        """Draw the offscreen texture into the viewport"""
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, SprytileGui.texture)
        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST)
        bgl.glEnable(bgl.GL_TEXTURE_2D)
        bgl.glEnable(bgl.GL_BLEND)

        bgl.glColor4f(1.0, 1.0, 1.0, 1.0)
        bgl.glBegin(bgl.GL_QUADS)

        uv = [(0,0), (0,1), (1,1), (1,0)]
        vtx = [(min.x, min.y), (min.x, max.y), (max.x, max.y), (max.x, min.y)]
        for i in range(4):
            glTexCoord2f(uv[i][0],uv[i][1])
            glVertex2f(vtx[i][0],vtx[i][1])

        bgl.glEnd()

        # restore opengl defaults
        bgl.glLineWidth(1)
        bgl.glDisable(bgl.GL_BLEND)
        bgl.glDisable(bgl.GL_TEXTURE_2D)
        bgl.glColor4f(0.0, 0.0, 0.0, 1.0)

def register():
    bpy.utils.register_module(__name__)

def unregister():
    bpy.utils.unregister_module(__name__)

if __name__ == '__main__':
    register()
<commit_msg>code cleanups to sprytile_gui<commit_after>import bpy
import bgl
import blf
from math import floor
from bgl import *
from bpy_extras import view3d_utils
from mathutils import Vector, Matrix
from . import sprytile_utils

class SprytileGui(bpy.types.Operator):
    bl_idname = "sprytile.gui_win"
    bl_label = "Sprytile GUI"

    # ================
    # Modal functions
    # ================
    @classmethod
    def poll(cls, context):
        return context.area.type == 'VIEW_3D'

    def modal(self, context, event):
        if context.scene.sprytile_data.is_running is False:
            SprytileGui.handler_remove(self, context)
            context.area.tag_redraw()
            return {'CANCELLED'}

        # Check if current_grid is different from current sprytile grid
        if context.object.sprytile_gridid != SprytileGui.current_grid:
            # Setup the offscreen texture for the new grid
            setup_off_return = SprytileGui.setup_offscreen(self, context)
            if setup_off_return is not None:
                return setup_off_return
            # Skip redrawing on this frame
            return {'PASS_THROUGH'}

        self.handle_ui(context, event)

        context.area.tag_redraw()
        return {'PASS_THROUGH'}

    def invoke(self, context, event):
        if context.space_data.type == 'VIEW_3D':
            if context.scene.sprytile_data.is_running is False:
                return {'CANCELLED'}

            # Try to setup offscreen
            setup_off_return = SprytileGui.setup_offscreen(self, context)
            if setup_off_return is not None:
                return setup_off_return

            self.handle_ui(context, event)

            SprytileGui.handler_add(self, context)
            if context.area:
                context.area.tag_redraw()
            context.window_manager.modal_handler_add(self)
            return {'RUNNING_MODAL'}
        else:
            return {'CANCELLED'}

    def handle_ui(self, context, event):
        if event.type in {'LEFTMOUSE', 'MOUSEMOVE'}:
            self.mouse_pt = Vector((event.mouse_region_x, event.mouse_region_y))

        mouse_pt = self.mouse_pt

        region = context.region
        object = context.object

        tilegrid = context.scene.sprytile_grids[object.sprytile_gridid]

        texture = SprytileGui.texture_grid
        tex_size = Vector((texture.size[0], texture.size[1]))

        display_size = 256
        display_pad = 5
        gui_min = Vector((region.width - (display_size + display_pad), display_pad))
        gui_max = Vector((region.width - display_pad, (display_size + display_pad)))

        self.gui_min = gui_min
        self.gui_max = gui_max

        reject_region = context.space_data.type != 'VIEW_3D' or region.type != 'WINDOW'
        if event is None or reject_region:
            return

        if event.type in {'MOUSEMOVE'}:
            mouse_within_x = gui_min.x <= mouse_pt.x <= gui_max.x
            mouse_within_y = gui_min.y <= mouse_pt.y <= gui_max.y
            context.scene.sprytile_data.gui_use_mouse = mouse_within_x and mouse_within_y

        if context.scene.sprytile_data.gui_use_mouse is False:
            return

        if event.type in {'LEFTMOUSE', 'MOUSEMOVE'}:
            click_pos = Vector((mouse_pt.x - gui_min.x, mouse_pt.y - gui_min.y))
            ratio_pos = Vector((click_pos.x / display_size, click_pos.y / display_size))
            tex_pos = Vector((ratio_pos.x * tex_size.x, ratio_pos.y * tex_size.y))
            # Inverse matrix tex_pos
            grid_pos = Vector((tex_pos.x / tilegrid.grid[0], tex_pos.y / tilegrid.grid[1]))
            grid_pos.x = floor(grid_pos.x)
            grid_pos.y = floor(grid_pos.y)
            SprytileGui.cursor_grid_pos = grid_pos

        if event.type == 'LEFTMOUSE' and event.value == 'PRESS':
            tilegrid.tile_selection[0] = grid_pos.x
            tilegrid.tile_selection[1] = grid_pos.y
            print("%d, %d" % grid_pos[:])

    # ==================
    # Actual GUI drawing
    # ==================
    @staticmethod
    def setup_offscreen(self, context):
        SprytileGui.offscreen = SprytileGui.setup_gpu_offscreen(self, context)
        if SprytileGui.offscreen:
            SprytileGui.texture = SprytileGui.offscreen.color_texture
        else:
            self.report({'ERROR'}, "Error initializing offscreen buffer. More details in the console")
            return {'CANCELLED'}
        return None

    @staticmethod
    def setup_gpu_offscreen(self, context):
        import gpu
        scene = context.scene
        obj = context.object

        grid_id = obj.sprytile_gridid

        # Get the current tile grid, to fetch the texture size to render to
        tilegrid = scene.sprytile_grids[grid_id]
        target_img = sprytile_utils.get_grid_texture(tilegrid)
        # Couldn't get the texture outta here
        if target_img is None:
            SprytileGui.clear_offscreen(self)
            return None

        import gpu
        try:
            offscreen = gpu.offscreen.new(target_img.size[0], target_img.size[1])
        except Exception as e:
            print(e)
            SprytileGui.clear_offscreen(self)
            offscreen = None

        SprytileGui.texture_grid = target_img
        SprytileGui.current_grid = grid_id
        SprytileGui.loaded_grid = tilegrid
        return offscreen

    @staticmethod
    def clear_offscreen(self):
        SprytileGui.texture = None

    @staticmethod
    def handler_add(self, context):
        SprytileGui.draw_callback = bpy.types.SpaceView3D.draw_handler_add(self.draw_callback_handler, (self, context),
                                                                           'WINDOW', 'POST_PIXEL')

    @staticmethod
    def handler_remove(self, context):
        if SprytileGui.draw_callback is not None:
            bpy.types.SpaceView3D.draw_handler_remove(SprytileGui.draw_callback, 'WINDOW')
        SprytileGui.draw_callback = None

    @staticmethod
    def draw_callback_handler(self, context):
        """Callback handler"""
        SprytileGui.draw_offscreen(self, context)
        SprytileGui.draw_to_viewport(self.gui_min, self.gui_max)

    @staticmethod
    def draw_offscreen(self, context):
        """Draw the GUI into the offscreen texture"""
        offscreen = SprytileGui.offscreen
        target_img = SprytileGui.texture_grid
        size = Vector((target_img.size[0], target_img.size[1]))

        offscreen.bind()
        glDisable(GL_DEPTH_TEST)
        glDisable(GL_LIGHTING)
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(0, size.x, 0, size.y)
        target_img.gl_load(0, GL_NEAREST, GL_NEAREST)
        glBindTexture(GL_TEXTURE_2D, target_img.bindcode[0])
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glEnable(GL_TEXTURE_2D)
        glEnable(GL_BLEND)

        glColor4f(1.0, 1.0, 1.0, 1.0)

        texco = [(0, 0), (0, 1), (1, 1), (1, 0)]
        verco = [(0, 0), (0, size.y), (size.x, size.y), (size.x, 0)]
        glBegin(bgl.GL_QUADS)
        # first draw the texture
        for i in range(4):
            glTexCoord2f(texco[i][0], texco[i][1])
            glVertex2f(verco[i][0], verco[i][1])
        glEnd()

        # Translate the gl context by grid matrix
        glDisable(GL_TEXTURE_2D)
        glLineWidth(2)

        def draw_selection(min, max):
            sel_vtx = [
                (min[0], min[1]),
                (max[0], min[1]),
                (max[0], max[1]),
                (min[0], max[1])
            ]
            glBegin(GL_LINE_STRIP)
            for vtx in sel_vtx:
                glVertex2f(vtx[0], vtx[1])
            glVertex2f(sel_vtx[0][0], sel_vtx[0][1])
            glEnd()

        grid_size = SprytileGui.loaded_grid.grid
        curr_sel = SprytileGui.loaded_grid.tile_selection
        curr_sel_min = (grid_size[0] * curr_sel[0]), (grid_size[1] * curr_sel[1])
        curr_sel_max = [
            (curr_sel_min[0] + grid_size[0] * curr_sel[2]),
            (curr_sel_min[1] + grid_size[1] * curr_sel[3])
        ]
        draw_selection(curr_sel_min, curr_sel_max)

        if context.scene.sprytile_data.gui_use_mouse is True:
            glColor4f(1.0, 0.0, 0.0, 1.0)
            glLineWidth(1)
            cursor_pos = SprytileGui.cursor_grid_pos
            cursor_min = int(cursor_pos.x * grid_size[0]), int(cursor_pos.y * grid_size[1])
            cursor_max = [
                cursor_min[0] + grid_size[0],
                cursor_min[1] + grid_size[1],
            ]
            draw_selection(cursor_min, cursor_max)

        offscreen.unbind()

    @staticmethod
    def draw_to_viewport(min, max):
        """Draw the offscreen texture into the viewport"""
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, SprytileGui.texture)
        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST)
        bgl.glEnable(bgl.GL_TEXTURE_2D)
        bgl.glEnable(bgl.GL_BLEND)

        bgl.glColor4f(1.0, 1.0, 1.0, 1.0)
        bgl.glBegin(bgl.GL_QUADS)

        uv = [(0, 0), (0, 1), (1, 1), (1, 0)]
        vtx = [(min.x, min.y), (min.x, max.y), (max.x, max.y), (max.x, min.y)]
        for i in range(4):
            glTexCoord2f(uv[i][0], uv[i][1])
            glVertex2f(vtx[i][0], vtx[i][1])

        bgl.glEnd()

        # restore opengl defaults
        bgl.glLineWidth(1)
        bgl.glDisable(bgl.GL_BLEND)
        bgl.glDisable(bgl.GL_TEXTURE_2D)
        bgl.glColor4f(0.0, 0.0, 0.0, 1.0)

def register():
    bpy.utils.register_module(__name__)

def unregister():
    bpy.utils.unregister_module(__name__)

if __name__ == '__main__':
    register()
<|endoftext|>


<commit_before>'''Test requests' interaction with vcr'''

# coding=utf-8

# Internal imports
import vcr
from .common import TestVCR

import os
import pytest
requests = pytest.importorskip("requests")

class TestRequestsBase(TestVCR):
    '''Some utility for running Requests tests'''
    fixtures = os.path.join('tests', 'fixtures', 'requests')

class TestHTTPRequests(TestRequestsBase):
    '''Some tests using requests and http'''
    scheme = 'http'

    def test_status_code(self):
        '''Ensure that we can read the status code'''
        url = self.scheme + '://httpbin.org/'
        with vcr.use_cassette(self.fixture('atts.yaml')) as cass:
            # Ensure that this is empty to begin with
            self.assertEqual(len(cass), 0)
            self.assertEqual(len(cass.cached()), 0)
            self.assertEqual(
                requests.get(url).status_code,
                requests.get(url).status_code)
            # Ensure that we've now cached a single response
            self.assertEqual(len(cass), 1)
            self.assertEqual(len(cass.cached()), 1)

    def test_headers(self):
        '''Ensure that we can read the headers back'''
        url = self.scheme + '://httpbin.org/'
        with vcr.use_cassette(self.fixture('headers.yaml')) as cass:
            # Ensure that this is empty to begin with
            self.assertEqual(len(cass), 0)
            self.assertEqual(len(cass.cached()), 0)
            self.assertEqual(
                requests.get(url).headers,
                requests.get(url).headers)
            # Ensure that we've now cached a single response
            self.assertEqual(len(cass), 1)
            self.assertEqual(len(cass.cached()), 1)

    def test_body(self):
        '''Ensure the responses are all identical enough'''
        url = self.scheme + '://httpbin.org/bytes/1024'
        with vcr.use_cassette(self.fixture('body.yaml')) as cass:
            # Ensure that this is empty to begin with
            self.assertEqual(len(cass), 0)
            self.assertEqual(len(cass.cached()), 0)
            self.assertEqual(
                requests.get(url).content,
                requests.get(url).content)
            # Ensure that we've now cached a single response
            self.assertEqual(len(cass), 1)
            self.assertEqual(len(cass.cached()), 1)

    def test_auth(self):
        '''Ensure that we can handle basic auth'''
        auth = ('user', 'passwd')
        url = self.scheme + '://httpbin.org/basic-auth/user/passwd'
        with vcr.use_cassette(self.fixture('auth.yaml')) as cass:
            # Ensure that this is empty to begin with
            self.assertEqual(len(cass), 0)
            self.assertEqual(len(cass.cached()), 0)
            one = requests.get(url, auth=auth)
            two = requests.get(url, auth=auth)
            self.assertEqual(one.content, two.content)
            self.assertEqual(one.status_code, two.status_code)
            # Ensure that we've now cached a single response
            self.assertEqual(len(cass), 1)
            self.assertEqual(len(cass.cached()), 1)

    def test_auth_failed(self):
        '''Ensure that we can save failed auth statuses'''
        auth = ('user', 'wrongwrongwrong')
        url = self.scheme + '://httpbin.org/basic-auth/user/passwd'
        with vcr.use_cassette(self.fixture('auth-failed.yaml')) as cass:
            # Ensure that this is empty to begin with
            self.assertEqual(len(cass), 0)
            self.assertEqual(len(cass.cached()), 0)
            one = requests.get(url, auth=auth)
            two = requests.get(url, auth=auth)
            self.assertEqual(one.content, two.content)
            self.assertEqual(one.status_code, two.status_code)
            self.assertNotEqual(one.status_code, 200)
            # Ensure that we've now cached a single response
            self.assertEqual(len(cass), 1)
            self.assertEqual(len(cass.cached()), 1)

    def test_post(self):
        '''Ensure that we can post and cache the results'''
        data = {'key1': 'value1', 'key2': 'value2'}
        url = self.scheme + '://httpbin.org/post'
        with vcr.use_cassette(self.fixture('redirect.yaml')) as cass:
            # Ensure that this is empty to begin with
            self.assertEqual(len(cass), 0)
            self.assertEqual(len(cass.cached()), 0)
            self.assertEqual(
                requests.post(url, data).content,
                requests.post(url, data).content)
            # Ensure that we've now cached a single response
            self.assertEqual(len(cass), 1)
            self.assertEqual(len(cass.cached()), 1)

    def test_redirects(self):
        '''Ensure that we can handle redirects'''
        url = self.scheme + '://httpbin.org/redirect-to?url=bytes/1024'
        with vcr.use_cassette(self.fixture('redirect.yaml')) as cass:
            # Ensure that this is empty to begin with
            self.assertEqual(len(cass), 0)
            self.assertEqual(len(cass.cached()), 0)
            self.assertEqual(
                requests.get(url).content,
                requests.get(url).content)
            # Ensure that we've now cached /two/ responses. One for the redirect
            # and one for the final fetch
            self.assertEqual(len(cass), 2)
            self.assertEqual(len(cass.cached()), 2)

class TestHTTPSRequests(TestHTTPRequests):
    '''Same as above, now in https'''
    scheme = 'https'

    def test_cross_scheme(self):
        '''Ensure that requests between schemes are treated separately'''
        # First fetch a url under https, and then again under https and then
        # ensure that we haven't served anything out of cache, and we have two
        # requests / response pairs in the cassette
        with vcr.use_cassette(self.fixture('cross_scheme.yaml')) as cass:
            requests.get('https://httpbin.org/')
            requests.get('http://httpbin.org/')
            self.assertEqual(len(cass), 2)
            self.assertEqual(len(cass.cached()), 0)

class TestWild(TestRequestsBase):
    '''Test some examples from the wild'''
    fixtures = os.path.join('tests', 'fixtures', 'wild')

    def tearDown(self):
        # No deleting our directory, and ensure that it exists
        self.assertTrue(os.path.exists(self.fixture()))

    def test_domain_redirect(self):
        '''Ensure that redirects across domains are considered unique'''
        # In this example, seomoz.org redirects to moz.com, and if those
        # requests are considered identical, then we'll be stuck in a redirect
        # loop.
        url = 'http://seomoz.org/'
        with vcr.use_cassette(self.fixture('domain_redirect.yaml')) as cass:
            requests.get(url, headers={'User-Agent': 'vcrpy-test'})
            # Ensure that we've now served two responses. One for the original
            # redirect, and a second for the actual fetch
            self.assertEqual(len(cass.cached()), 2)
<commit_msg>fix typo<commit_after>'''Test requests' interaction with vcr'''

# coding=utf-8

# Internal imports
import vcr
from .common import TestVCR

import os
import pytest
requests = pytest.importorskip("requests")

class TestRequestsBase(TestVCR):
    '''Some utility for running Requests tests'''
    fixtures = os.path.join('tests', 'fixtures', 'requests')

class TestHTTPRequests(TestRequestsBase):
    '''Some tests using requests and http'''
    scheme = 'http'

    def test_status_code(self):
        '''Ensure that we can read the status code'''
        url = self.scheme + '://httpbin.org/'
        with vcr.use_cassette(self.fixture('atts.yaml')) as cass:
            # Ensure that this is empty to begin with
            self.assertEqual(len(cass), 0)
            self.assertEqual(len(cass.cached()), 0)
            self.assertEqual(
                requests.get(url).status_code,
                requests.get(url).status_code)
            # Ensure that we've now cached a single response
            self.assertEqual(len(cass), 1)
            self.assertEqual(len(cass.cached()), 1)

    def test_headers(self):
        '''Ensure that we can read the headers back'''
        url = self.scheme + '://httpbin.org/'
        with vcr.use_cassette(self.fixture('headers.yaml')) as cass:
            # Ensure that this is empty to begin with
            self.assertEqual(len(cass), 0)
            self.assertEqual(len(cass.cached()), 0)
            self.assertEqual(
                requests.get(url).headers,
                requests.get(url).headers)
            # Ensure that we've now cached a single response
            self.assertEqual(len(cass), 1)
            self.assertEqual(len(cass.cached()), 1)

    def test_body(self):
        '''Ensure the responses are all identical enough'''
        url = self.scheme + '://httpbin.org/bytes/1024'
        with vcr.use_cassette(self.fixture('body.yaml')) as cass:
            # Ensure that this is empty to begin with
            self.assertEqual(len(cass), 0)
            self.assertEqual(len(cass.cached()), 0)
            self.assertEqual(
                requests.get(url).content,
                requests.get(url).content)
            # Ensure that we've now cached a single response
            self.assertEqual(len(cass), 1)
            self.assertEqual(len(cass.cached()), 1)

    def test_auth(self):
        '''Ensure that we can handle basic auth'''
        auth = ('user', 'passwd')
        url = self.scheme + '://httpbin.org/basic-auth/user/passwd'
        with vcr.use_cassette(self.fixture('auth.yaml')) as cass:
            # Ensure that this is empty to begin with
            self.assertEqual(len(cass), 0)
            self.assertEqual(len(cass.cached()), 0)
            one = requests.get(url, auth=auth)
            two = requests.get(url, auth=auth)
            self.assertEqual(one.content, two.content)
            self.assertEqual(one.status_code, two.status_code)
            # Ensure that we've now cached a single response
            self.assertEqual(len(cass), 1)
            self.assertEqual(len(cass.cached()), 1)

    def test_auth_failed(self):
        '''Ensure that we can save failed auth statuses'''
        auth = ('user', 'wrongwrongwrong')
        url = self.scheme + '://httpbin.org/basic-auth/user/passwd'
        with vcr.use_cassette(self.fixture('auth-failed.yaml')) as cass:
            # Ensure that this is empty to begin with
            self.assertEqual(len(cass), 0)
            self.assertEqual(len(cass.cached()), 0)
            one = requests.get(url, auth=auth)
            two = requests.get(url, auth=auth)
            self.assertEqual(one.content, two.content)
            self.assertEqual(one.status_code, two.status_code)
            self.assertNotEqual(one.status_code, 200)
            # Ensure that we've now cached a single response
            self.assertEqual(len(cass), 1)
            self.assertEqual(len(cass.cached()), 1)

    def test_post(self):
        '''Ensure that we can post and cache the results'''
        data = {'key1': 'value1', 'key2': 'value2'}
        url = self.scheme + '://httpbin.org/post'
        with vcr.use_cassette(self.fixture('redirect.yaml')) as cass:
            # Ensure that this is empty to begin with
            self.assertEqual(len(cass), 0)
            self.assertEqual(len(cass.cached()), 0)
            self.assertEqual(
                requests.post(url, data).content,
                requests.post(url, data).content)
            # Ensure that we've now cached a single response
            self.assertEqual(len(cass), 1)
            self.assertEqual(len(cass.cached()), 1)

    def test_redirects(self):
        '''Ensure that we can handle redirects'''
        url = self.scheme + '://httpbin.org/redirect-to?url=bytes/1024'
        with vcr.use_cassette(self.fixture('redirect.yaml')) as cass:
            # Ensure that this is empty to begin with
            self.assertEqual(len(cass), 0)
            self.assertEqual(len(cass.cached()), 0)
            self.assertEqual(
                requests.get(url).content,
                requests.get(url).content)
            # Ensure that we've now cached /two/ responses. One for the redirect
            # and one for the final fetch
            self.assertEqual(len(cass), 2)
            self.assertEqual(len(cass.cached()), 2)

class TestHTTPSRequests(TestHTTPRequests):
    '''Same as above, now in https'''
    scheme = 'https'

    def test_cross_scheme(self):
        '''Ensure that requests between schemes are treated separately'''
        # First fetch a url under http, and then again under https and then
        # ensure that we haven't served anything out of cache, and we have two
        # requests / response pairs in the cassette
        with vcr.use_cassette(self.fixture('cross_scheme.yaml')) as cass:
            requests.get('https://httpbin.org/')
            requests.get('http://httpbin.org/')
            self.assertEqual(len(cass), 2)
            self.assertEqual(len(cass.cached()), 0)

class TestWild(TestRequestsBase):
    '''Test some examples from the wild'''
    fixtures = os.path.join('tests', 'fixtures', 'wild')

    def tearDown(self):
        # No deleting our directory, and ensure that it exists
        self.assertTrue(os.path.exists(self.fixture()))

    def test_domain_redirect(self):
        '''Ensure that redirects across domains are considered unique'''
        # In this example, seomoz.org redirects to moz.com, and if those
        # requests are considered identical, then we'll be stuck in a redirect
        # loop.
        url = 'http://seomoz.org/'
        with vcr.use_cassette(self.fixture('domain_redirect.yaml')) as cass:
            requests.get(url, headers={'User-Agent': 'vcrpy-test'})
            # Ensure that we've now served two responses. One for the original
            # redirect, and a second for the actual fetch
            self.assertEqual(len(cass.cached()), 2)
<|endoftext|>


<commit_before>"""
Testing signals before/after saving and deleting.
"""

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=20)

    def __unicode__(self):
        return u"%s %s" % (self.first_name, self.last_name)

def pre_save_test(signal, sender, instance, **kwargs):
    print 'pre_save signal,', instance
    if kwargs.get('raw'):
        print 'Is raw'

def post_save_test(signal, sender, instance, **kwargs):
    print 'post_save signal,', instance
    if 'created' in kwargs:
        if kwargs['created']:
            print 'Is created'
        else:
            print 'Is updated'
    if kwargs.get('raw'):
        print 'Is raw'

def pre_delete_test(signal, sender, instance, **kwargs):
    print 'pre_delete signal,', instance
    print 'instance.id is not None: %s' % (instance.id != None)

# #8285: signals can be any callable
class PostDeleteHandler(object):
    def __call__(self, signal, sender, instance, **kwargs):
        print 'post_delete signal,', instance
        print 'instance.id is None: %s' % (instance.id == None)

post_delete_test = PostDeleteHandler()

__test__ = {'API_TESTS':"""
>>> models.signals.pre_save.connect(pre_save_test)
>>> models.signals.post_save.connect(post_save_test)
>>> models.signals.pre_delete.connect(pre_delete_test)
>>> models.signals.post_delete.connect(post_delete_test)

>>> p1 = Person(first_name='John', last_name='Smith')
>>> p1.save()
pre_save signal, John Smith
post_save signal, John Smith
Is created

>>> p1.first_name = 'Tom'
>>> p1.save()
pre_save signal, Tom Smith
post_save signal, Tom Smith
Is updated

# Calling an internal method purely so that we can trigger a "raw" save.
>>> p1.save_base(raw=True)
pre_save signal, Tom Smith
Is raw
post_save signal, Tom Smith
Is updated
Is raw

>>> p1.delete()
pre_delete signal, Tom Smith
instance.id is not None: True
post_delete signal, Tom Smith
instance.id is None: False

>>> p2 = Person(first_name='James', last_name='Jones')
>>> p2.id = 99999
>>> p2.save()
pre_save signal, James Jones
post_save signal, James Jones
Is created

>>> p2.id = 99998
>>> p2.save()
pre_save signal, James Jones
post_save signal, James Jones
Is created

>>> p2.delete()
pre_delete signal, James Jones
instance.id is not None: True
post_delete signal, James Jones
instance.id is None: False

>>> Person.objects.all()
[<Person: James Jones>]

>>> models.signals.post_delete.disconnect(post_delete_test)
>>> models.signals.pre_delete.disconnect(pre_delete_test)
>>> models.signals.post_save.disconnect(post_save_test)
>>> models.signals.pre_save.disconnect(pre_save_test)

# Make sure all the signals got removed properly (#9989)
>>> models.signals.post_delete.receivers
[]
>>> models.signals.pre_delete.receivers
[]
>>> models.signals.post_save.receivers
[]
>>> models.signals.pre_save.receivers
[]

"""}
<commit_msg>Fixed the test case from [10399] to be more robust against the order in which test cases are executed.<commit_after>"""
Testing signals before/after saving and deleting.
"""

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=20)

    def __unicode__(self):
        return u"%s %s" % (self.first_name, self.last_name)

def pre_save_test(signal, sender, instance, **kwargs):
    print 'pre_save signal,', instance
    if kwargs.get('raw'):
        print 'Is raw'

def post_save_test(signal, sender, instance, **kwargs):
    print 'post_save signal,', instance
    if 'created' in kwargs:
        if kwargs['created']:
            print 'Is created'
        else:
            print 'Is updated'
    if kwargs.get('raw'):
        print 'Is raw'

def pre_delete_test(signal, sender, instance, **kwargs):
    print 'pre_delete signal,', instance
    print 'instance.id is not None: %s' % (instance.id != None)

# #8285: signals can be any callable
class PostDeleteHandler(object):
    def __call__(self, signal, sender, instance, **kwargs):
        print 'post_delete signal,', instance
        print 'instance.id is None: %s' % (instance.id == None)

post_delete_test = PostDeleteHandler()

__test__ = {'API_TESTS':"""

# Save up the number of connected signals so that we can check at the end
# that all the signals we register get properly unregistered (#9989)
>>> pre_signals = (len(models.signals.pre_save.receivers),
...                len(models.signals.post_save.receivers),
...                len(models.signals.pre_delete.receivers),
...                len(models.signals.post_delete.receivers))

>>> models.signals.pre_save.connect(pre_save_test)
>>> models.signals.post_save.connect(post_save_test)
>>> models.signals.pre_delete.connect(pre_delete_test)
>>> models.signals.post_delete.connect(post_delete_test)

>>> p1 = Person(first_name='John', last_name='Smith')
>>> p1.save()
pre_save signal, John Smith
post_save signal, John Smith
Is created

>>> p1.first_name = 'Tom'
>>> p1.save()
pre_save signal, Tom Smith
post_save signal, Tom Smith
Is updated

# Calling an internal method purely so that we can trigger a "raw" save.
>>> p1.save_base(raw=True)
pre_save signal, Tom Smith
Is raw
post_save signal, Tom Smith
Is updated
Is raw

>>> p1.delete()
pre_delete signal, Tom Smith
instance.id is not None: True
post_delete signal, Tom Smith
instance.id is None: False

>>> p2 = Person(first_name='James', last_name='Jones')
>>> p2.id = 99999
>>> p2.save()
pre_save signal, James Jones
post_save signal, James Jones
Is created

>>> p2.id = 99998
>>> p2.save()
pre_save signal, James Jones
post_save signal, James Jones
Is created

>>> p2.delete()
pre_delete signal, James Jones
instance.id is not None: True
post_delete signal, James Jones
instance.id is None: False

>>> Person.objects.all()
[<Person: James Jones>]

>>> models.signals.post_delete.disconnect(post_delete_test)
>>> models.signals.pre_delete.disconnect(pre_delete_test)
>>> models.signals.post_save.disconnect(post_save_test)
>>> models.signals.pre_save.disconnect(pre_save_test)

# Check that all our signals got disconnected properly.
>>> post_signals = (len(models.signals.pre_save.receivers),
...                 len(models.signals.post_save.receivers),
...                 len(models.signals.pre_delete.receivers),
...                 len(models.signals.post_delete.receivers))

>>> pre_signals == post_signals
True

"""}
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

from flask import Flask

app = Flask(__name__, instance_relative_config=True)

from spz import views
from spz import errorhandlers

# vim: set tabstop=4 shiftwidth=4 expandtab:
<commit_msg>Customized behavior by inheriting from the flask class (whitespace and newline control, reloading)<commit_after># -*- coding: utf-8 -*-

from flask import Flask

# provides custom options by inheriting from the flask class
class CustomFlask(Flask):
    jinja_options = dict(Flask.jinja_options, trim_blocks=True, lstrip_blocks=True, auto_reload=False)

app = CustomFlask(__name__, instance_relative_config=True)

app.config.from_pyfile('development.cfg')
#app.config.from_pyfile('production.cfg')

# register all views here
from spz import views
from spz import errorhandlers

# vim: set tabstop=4 shiftwidth=4 expandtab:
<|endoftext|>


<commit_before>import os

from corpus_browser.settings import PROJECT_ROOT
from indexer.models import AuxiliaryIndex
from utils.tests import MongoTestCase

class TestIndexQuerySet(MongoTestCase):

    def setUp(self):
        fixture_file = os.path.join(PROJECT_ROOT, 'indexer/fixtures/testindex.json')
        fixture = open(fixture_file).read()
        AuxiliaryIndex.load_data(fixture)
        self.token1 = u"\u0648\u0627\u0639\u062f"
        self.token2 = u"\u0647\u064a\u062a\u062c\u0646\u0646\u0648\u0627"
        self.token3 = u"\u0648\u0627\u0642\u0641\u0629"

    def test_intersect(self):
        result = AuxiliaryIndex.objects.intersect(
            token__in=[self.token1, self.token2]
        )
        t1 = AuxiliaryIndex.objects.get(token=self.token1)
        t2 = AuxiliaryIndex.objects.get(token=self.token2)
        for posting in result:
            self.assertTrue(posting.document in map(lambda x:x.document, t1.postings))
            self.assertTrue(posting.document in map(lambda x:x.document, t2.postings))

            p1 = [p for p in t1.postings if posting.document == p.document][0].positions
            p2 = [p for p in t2.postings if posting.document == p.document][0].positions

            self.assertTrue((self.token1, p1) in posting.positions)
            self.assertTrue((self.token2, p2) in posting.positions)

        result2 = AuxiliaryIndex.objects.intersect(
            token__in=[self.token1, self.token3]
        )
        self.assertFalse(bool(result2))

    def test_proximity(self):
        result = AuxiliaryIndex.objects.proximity(
            window=5,
            token__in=[self.token1, self.token2]
        )
        t1 = AuxiliaryIndex.objects.get(token=self.token1)
        t2 = AuxiliaryIndex.objects.get(token=self.token2)
        for posting in result:
            self.assertTrue(posting.document in map(lambda x:x.document, t1.postings))
            self.assertTrue(posting.document in map(lambda x:x.document, t2.postings))

            p1 = [p for p in t1.postings if posting.document == p.document][0].positions
            p2 = [p for p in t2.postings if posting.document == p.document][0].positions

            self.assertTrue((self.token1, p1) in posting.positions)
            self.assertTrue((self.token2, p2) in posting.positions)

        result2 = AuxiliaryIndex.objects.proximity(
            window=3,
            token__in=[self.token1, self.token2]
        )
        self.assertFalse(bool(result2))

        result3 = AuxiliaryIndex.objects.intersect(
            token__in=[self.token1, self.token3]
        )
        self.assertFalse(bool(result3))

    def test_consequent(self):
        # TODO:
        pass

    def test_frequency(self):
        pass

<commit_msg>frequency setQuery method tests<commit_after>#!/usr/bin/python
# -*- coding:UTF-8 -*-

import os

from corpus_browser.settings import PROJECT_ROOT
from indexer.models import AuxiliaryIndex
from utils.tests import MongoTestCase

class TestIndexQuerySet(MongoTestCase):

    def setUp(self):
        fixture_file = os.path.join(PROJECT_ROOT, 'indexer/fixtures/testindex.json')
        fixture = open(fixture_file).read()
        AuxiliaryIndex.load_data(fixture)
        self.token1 = u"\u0648\u0627\u0639\u062f"
        self.token2 = u"\u0647\u064a\u062a\u062c\u0646\u0646\u0648\u0627"
        self.token3 = u"\u0648\u0627\u0642\u0641\u0629"

    def test_intersect(self):
        result = AuxiliaryIndex.objects.intersect(
            token__in=[self.token1, self.token2]
        )
        t1 = AuxiliaryIndex.objects.get(token=self.token1)
        t2 = AuxiliaryIndex.objects.get(token=self.token2)
        for posting in result:
            self.assertTrue(posting.document in map(lambda x:x.document, t1.postings))
            self.assertTrue(posting.document in map(lambda x:x.document, t2.postings))

            p1 = [p for p in t1.postings if posting.document == p.document][0].positions
            p2 = [p for p in t2.postings if posting.document == p.document][0].positions

            self.assertTrue((self.token1, p1) in posting.positions)
            self.assertTrue((self.token2, p2) in posting.positions)

        result2 = AuxiliaryIndex.objects.intersect(
            token__in=[self.token1, self.token3]
        )
        self.assertFalse(bool(result2))

    def test_proximity(self):
        result = AuxiliaryIndex.objects.proximity(
            window=5,
            token__in=[self.token1, self.token2]
        )
        t1 = AuxiliaryIndex.objects.get(token=self.token1)
        t2 = AuxiliaryIndex.objects.get(token=self.token2)
        for posting in result:
            self.assertTrue(posting.document in map(lambda x:x.document, t1.postings))
            self.assertTrue(posting.document in map(lambda x:x.document, t2.postings))

            p1 = [p for p in t1.postings if posting.document == p.document][0].positions
            p2 = [p for p in t2.postings if posting.document == p.document][0].positions

            self.assertTrue((self.token1, p1) in posting.positions)
            self.assertTrue((self.token2, p2) in posting.positions)

        result2 = AuxiliaryIndex.objects.proximity(
            window=3,
            token__in=[self.token1, self.token2]
        )
        self.assertFalse(bool(result2))

        result3 = AuxiliaryIndex.objects.intersect(
            token__in=[self.token1, self.token3]
        )
        self.assertFalse(bool(result3))

    def test_consequent(self):
        # TODO:
        pass

    def test_frequency(self):
        freq1 = AuxiliaryIndex.objects.frequency(u'الله')
        self.assertEqual(3, freq1)

        freq2 = AuxiliaryIndex.objects.frequency(u'الله', u'كبير', window=0)
        self.assertEqual(1, freq2)

        freq3 = AuxiliaryIndex.objects.frequency(u'مصر', u'الإخوان')
        self.assertEqual(0, freq3)
        freq3 = AuxiliaryIndex.objects.frequency((u'مصر', u'الإخوان'), u'حرق', window=0)
        self.assertEqual(1, freq3)
        freq4 = AuxiliaryIndex.objects.frequency(u'مصر', u'الإخوان', window=0)
        self.assertEqual(3, freq4)
        freq5 = AuxiliaryIndex.objects.frequency(u'مصر', u'الإخوان', window=5)
        self.assertEqual(1, freq5)
        freq6 = AuxiliaryIndex.objects.frequency(u'مصر', u'الإخوان', window=3)
        self.assertEqual(0, freq6)

<|endoftext|>


<commit_before>from neutronclient.common.exceptions import NeutronException
from novaclient.exceptions import NotFound

from cosmo_tester.framework.util import YamlPatcher

__author__ = 'elip'

from cosmo_tester.framework.testenv import TestCase
from cosmo_tester.framework.util import get_yaml_as_dict
from cosmo_tester.framework.openstack_api import openstack_clients

import requests

class HelloWorldBashTest(TestCase):

    CLOUDIFY_EXAMPLES_URL = "https://github.com/cloudify-cosmo/cloudify-examples.git"

    host_name = 'bash-web-server'
    flavor = 102
    image = '8c096c29-a666-4b82-99c4-c77dc70cfb40'
    security_groups = ['webserver_security_group']
    virtual_ip_node_id = 'virtual_ip'
    server_node_id = 'vm'
    security_group_node_id = 'security_group'

    repo_dir = None

    def setUp(self):
        super(HelloWorldBashTest, self).setUp()
        from cosmo_tester.framework.git_helper import clone
        self.repo_dir = clone(self.CLOUDIFY_EXAMPLES_URL, self.workdir)

    def test_hello_world_bash(self):

        # Add agents security group from the cloudify-config file used to bootstrap
        self.security_groups.append(self.env.agents_security_group)

        self.blueprint_path = self.repo_dir / 'hello-world'
        self.blueprint_yaml = self.blueprint_path / 'blueprint.yaml'

        blueprint = get_yaml_as_dict(self.blueprint_yaml)

        self.modify_yaml(self.blueprint_yaml)

        # Upload --> Create Deployment --> Execute Install
        before, after = self.upload_deploy_and_execute_install()

        manager_state_delta = self.get_manager_state_delta(before, after)
        nodes_state = manager_state_delta['node_state'].values()[0]

        server_node = None
        security_group_node = None
        floatingip_node = None
        for key, value in nodes_state.items():
            if key.startswith(self.virtual_ip_node_id):
                floatingip_node = value
            if key.startswith(self.security_group_node_id):
                security_group_node = value
            if key.startswith(self.server_node_id):
                server_node = value

        webserver_port = blueprint['blueprint']['nodes'][3]['properties']['port']
        web_server_page_response = \
            requests.get('http://{0}:{1}'.format(
                floatingip_node['runtimeInfo']['floating_ip_address'],
                webserver_port))

        self.assertEqual(200, web_server_page_response.status_code)

        nova, neutron = openstack_clients(self.env.cloudify_config)

        self.logger.info("Retrieving agent server : {0}"
            .format(nova.servers.get(server_node['runtimeInfo']['openstack_server_id'])))
        self.logger.info("Retrieving agent floating ip : {0}"
            .format(neutron.show_floatingip(floatingip_node['runtimeInfo']['external_id'])))
        self.logger.info("Retrieving agent security group : {0}"
            .format(neutron.show_security_group(security_group_node['runtimeInfo']['external_id'])))

        self.execute_uninstall()

        # No components should exist after uninstall

        try:
            server = nova.servers.get(server_node['runtimeInfo']['openstack_server_id'])
            self.fail("Expected agent machine to be terminated. but found : {0}"
                .format(server))
        except NotFound as e:
            self.logger.info(e)
            pass

        try:
            floatingip = neutron.show_floatingip(floatingip_node['runtimeInfo']['external_id'])
            self.fail("Expected agent floating ip to be terminated. but found : {0}"
                .format(floatingip))
        except NeutronException as e:
            self.logger.info(e)
            pass

        try:
            security_group = neutron.show_security_group(security_group_node['runtimeInfo']['external_id'])
            self.fail("Expected webserver security group ip to be terminated. but found : {0}"
                .format(security_group))
        except NeutronException as e:
            self.logger.info(e)
            pass

    def modify_yaml(self, yaml_file):
        with YamlPatcher(yaml_file) as patch:
            vm_properties_path = 'blueprint.nodes[2].properties'
            patch.set_value('{0}.management_network_name'.format(vm_properties_path),
                            self.env.management_network_name)
            patch.set_value('{0}.worker_config.key'.format(vm_properties_path),
                            self.env.agent_key_path)
            patch.merge_obj('{0}.server'.format(vm_properties_path), {
                'name': self.host_name,
                'image': self.image,
                'flavor': self.flavor,
                'key_name': self.env.agent_keypair_name,
                'security_groups': self.security_groups,
            })
            floating_ip_path = 'blueprint.nodes[0]'
            patch.merge_obj('{0}'.format(floating_ip_path), {
                'properties':                             {
                    'floatingip': {
                        'floating_network_name': self.env.external_network_name
                    }
                }
            })<commit_msg>CFY-552 fix image and flavor<commit_after>from neutronclient.common.exceptions import NeutronException
from novaclient.exceptions import NotFound

from cosmo_tester.framework.util import YamlPatcher

__author__ = 'elip'

from cosmo_tester.framework.testenv import TestCase
from cosmo_tester.framework.util import get_yaml_as_dict
from cosmo_tester.framework.openstack_api import openstack_clients

import requests

class HelloWorldBashTest(TestCase):

    CLOUDIFY_EXAMPLES_URL = "https://github.com/cloudify-cosmo/cloudify-examples.git"

    host_name = 'bash-web-server'
    flavor_name = 'm1.small'
    image_name = 'Ubuntu-NP'
    security_groups = ['webserver_security_group']
    virtual_ip_node_id = 'virtual_ip'
    server_node_id = 'vm'
    security_group_node_id = 'security_group'

    repo_dir = None

    def setUp(self):
        super(HelloWorldBashTest, self).setUp()
        from cosmo_tester.framework.git_helper import clone
        self.repo_dir = clone(self.CLOUDIFY_EXAMPLES_URL, self.workdir)

    def test_hello_world_bash(self):

        # Add agents security group from the cloudify-config file used to bootstrap
        self.security_groups.append(self.env.agents_security_group)

        self.blueprint_path = self.repo_dir / 'hello-world'
        self.blueprint_yaml = self.blueprint_path / 'blueprint.yaml'

        blueprint = get_yaml_as_dict(self.blueprint_yaml)

        self.modify_yaml(self.blueprint_yaml)

        # Upload --> Create Deployment --> Execute Install
        before, after = self.upload_deploy_and_execute_install()

        manager_state_delta = self.get_manager_state_delta(before, after)
        nodes_state = manager_state_delta['node_state'].values()[0]

        server_node = None
        security_group_node = None
        floatingip_node = None
        for key, value in nodes_state.items():
            if key.startswith(self.virtual_ip_node_id):
                floatingip_node = value
            if key.startswith(self.security_group_node_id):
                security_group_node = value
            if key.startswith(self.server_node_id):
                server_node = value

        webserver_port = blueprint['blueprint']['nodes'][3]['properties']['port']
        web_server_page_response = \
            requests.get('http://{0}:{1}'.format(
                floatingip_node['runtimeInfo']['floating_ip_address'],
                webserver_port))

        self.assertEqual(200, web_server_page_response.status_code)

        nova, neutron = openstack_clients(self.env.cloudify_config)

        self.logger.info("Retrieving agent server : {0}"
            .format(nova.servers.get(server_node['runtimeInfo']['openstack_server_id'])))
        self.logger.info("Retrieving agent floating ip : {0}"
            .format(neutron.show_floatingip(floatingip_node['runtimeInfo']['external_id'])))
        self.logger.info("Retrieving agent security group : {0}"
            .format(neutron.show_security_group(security_group_node['runtimeInfo']['external_id'])))

        self.execute_uninstall()

        # No components should exist after uninstall

        try:
            server = nova.servers.get(server_node['runtimeInfo']['openstack_server_id'])
            self.fail("Expected agent machine to be terminated. but found : {0}"
                .format(server))
        except NotFound as e:
            self.logger.info(e)
            pass

        try:
            floatingip = neutron.show_floatingip(floatingip_node['runtimeInfo']['external_id'])
            self.fail("Expected agent floating ip to be terminated. but found : {0}"
                .format(floatingip))
        except NeutronException as e:
            self.logger.info(e)
            pass

        try:
            security_group = neutron.show_security_group(security_group_node['runtimeInfo']['external_id'])
            self.fail("Expected webserver security group ip to be terminated. but found : {0}"
                .format(security_group))
        except NeutronException as e:
            self.logger.info(e)
            pass

    def modify_yaml(self, yaml_file):
        with YamlPatcher(yaml_file) as patch:
            vm_properties_path = 'blueprint.nodes[2].properties'
            patch.set_value('{0}.management_network_name'.format(vm_properties_path),
                            self.env.management_network_name)
            patch.set_value('{0}.worker_config.key'.format(vm_properties_path),
                            self.env.agent_key_path)
            patch.merge_obj('{0}.server'.format(vm_properties_path), {
                'name': self.host_name,
                'image_name': self.image_name,
                'flavor_name': self.flavor_name,
                'key_name': self.env.agent_keypair_name,
                'security_groups': self.security_groups,
            })
            floating_ip_path = 'blueprint.nodes[0]'
            patch.merge_obj('{0}'.format(floating_ip_path), {
                'properties':                             {
                    'floatingip': {
                        'floating_network_name': self.env.external_network_name
                    }
                }
            })<|endoftext|>


<commit_before>from django.conf import settings
from django.db.models import Count
from statistics import median
from statistics import StatisticsError
from collections import defaultdict
from django.db import models
from coursedashboards.models.instructor import Instructor
from coursedashboards.models.course import Course
from coursedashboards.models.term import Term
from coursedashboards.models.registration import Registration
from coursedashboards.models.major import StudentMajor
from coursedashboards.util.profile import (
    profile, log_profile_data, clear_prof_data)
from threading import Thread
import logging

logger = logging.getLogger(__name__)

class CourseOffering(models.Model):
    term = models.ForeignKey(Term,
                             on_delete=models.PROTECT)
    course = models.ForeignKey(Course,
                               on_delete=models.PROTECT)
    current_enrollment = models.PositiveSmallIntegerField()
    limit_estimate_enrollment = models.PositiveSmallIntegerField()
    canvas_course_url = models.CharField(max_length=2000)

    @profile
    def get_students(self):
        """
        Return Registration queryset for this course offering
        """
        if not hasattr(self, 'students'):
            self.students = Registration.objects.filter(
                course=self.course, term=self.term)

        return self.students

    @profile
    def get_student_gpa(self, registrations):
        """
        Return current gpa for given student
        (assumption: all student registrations are modelled)
        """
        try:

            points = 0.0
            credits = 0
            for reg in registrations:
                try:
                    course_credits = int(reg.credits)
                    points += (float(reg.grade) * course_credits)
                    credits += course_credits
                except ValueError:
                    pass

            return round(points / credits, 2)
        except ZeroDivisionError:
            return None

    def set_student_gpa(self, student, cumulative):
        gpa = self.get_student_gpa(student)
        if gpa:
            cumulative.append(gpa)

    @profile
    def get_cumulative_median_gpa(self):
        """
        Return median gpa for this course offering at the time it was offered
        """
        try:
            cumulative = []
            userids = []

            for student in self.get_students():
                userids.append(student.user_id)

            all_registrations = Registration.objects.\
                filter(user_id__in=userids)\
                .filter(term__term_key__lt=self.term.term_key)\
                .select_related('term')

            for student in self.get_students():
                regs = all_registrations.filter(user=student.user_id)
                gpa = self.get_student_gpa(regs)

                if gpa is not None:
                    cumulative.append(gpa)

            return round(median(cumulative), 2)
        except StatisticsError:
            return None

    @profile
    def get_grades(self):
        """
        Return grade array for course offering
        """
        return [float(s.grade) for s in self.get_students()
                if s.grade is not None and len(s.grade) > 0 and
                s.grade[0] in '01234']

    @profile
    def get_repeating_total(self):
        """
        Number of students repeating this course offering
        """
        return len(self.get_students().filter(is_repeat=True))

    @profile
    def get_instructors(self):
        return [{
            'display_name': inst.user.display_name,
            'uwnetid': inst.user.uwnetid,
            'email': inst.user.email
        } for inst in Instructor.objects.filter(course=self.course,
                                                term=self.term)]

    @profile
    def all_student_registrations(self):
        """
        Return given user's Courses for this term
        """
        students = list(self.get_students().values_list('user_id', flat=True))
        return Registration.objects.filter(
            user_id__in=students, term=self.term).select_related('course')

    @profile
    def concurrent_courses(self):
        course_dict = {}
        for reg in self.all_student_registrations():
            if reg.course.id != self.course.id:
                name = "%s" % reg.course
                name = name[:name.rindex('-')]
                if name in course_dict:
                    course_dict[name] += 1
                else:
                    course_dict[name] = 1

        total_students = float(len(self.get_students()))
        return [{
            "course": sort,
            "number_students": course_dict[sort],
            "percent_students": round(
                (float(course_dict[sort]) / total_students) * 100.0, 2)
        } for sort in sorted(course_dict, reverse=True, key=course_dict.get)]

    @profile
    def student_majors_for_term(self, students):
        return [sm.major.major for sm in StudentMajor.objects.filter(
            user_id__in=students.values_list('user_id', flat=True),
            term=self.term).select_related('major')]

    @profile
    def last_student_undergraduate_major(self, students):

        students = students.select_related('user')

        class_majors = self.retrieve_course_majors(students)
        student_majors = self.sort_major_by_user(class_majors)

        return self.process_individual_majors(student_majors)

    @profile
    def sort_major_by_user(self, class_majors):
        student_majors = {}

        for major in class_majors:
            if major.user_id not in student_majors:
                majors = []
                student_majors[major.user_id] = majors
            else:
                majors = student_majors[major.user_id]

            majors.append(major)

        return student_majors

    @profile
    def retrieve_course_majors(self, students):
        users = [student.user for student in students if student.user.is_alum]

        queryset = StudentMajor.objects.filter(user__in=users,
                                               major__degree_level=1) \
            .select_related('major', 'term')
        # trigger query for profiling:
        repr(queryset)

        return queryset

    @profile
    def process_individual_majors(self, student_majors):
        major_list = []
        for student in student_majors:

            majors = student_majors[student]
            majors = sorted(majors, cmp=StudentMajor.sort_by_term,
                            reverse=True)

            graduated_term = majors[0].term

            majors = [major for major in majors
                      if major.term == graduated_term]

            major_list += [sm.major.major for sm in majors]

        return major_list

    @profile
    def get_graduated_majors(self):
        return self._get_majors(self.last_student_undergraduate_major)

    @profile
    def get_majors(self):
        return self._get_majors(self.student_majors_for_term)

    def _get_majors(self, student_majors):
        students = self.get_students()
        total_students = float(len(students))
        majors = student_majors(students)
        majors_dict = defaultdict(int)
        for major in majors:
            majors_dict[major] += 1

        return [{
            "major": sort,
            "number_students": majors_dict[sort],
            "percent_students": round(
                (float(majors_dict[sort]) / total_students) * 100.0, 2)
            } for sort in sorted(
                majors_dict, reverse=True, key=majors_dict.get)]

    def get_fail_rate(self):
        past_objs = []
        threads = []

        for co in CourseOffering.objects.filter(
                course=self.course).select_related('course', 'term'):
            past_obj = {}
            past_objs.append(past_obj)

            t = Thread(target=co.set_past_course_grades,
                       args=(past_obj,))
            threads.append(t)
            t.start()

        for t in threads:
            t.join()

        total = 0.0
        failed = 0.0
        for past_obj in past_objs:
            total += len(past_obj['course_grades'])
            failed += len([grade for grade in past_obj['course_grades']
                          if grade < 0.7])

        if total == 0:
            return 0

        return failed / total

    def brief_json_object(self):
        json_obj = {
            'section_label': '%s' % self,
            'curriculum': self.course.curriculum,
            'course_number': self.course.course_number,
            'section_id': self.course.section_id
        }

        return json_obj

    def set_json_repeating_total(self, json_obj):
        json_obj['current_repeating'] = self.get_repeating_total()

    def set_fail_rate(self, json_obj):
        json_obj['failure_rate'] = self.get_fail_rate()

    def set_json_cumulative_median(self, json_obj):
        json_obj['current_median'] = self.get_cumulative_median_gpa()

    def set_json_concurrent_courses(self, json_obj):
        json_obj['concurrent_courses'] = self.concurrent_courses()

    def set_json_current_student_majors(self, json_obj):
        json_obj['current_student_majors'] = self.get_majors()

    def set_course_data(self, json_obj):
        # get students first

        self.get_students()
        repr(self.get_students())

        # start threads
        threads = []
        t = Thread(target=self.set_json_repeating_total,
                   args=(json_obj,))
        threads.append(t)
        t.start()

        t = Thread(target=self.set_json_cumulative_median,
                   args=(json_obj,))
        threads.append(t)
        t.start()

        t = Thread(target=self.set_json_concurrent_courses,
                   args=(json_obj,))
        threads.append(t)
        t.start()

        t = Thread(target=self.set_json_current_student_majors,
                   args=(json_obj,))
        threads.append(t)
        t.start()

        for t in threads:
            t.join()

    @profile
    def retrieve_db_objects(self):
        pass

    @profile
    def json_object(self):
        json_obj = self.base_json_object()

        json_obj['display_course'] = True

        self.set_course_data(json_obj)

        fields = ["current_repeating", "current_student_majors",
                  "current_student_majors", "concurrent_courses"]

        for field in fields:
            if field not in json_obj:
                raise(Exception("There was an error in data processing!"))

        log_profile_data('%s,%s' % (self.term, self.course), logger)
        clear_prof_data()
        return json_obj

    def base_json_object(self):
        json_obj = {
            'curriculum': self.course.curriculum,
            'course_number': self.course.course_number,
            'section_id': self.course.section_id,
            'course_title': self.course.course_title,
            'section_label': '%s' % self,
            'current_enrollment': self.current_enrollment,
            'limit_estimate_enrollment': self.limit_estimate_enrollment,
            'canvas_course_url': self.canvas_course_url,
            'display_course': False
        }

        return json_obj

    def set_past_offering_instructors(self, past_obj):
        past_obj['instructors'] = self.get_instructors()

    def set_past_offering_majors(self, past_obj):
        past_obj['majors'] = self.get_majors()

    def set_past_concurrent_courses(self, past_obj):
        past_obj['concurrent_courses'] = self.concurrent_courses()

    def set_past_latest_majors(self, past_obj):
        past_obj['latest_majors'] = self.get_graduated_majors()

    def set_past_course_grades(self, past_obj):
        past_obj['course_grades'] = self.get_grades()

    def set_past_median_gpa(self, past_obj):
        past_obj['past_median_gpa'] = self.get_cumulative_median_gpa()

    def set_past_offering_data(self, past_obj):
        # get students first

        self.get_students()
        repr(self.get_students())

        # start threads
        threads = []
        t = Thread(target=self.set_past_offering_instructors,
                   args=(past_obj,))
        threads.append(t)
        t.start()

        if getattr(settings, "HISTORIC_CGPA_ENABLED", False):
            t = Thread(target=self.set_past_median_gpa,
                       args=(past_obj,))
            threads.append(t)
            t.start()

        t = Thread(target=self.set_past_offering_majors,
                   args=(past_obj,))
        threads.append(t)
        t.start()

        t = Thread(target=self.set_past_concurrent_courses,
                   args=(past_obj,))
        threads.append(t)
        t.start()

        t = Thread(target=self.set_past_latest_majors,
                   args=(past_obj,))
        threads.append(t)
        t.start()

        t = Thread(target=self.set_past_course_grades,
                   args=(past_obj,))
        threads.append(t)
        t.start()

        for t in threads:
            t.join()

    def set_past_offering(self, course_offering, offerings):
        off_obj = {
            "year": course_offering.term.year,
            "quarter": course_offering.term.quarter,
            "enrollment": course_offering.current_enrollment
        }

        course_offering.set_past_offering_data(off_obj)

        offerings.append(off_obj)

    @profile
    def get_past_offerings(self):
        """
        List of past course offerings
        """
        min_offerings = 1
        quarters = 20

        threads = []
        offerings = []
        for co in CourseOffering.objects.filter(
                course=self.course).select_related('course', 'term'):
            if co.id != self.id:
                t = Thread(target=self.set_past_offering,
                           args=(co, offerings,))
                threads.append(t)
                t.start()

                if len(threads) >= quarters:
                    break

        for t in threads:
            t.join()

        fields = ["instructors", "majors", "concurrent_courses",
                  "course_grades", "latest_majors"]

        for offering in offerings:
            for field in fields:
                if field not in offering:
                    raise (Exception("There was an error in data processing!"))

        return offerings if len(offerings) >= min_offerings else []

    def past_offerings_json_object(self):
        json_obj = {
            'past_offerings': self.get_past_offerings(),
        }

        log_profile_data('%s,%s: PAST: ' % (self.term, self.course), logger)
        clear_prof_data()
        return json_obj

    class Meta:
        db_table = 'CourseOffering'
        unique_together = ('term', 'course')
        ordering = ['-term__year', '-term__quarter']

    def __str__(self):
        return "%s-%s" % (self.term, self.course)
<commit_msg>Removing prefetch of registrations<commit_after>from django.conf import settings
from django.db.models import Count
from statistics import median
from statistics import StatisticsError
from collections import defaultdict
from django.db import models
from coursedashboards.models.instructor import Instructor
from coursedashboards.models.course import Course
from coursedashboards.models.term import Term
from coursedashboards.models.registration import Registration
from coursedashboards.models.major import StudentMajor
from coursedashboards.util.profile import (
    profile, log_profile_data, clear_prof_data)
from threading import Thread
import logging

logger = logging.getLogger(__name__)

class CourseOffering(models.Model):
    term = models.ForeignKey(Term,
                             on_delete=models.PROTECT)
    course = models.ForeignKey(Course,
                               on_delete=models.PROTECT)
    current_enrollment = models.PositiveSmallIntegerField()
    limit_estimate_enrollment = models.PositiveSmallIntegerField()
    canvas_course_url = models.CharField(max_length=2000)

    @profile
    def get_students(self):
        """
        Return Registration queryset for this course offering
        """
        if not hasattr(self, 'students'):
            self.students = Registration.objects.filter(
                course=self.course, term=self.term)

        return self.students

    @profile
    def get_student_gpa(self, registrations):
        """
        Return current gpa for given student
        (assumption: all student registrations are modelled)
        """
        try:

            points = 0.0
            credits = 0
            for reg in registrations:
                try:
                    course_credits = int(reg.credits)
                    points += (float(reg.grade) * course_credits)
                    credits += course_credits
                except ValueError:
                    pass

            return round(points / credits, 2)
        except ZeroDivisionError:
            return None

    def set_student_gpa(self, student, cumulative):
        gpa = self.get_student_gpa(student)
        if gpa:
            cumulative.append(gpa)

    @profile
    def get_cumulative_median_gpa(self):
        """
        Return median gpa for this course offering at the time it was offered
        """
        try:
            cumulative = []
            userids = []

            for student in self.get_students():
                userids.append(student.user_id)

            all_registrations = Registration.objects.\
                filter(user_id__in=userids)\
                .filter(term__term_key__lt=self.term.term_key)\
                .select_related('term')

            for student in self.get_students():
                regs = all_registrations.filter(user=student.user_id)
                gpa = self.get_student_gpa(regs)

                if gpa is not None:
                    cumulative.append(gpa)

            return round(median(cumulative), 2)
        except StatisticsError:
            return None

    @profile
    def get_grades(self):
        """
        Return grade array for course offering
        """
        return [float(s.grade) for s in self.get_students()
                if s.grade is not None and len(s.grade) > 0 and
                s.grade[0] in '01234']

    @profile
    def get_repeating_total(self):
        """
        Number of students repeating this course offering
        """
        return len(self.get_students().filter(is_repeat=True))

    @profile
    def get_instructors(self):
        return [{
            'display_name': inst.user.display_name,
            'uwnetid': inst.user.uwnetid,
            'email': inst.user.email
        } for inst in Instructor.objects.filter(course=self.course,
                                                term=self.term)]

    @profile
    def all_student_registrations(self):
        """
        Return given user's Courses for this term
        """
        students = list(self.get_students().values_list('user_id', flat=True))
        return Registration.objects.filter(
            user_id__in=students, term=self.term).select_related('course')

    @profile
    def concurrent_courses(self):
        course_dict = {}
        for reg in self.all_student_registrations():
            if reg.course.id != self.course.id:
                name = "%s" % reg.course
                name = name[:name.rindex('-')]
                if name in course_dict:
                    course_dict[name] += 1
                else:
                    course_dict[name] = 1

        total_students = float(len(self.get_students()))
        return [{
            "course": sort,
            "number_students": course_dict[sort],
            "percent_students": round(
                (float(course_dict[sort]) / total_students) * 100.0, 2)
        } for sort in sorted(course_dict, reverse=True, key=course_dict.get)]

    @profile
    def student_majors_for_term(self, students):
        return [sm.major.major for sm in StudentMajor.objects.filter(
            user_id__in=students.values_list('user_id', flat=True),
            term=self.term).select_related('major')]

    @profile
    def last_student_undergraduate_major(self, students):

        students = students.select_related('user')

        class_majors = self.retrieve_course_majors(students)
        student_majors = self.sort_major_by_user(class_majors)

        return self.process_individual_majors(student_majors)

    @profile
    def sort_major_by_user(self, class_majors):
        student_majors = {}

        for major in class_majors:
            if major.user_id not in student_majors:
                majors = []
                student_majors[major.user_id] = majors
            else:
                majors = student_majors[major.user_id]

            majors.append(major)

        return student_majors

    @profile
    def retrieve_course_majors(self, students):
        users = [student.user for student in students if student.user.is_alum]

        queryset = StudentMajor.objects.filter(user__in=users,
                                               major__degree_level=1) \
            .select_related('major', 'term')
        # trigger query for profiling:
        repr(queryset)

        return queryset

    @profile
    def process_individual_majors(self, student_majors):
        major_list = []
        for student in student_majors:

            majors = student_majors[student]
            majors = sorted(majors, cmp=StudentMajor.sort_by_term,
                            reverse=True)

            graduated_term = majors[0].term

            majors = [major for major in majors
                      if major.term == graduated_term]

            major_list += [sm.major.major for sm in majors]

        return major_list

    @profile
    def get_graduated_majors(self):
        return self._get_majors(self.last_student_undergraduate_major)

    @profile
    def get_majors(self):
        return self._get_majors(self.student_majors_for_term)

    def _get_majors(self, student_majors):
        students = self.get_students()
        total_students = float(len(students))
        majors = student_majors(students)
        majors_dict = defaultdict(int)
        for major in majors:
            majors_dict[major] += 1

        return [{
            "major": sort,
            "number_students": majors_dict[sort],
            "percent_students": round(
                (float(majors_dict[sort]) / total_students) * 100.0, 2)
            } for sort in sorted(
                majors_dict, reverse=True, key=majors_dict.get)]

    def get_fail_rate(self):
        past_objs = []
        threads = []

        for co in CourseOffering.objects.filter(
                course=self.course).select_related('course', 'term'):
            past_obj = {}
            past_objs.append(past_obj)

            t = Thread(target=co.set_past_course_grades,
                       args=(past_obj,))
            threads.append(t)
            t.start()

        for t in threads:
            t.join()

        total = 0.0
        failed = 0.0
        for past_obj in past_objs:
            total += len(past_obj['course_grades'])
            failed += len([grade for grade in past_obj['course_grades']
                          if grade < 0.7])

        if total == 0:
            return 0

        return failed / total

    def brief_json_object(self):
        json_obj = {
            'section_label': '%s' % self,
            'curriculum': self.course.curriculum,
            'course_number': self.course.course_number,
            'section_id': self.course.section_id
        }

        return json_obj

    def set_json_repeating_total(self, json_obj):
        json_obj['current_repeating'] = self.get_repeating_total()

    def set_fail_rate(self, json_obj):
        json_obj['failure_rate'] = self.get_fail_rate()

    def set_json_cumulative_median(self, json_obj):
        json_obj['current_median'] = self.get_cumulative_median_gpa()

    def set_json_concurrent_courses(self, json_obj):
        json_obj['concurrent_courses'] = self.concurrent_courses()

    def set_json_current_student_majors(self, json_obj):
        json_obj['current_student_majors'] = self.get_majors()

    def set_course_data(self, json_obj):
        threads = []
        t = Thread(target=self.set_json_repeating_total,
                   args=(json_obj,))
        threads.append(t)
        t.start()

        t = Thread(target=self.set_json_cumulative_median,
                   args=(json_obj,))
        threads.append(t)
        t.start()

        t = Thread(target=self.set_json_concurrent_courses,
                   args=(json_obj,))
        threads.append(t)
        t.start()

        t = Thread(target=self.set_json_current_student_majors,
                   args=(json_obj,))
        threads.append(t)
        t.start()

        for t in threads:
            t.join()

    @profile
    def retrieve_db_objects(self):
        pass

    @profile
    def json_object(self):
        json_obj = self.base_json_object()

        json_obj['display_course'] = True

        self.set_course_data(json_obj)

        fields = ["current_repeating", "current_student_majors",
                  "current_student_majors", "concurrent_courses"]

        for field in fields:
            if field not in json_obj:
                raise(Exception("There was an error in data processing!"))

        log_profile_data('%s,%s' % (self.term, self.course), logger)
        clear_prof_data()
        return json_obj

    def base_json_object(self):
        json_obj = {
            'curriculum': self.course.curriculum,
            'course_number': self.course.course_number,
            'section_id': self.course.section_id,
            'course_title': self.course.course_title,
            'section_label': '%s' % self,
            'current_enrollment': self.current_enrollment,
            'limit_estimate_enrollment': self.limit_estimate_enrollment,
            'canvas_course_url': self.canvas_course_url,
            'display_course': False
        }

        return json_obj

    def set_past_offering_instructors(self, past_obj):
        past_obj['instructors'] = self.get_instructors()

    def set_past_offering_majors(self, past_obj):
        past_obj['majors'] = self.get_majors()

    def set_past_concurrent_courses(self, past_obj):
        past_obj['concurrent_courses'] = self.concurrent_courses()

    def set_past_latest_majors(self, past_obj):
        past_obj['latest_majors'] = self.get_graduated_majors()

    def set_past_course_grades(self, past_obj):
        past_obj['course_grades'] = self.get_grades()

    def set_past_median_gpa(self, past_obj):
        past_obj['past_median_gpa'] = self.get_cumulative_median_gpa()

    def set_past_offering_data(self, past_obj):
        threads = []
        t = Thread(target=self.set_past_offering_instructors,
                   args=(past_obj,))
        threads.append(t)
        t.start()

        if getattr(settings, "HISTORIC_CGPA_ENABLED", False):
            t = Thread(target=self.set_past_median_gpa,
                       args=(past_obj,))
            threads.append(t)
            t.start()

        t = Thread(target=self.set_past_offering_majors,
                   args=(past_obj,))
        threads.append(t)
        t.start()

        t = Thread(target=self.set_past_concurrent_courses,
                   args=(past_obj,))
        threads.append(t)
        t.start()

        t = Thread(target=self.set_past_latest_majors,
                   args=(past_obj,))
        threads.append(t)
        t.start()

        t = Thread(target=self.set_past_course_grades,
                   args=(past_obj,))
        threads.append(t)
        t.start()

        for t in threads:
            t.join()

    def set_past_offering(self, course_offering, offerings):
        off_obj = {
            "year": course_offering.term.year,
            "quarter": course_offering.term.quarter,
            "enrollment": course_offering.current_enrollment
        }

        course_offering.set_past_offering_data(off_obj)

        offerings.append(off_obj)

    @profile
    def get_past_offerings(self):
        """
        List of past course offerings
        """
        min_offerings = 1
        quarters = 20

        threads = []
        offerings = []
        for co in CourseOffering.objects.filter(
                course=self.course).select_related('course', 'term'):
            if co.id != self.id:
                t = Thread(target=self.set_past_offering,
                           args=(co, offerings,))
                threads.append(t)
                t.start()

                if len(threads) >= quarters:
                    break

        for t in threads:
            t.join()

        fields = ["instructors", "majors", "concurrent_courses",
                  "course_grades", "latest_majors"]

        for offering in offerings:
            for field in fields:
                if field not in offering:
                    raise (Exception("There was an error in data processing!"))

        return offerings if len(offerings) >= min_offerings else []

    def past_offerings_json_object(self):
        json_obj = {
            'past_offerings': self.get_past_offerings(),
        }

        log_profile_data('%s,%s: PAST: ' % (self.term, self.course), logger)
        clear_prof_data()
        return json_obj

    class Meta:
        db_table = 'CourseOffering'
        unique_together = ('term', 'course')
        ordering = ['-term__year', '-term__quarter']

    def __str__(self):
        return "%s-%s" % (self.term, self.course)
<|endoftext|>


<commit_before>#!/usr/bin/python -tt
# -*- coding: utf-8 -*-
#
#   Copyright 2014 Michal Sadowski (sq6jnx at hamradio dot pl)
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#

import warnings

class SR0WXModule(object):
    """Base class for SR0WX modules."""
    def __init__(self):
        pass

    def getData(self):
        """Deprecated method. Runs `get_data()`."""
        msg = "Use if getData() is deprecated, use get_data() instead"
        warnings.warn(msg)
        return self.get_data()

    def get_data(self):
        """Returns message to be played back by core of sr0wx.py. Not
implemented here"""
        msg = "This method should be implemented in child class"
        raise NotImplementedError(msg)
<commit_msg>Updated SR0WXModule.get_data() docstring (#17)<commit_after>#!/usr/bin/python -tt
# -*- coding: utf-8 -*-
#
#   Copyright 2014 Michal Sadowski (sq6jnx at hamradio dot pl)
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#

import warnings

class SR0WXModule(object):
    """Base class for SR0WX modules."""
    def __init__(self):
        pass

    def getData(self):
        """Deprecated method. Runs `get_data()`."""
        msg = "Use if getData() is deprecated, use get_data() instead"
        warnings.warn(msg)
        return self.get_data()

    def get_data(self):
        """Returns message to be played back by core of sr0wx.py. Not
implemented here.

Modules are expected to return a `dict` with the following keys:
    - `message` -- message text, filled template, etc (currently list of
    samples)
    - `need_ctcss` -- hint for core module whether or not to playback CTCSS tone
"""
        msg = "This method should be implemented in child class"
        raise NotImplementedError(msg)
<|endoftext|>