Esempio n. 1
0
def add_cell(request, matrix_id):

    data = get_header_data(request.user)

    if data["credential_flag"] == NO_CREDENTIALS:

        return HttpResponseRedirect(reverse('home', args=()))

    else:

        matrix = Matrix.objects.get(id=matrix_id)

        authority = get_authority_for_bench_and_user_and_requester(
            matrix, request.user)

        if authority.is_viewer() == True or authority.is_none() == True:

            return HttpResponseRedirect(reverse('home', args=()))

        else:

            cell_list = Cell.objects.filter(matrix=matrix)

            if not cell_list:

                cell1 = Cell.create(matrix, "", "", 0, 0, "", None)
                cell2 = Cell.create(matrix, "", "", 0, 1, "", None)
                cell3 = Cell.create(matrix, "", "", 0, 2, "", None)
                cell4 = Cell.create(matrix, "", "", 1, 0, "", None)
                cell5 = Cell.create(matrix, "", "", 1, 1, "", None)
                cell6 = Cell.create(matrix, "", "", 1, 2, "", None)
                cell7 = Cell.create(matrix, "", "", 2, 0, "", None)
                cell8 = Cell.create(matrix, "", "", 2, 1, "", None)
                cell9 = Cell.create(matrix, "", "", 2, 2, "", None)

                cell1.save()
                cell2.save()
                cell3.save()
                cell4.save()
                cell5.save()
                cell6.save()
                cell7.save()
                cell8.save()
                cell9.save()

                matrix.save()

            matrix_cells = matrix.get_matrix()
            columns = matrix.get_columns()
            rows = matrix.get_rows()

            data.update({
                'matrix': matrix,
                'rows': rows,
                'columns': columns,
                'matrix_cells': matrix_cells
            })

            return HttpResponseRedirect(reverse('matrix', args=(matrix_id, )))
def add_row_below(request, matrix_id, row_id):

    data = get_header_data(request.user)

    if data["credential_flag"] == NO_CREDENTIALS:

        return HttpResponseRedirect(reverse('home', args=()))

    else:

        matrix = get_object_or_404(Matrix, pk=matrix_id)

        authority = get_authority_for_bench_and_user_and_requester(
            matrix, request.user)

        if authority.is_viewer() == True or authority.is_none() == True:

            return HttpResponseRedirect(reverse('home', args=()))

        else:

            matrix = Matrix.objects.get(id=matrix_id)

            oldCells = Cell.objects.filter(matrix=matrix_id).filter(
                ycoordinate__gt=row_id)
            columns = matrix.get_columns()

            new_row_id = int(row_id) + 1

            for oldcell in oldCells:

                oldcell.increment_y()

                oldcell.save()

            for i, column in enumerate(columns):

                cell = Cell.create(matrix, "", "", i, new_row_id, "", None)

                cell.save()

            matrix.save()
            matrix_cells = matrix.get_matrix()
            columns = matrix.get_columns()
            rows = matrix.get_rows()

            data.update({
                'matrix': matrix,
                'rows': rows,
                'columns': columns,
                'matrix_cells': matrix_cells
            })

            return HttpResponseRedirect(reverse('matrix', args=(matrix_id, )))
def append_column(request, matrix_id):

    data = get_header_data(request.user)

    if data["credential_flag"] == NO_CREDENTIALS:

        return HttpResponseRedirect(reverse('home', args=()))

    else:

        matrix = get_object_or_404(Matrix, pk=matrix_id)

        authority = get_authority_for_bench_and_user_and_requester(
            matrix, request.user)

        if authority.is_viewer() == True or authority.is_none() == True:

            return HttpResponseRedirect(reverse('home', args=()))

        else:

            matrix = Matrix.objects.get(id=matrix_id)

            nextColumn = matrix.get_column_count()
            rows = matrix.get_rows()

            for i, row in enumerate(rows):

                cell = Cell.create(matrix, "", "", nextColumn, i, "", None)

                cell.save()

            matrix.save()

            matrix_cells = matrix.get_matrix()
            columns = matrix.get_columns()
            rows = matrix.get_rows()

            data.update({
                'matrix': matrix,
                'rows': rows,
                'columns': columns,
                'matrix_cells': matrix_cells
            })

            return HttpResponseRedirect(reverse('matrix', args=(matrix_id, )))
Esempio n. 4
0
    def test_cell_init(self):

        ordinary_cell = Cell(matrix=self.matrix,
                             title="ordinary_cell",
                             description="cell_description",
                             xcoordinate=99,
                             ycoordinate=99,
                             blogpost="cell_blogpost",
                             image=self.image)

        cell_form = CellForm(self.owner.id,
                             ordinary_cell.image.id,
                             "POST",
                             instance=ordinary_cell)

        self.assertTrue(isinstance(cell_form, CellForm))

        self.assertEqual(
            cell_form.label_from_instance(self.image),
            "image_name<a href=\"image_viewer_url\" target=\"_blank\"><img  style=\"width:256px; height:256px; float: left\" title=\"image_name\" src=\"image_birdseye_url\" ></a>"
        )
Esempio n. 5
0
def shuffle_rows(request):
    """
    AJAX - Shuffle the Rows
    """

    source = request.POST['source']
    target = request.POST['target']

    in_source_cell = get_object_or_404(Cell, pk=source)
    in_target_cell = get_object_or_404(Cell, pk=target)

    source_ycoordinate = in_source_cell.ycoordinate
    target_ycoordinate = in_target_cell.ycoordinate

    matrix = in_source_cell.matrix

    owner = get_object_or_404(User, pk=matrix.owner_id)
    user = get_object_or_404(User, pk=request.user.id)

    if credential_exists(user):

        authority = get_authority_for_bench_and_user_and_requester(
            matrix, request.user)

        if authority.is_viewer() or authority.is_none():

            data = {
                'failure': True,
                'source': str(source),
                'target': str(target)
            }

            return JsonResponse(data)

        else:

            source_row_cells = matrix.get_row(source_ycoordinate)

            if source_ycoordinate < target_ycoordinate:

                oldCells = Cell.objects.filter(matrix=matrix.id).filter(
                    ycoordinate__gt=source_ycoordinate).filter(
                        ycoordinate__lte=target_ycoordinate)

                output_cells = list()

                for oldcell in oldCells:

                    oldcell.decrement_y()

                    output_cells.append(oldcell)

                for source_cell in source_row_cells:

                    source_cell.set_ycoordinate(target_ycoordinate)

                    output_cells.append(source_cell)

                for output_cell in output_cells:

                    output_cell.save()

            if source_ycoordinate > target_ycoordinate:

                oldCells = Cell.objects.filter(matrix=matrix.id).filter(
                    ycoordinate__gte=target_ycoordinate).filter(
                        ycoordinate__lt=source_ycoordinate)

                output_cells = list()

                for oldcell in oldCells:

                    oldcell.increment_y()

                    output_cells.append(oldcell)

                for source_cell in source_row_cells:

                    source_cell.set_ycoordinate(target_ycoordinate)

                    output_cells.append(source_cell)

                for output_cell in output_cells:

                    output_cell.save()

            if matrix.get_max_row() == target_ycoordinate:

                nextRow = matrix.get_row_count()
                columns = matrix.get_columns()

                for i, column in enumerate(columns):

                    cell = Cell.create(matrix, "", "", i, nextRow, "", None)

                    cell.save()

                matrix.save()

            data = {
                'failure': False,
                'source': str(source),
                'target': str(target)
            }

            return JsonResponse(data)

    else:

        data = {'failure': True, 'source': str(source), 'target': str(target)}

        return JsonResponse(data)
def new_matrix(request):

    serverWordpress = get_primary_wordpress_server()

    data = get_header_data(request.user)

    if data["credential_flag"] == NO_CREDENTIALS:

        return HttpResponseRedirect(reverse('home', args=()))

    else:

        if request.method == HTTP_POST:

            form = NewMatrixForm(request.POST)

            if form.is_valid():

                matrix = form.save(commit=False)

                rows = form.cleaned_data['rows']
                columns = form.cleaned_data['columns']

                rows = rows + 1
                columns = columns + 1

                if rows == 1:
                    rows = 2

                if columns == 1:
                    columns = 2

                if rows > MAX_INITIAL_ROWS:
                    rows = 2

                if columns > MAX_INITIAL_COLUMNS:
                    columns = 2

                if matrix.is_not_high_enough() == True:
                    matrix.set_minimum_height()

                if matrix.is_not_wide_enough() == True:
                    matrix.set_minimum_width()

                if matrix.is_too_high() == True:
                    matrix.set_maximum_height()

                if matrix.is_too_wide() == True:
                    matrix.set_maximum_width()

                credential = get_credential_for_user(request.user)

                post_id = ''

                if credential.has_apppwd():

                    returned_blogpost = serverWordpress.post_wordpress_post(credential, matrix.title, matrix.description)

                    if returned_blogpost['status'] == WORDPRESS_SUCCESS:

                        post_id = returned_blogpost['id']

                    else:

                        messages.error(request, "ERROR: WordPress Error - Contact System Administrator!")
                        form.add_error(None, "ERROR: WordPress Error - Contact System Administrator!")

                        data.update({ 'form': form })

                        return render(request, 'matrices/new_matrix.html', data)


                matrix.set_blogpost(post_id)

                matrix.set_owner(request.user)

                matrix.save()

                x = 0

                while x <= columns:

                    y = 0

                    while y <= rows:

                        cell = Cell.create(matrix, "", "", x, y, "", None)

                        cell.save()

                        y = y + 1

                    x = x + 1

                return HttpResponseRedirect(reverse('matrix', args=(matrix.id,)))

            else:

                messages.error(request, "Matrix Form is Invalid!")
                form.add_error(None, "Matrix Form is Invalid!")

                data.update({ 'form': form })

        else:

            form = NewMatrixForm()

            data.update({ 'form': form })

        return render(request, 'matrices/new_matrix.html', data)
Esempio n. 7
0
def overwrite_cell(request):
    """
    AJAX - Overwrite Cell - MOVE
    """

    source = request.POST['source']
    target = request.POST['target']
    source_type = request.POST['source_type']

    source_cell = get_object_or_404(Cell, pk=source)
    target_cell = get_object_or_404(Cell, pk=target)

    matrix = source_cell.matrix

    owner = get_object_or_404(User, pk=matrix.owner_id)
    user = get_object_or_404(User, pk=request.user.id)

    serverWordpress = get_primary_wordpress_server()

    if credential_exists(user):

        authority = get_authority_for_bench_and_user_and_requester(
            matrix, request.user)

        if authority.is_viewer() or authority.is_none():

            data = {
                'failure': True,
                'source': str(source),
                'target': str(target)
            }

            return JsonResponse(data)

        else:

            if matrix.get_max_row() == target_cell.ycoordinate:

                nextRow = matrix.get_row_count()
                columns = matrix.get_columns()

                for i, column in enumerate(columns):

                    cell = Cell.create(matrix, "", "", i, nextRow, "", None)

                    cell.save()

                matrix.save()

            if matrix.get_max_column() == target_cell.xcoordinate:

                nextColumn = matrix.get_column_count()
                rows = matrix.get_rows()

                for i, row in enumerate(rows):

                    cell = Cell.create(matrix, "", "", nextColumn, i, "", None)

                    cell.save()

                matrix.save()

            if target_cell.has_blogpost():

                credential = get_credential_for_user(request.user)

                if credential.has_apppwd():

                    response = serverWordpress.delete_wordpress_post(
                        credential, target_cell.blogpost)

            if target_cell.has_image():

                if not exists_collections_for_image(target_cell.image):

                    cell_list = get_cells_for_image(target_cell.image)

                    delete_flag = True

                    for otherCell in cell_list:

                        if otherCell.matrix.id != matrix.id:

                            delete_flag = False

                    if delete_flag == True:

                        image = target_cell.image

                        target_cell.image = None

                        target_cell.save()

                        image.delete()

            source_xcoordinate = source_cell.xcoordinate
            source_ycoordinate = source_cell.ycoordinate

            target_xcoordinate = target_cell.xcoordinate
            target_ycoordinate = target_cell.ycoordinate

            source_cell.xcoordinate = target_xcoordinate
            source_cell.ycoordinate = target_ycoordinate

            target_cell.xcoordinate = source_xcoordinate
            target_cell.ycoordinate = source_ycoordinate

            target_cell.title = ""
            target_cell.description = ""

            target_cell.blogpost = ""
            target_cell.image = None

            source_cell.save()
            target_cell.save()

            data = {
                'failure': False,
                'source': str(source),
                'target': str(target)
            }

            return JsonResponse(data)

    else:

        data = {'failure': True, 'source': str(source), 'target': str(target)}

        return JsonResponse(data)
Esempio n. 8
0
def overwrite_cell_leave(request):
    """
    AJAX - Overwrite Cell
    """

    source = request.POST['source']
    target = request.POST['target']
    source_type = request.POST['source_type']

    source_cell = get_object_or_404(Cell, pk=source)
    target_cell = get_object_or_404(Cell, pk=target)

    matrix = source_cell.matrix

    owner = get_object_or_404(User, pk=matrix.owner_id)
    user = get_object_or_404(User, pk=request.user.id)

    serverWordpress = get_primary_wordpress_server()

    if credential_exists(user):

        authority = get_authority_for_bench_and_user_and_requester(
            matrix, request.user)

        if authority.is_viewer() or authority.is_none():

            data = {
                'failure': True,
                'source': str(source),
                'target': str(target)
            }

            return JsonResponse(data)

        else:

            if matrix.get_max_row() == target_cell.ycoordinate:

                nextRow = matrix.get_row_count()
                columns = matrix.get_columns()

                for i, column in enumerate(columns):

                    cell = Cell.create(matrix, "", "", i, nextRow, "", None)

                    cell.save()

                matrix.save()

            if matrix.get_max_column() == target_cell.xcoordinate:

                nextColumn = matrix.get_column_count()
                rows = matrix.get_rows()

                for i, row in enumerate(rows):

                    cell = Cell.create(matrix, "", "", nextColumn, i, "", None)

                    cell.save()

                matrix.save()

            if target_cell.has_blogpost():

                credential = get_credential_for_user(request.user)

                if credential.has_apppwd():

                    response = serverWordpress.delete_wordpress_post(
                        credential, target_cell.blogpost)

            if target_cell.has_image():

                if not exists_collections_for_image(target_cell.image):

                    cell_list = get_cells_for_image(target_cell.image)

                    delete_flag = True

                    for otherCell in cell_list:

                        if otherCell.matrix.id != matrix.id:

                            delete_flag = False

                    if delete_flag == True:

                        image = target_cell.image

                        target_cell.image = None

                        target_cell.save()

                        image.delete()

            target_cell.title = source_cell.title
            target_cell.description = source_cell.description

            if source_cell.has_image():

                imageOld = Image.objects.get(pk=source_cell.image.id)

                imageNew = Image.create(imageOld.identifier, imageOld.name,
                                        imageOld.server, imageOld.viewer_url,
                                        imageOld.birdseye_url, imageOld.roi,
                                        imageOld.owner)

                imageNew.save()

                target_cell.image = imageNew

            target_cell.blogpost = source_cell.blogpost

            if source_cell.has_blogpost():

                credential = get_credential_for_user(request.user)

                post_id = ''

                if credential.has_apppwd():

                    returned_blogpost = serverWordpress.post_wordpress_post(
                        credential, source_cell.title, source_cell.description)

                    if returned_blogpost['status'] == WORDPRESS_SUCCESS:

                        post_id = returned_blogpost['id']

                source_cell.set_blogpost(post_id)

            source_cell.save()
            target_cell.save()

            data = {
                'failure': False,
                'source': str(source),
                'target': str(target)
            }

            return JsonResponse(data)

    else:

        data = {'failure': True, 'source': str(source), 'target': str(target)}

        return JsonResponse(data)
def swap_columns(request):
    """
    AJAX - Swap Columns
    """

    source = request.POST['source']
    target = request.POST['target']

    in_source_cell = get_object_or_404(Cell, pk=source)
    in_target_cell = get_object_or_404(Cell, pk=target)

    matrix = in_source_cell.matrix

    owner = get_object_or_404(User, pk=matrix.owner_id)
    user = get_object_or_404(User, pk=request.user.id)

    if credential_exists(user):

        authority = get_authority_for_bench_and_user_and_requester(
            matrix, request.user)

        if authority.is_viewer() or authority.is_none():

            data = {
                'failure': True,
                'source': str(source),
                'target': str(target)
            }

            return JsonResponse(data)

        else:

            source_column_cells = matrix.get_column(in_source_cell.xcoordinate)
            target_column_cells = matrix.get_column(in_target_cell.xcoordinate)

            source_xcoordinate = in_source_cell.xcoordinate
            target_xcoordinate = in_target_cell.xcoordinate

            output_cells = list()

            for target_cell in target_column_cells:

                target_cell.set_xcoordinate(source_xcoordinate)

                output_cells.append(target_cell)

            for source_cell in source_column_cells:

                source_cell.set_xcoordinate(target_xcoordinate)

                output_cells.append(source_cell)

            for output_cell in output_cells:

                output_cell.save()

            if matrix.get_max_column() == in_target_cell.xcoordinate:

                nextColumn = matrix.get_column_count()
                rows = matrix.get_rows()

                for i, row in enumerate(rows):

                    cell = Cell.create(matrix, "", "", nextColumn, i, "", None)

                    cell.save()

                matrix.save()

            data = {
                'failure': False,
                'source': str(source),
                'target': str(target)
            }

            return JsonResponse(data)

    else:

        data = {'failure': True, 'source': str(source), 'target': str(target)}

        return JsonResponse(data)
def import_image(request):
    """
    AJAX - Import Image
    """

    source = request.POST['source']
    target = request.POST['target']
    source_type = request.POST['source_type']

    source_image = get_object_or_404(Image, pk=source)
    target_cell = get_object_or_404(Cell, pk=target)

    matrix = target_cell.matrix

    owner = get_object_or_404(User, pk=matrix.owner_id)
    user = get_object_or_404(User, pk=request.user.id)

    serverWordpress = get_primary_wordpress_server()

    if credential_exists(user):

        authority = get_authority_for_bench_and_user_and_requester(
            matrix, request.user)

        if authority.is_viewer() or authority.is_none():

            data = {
                'failure': True,
                'source': str(source),
                'target': str(target)
            }

            return JsonResponse(data)

        else:

            if matrix.get_max_row() == target_cell.ycoordinate:

                nextRow = matrix.get_row_count()
                columns = matrix.get_columns()

                for i, column in enumerate(columns):

                    cell = Cell.create(matrix, "", "", i, nextRow, "", None)

                    cell.save()

                matrix.save()

            if matrix.get_max_column() == target_cell.xcoordinate:

                nextColumn = matrix.get_column_count()
                rows = matrix.get_rows()

                for i, row in enumerate(rows):

                    cell = Cell.create(matrix, "", "", nextColumn, i, "", None)

                    cell.save()

                matrix.save()

            post_id = ''

            target_cell.title = source_image.name
            target_cell.description = source_image.name

            target_cell.image = source_image

            if target_cell.has_no_blogpost():

                credential = get_credential_for_user(request.user)

                if credential.has_apppwd():

                    returned_blogpost = serverWordpress.post_wordpress_post(
                        credential, target_cell.title, target_cell.description)

                    if returned_blogpost['status'] == WORDPRESS_SUCCESS:

                        post_id = returned_blogpost['id']

                target_cell.set_blogpost(post_id)

            target_cell.save()

            data = {
                'failure': False,
                'source': str(source),
                'target': str(target)
            }

            return JsonResponse(data)

    else:

        data = {'failure': True, 'source': str(source), 'target': str(target)}

        return JsonResponse(data)