quarta-feira, 25 de abril de 2018

How to cause a deadlock on SQL Server on purpose

Hi!

What I really needed was an error with the word deadlock inside of the message, so this is what I did.
Whenever I need a deadlock to raise up I execute my procedure proc_deadlock.
Then I make my code treatment based on the message that contains the word deadlock.


USE [master]
GO

/****** Object:  StoredProcedure [dbo].[proc_deadlock]    Script Date: 25/04/2018 12:47:19 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

create procedure [dbo].[proc_deadlock]
as

    -- RAISERROR with severity 11-19 will cause execution to 
    -- jump to the CATCH block. 
    RAISERROR ('this is my deadlock', -- Message text. 
               16, -- Severity. 
               1 -- State. 
               ); 

GO


quinta-feira, 19 de outubro de 2017

sexta-feira, 18 de agosto de 2017

DJANGO - Initial steps

If you're starting from scratch
pip install virtualenv
--------------------------------------

virtualenv [env_name]
pip install django
python Scripts\django-admin.py startproject [project_name]
python manage.py startapp [app_name]


sexta-feira, 11 de agosto de 2017

Without LoginRequiredMixin redirect, request and settings show an error 'not defined'


If you don't use this LoginRequiredMixin, you're gonna receive

name settings not defined
name request not defined
name redirect not defined



class BookListView(LoginRequiredMixin,generic.ListView):
    #login_url = '/login/'
    #redirect_field_name = 'redirect_to'
       
    model = Book
    paginate_by = 2  
    context_object_name = 'book_list'   # your own name for the list as a template variable
    #queryset = Book.objects.filter(title__icontains='war')[:5] # Get 5 books containing the title war
    queryset = Book.objects.all()
    template_name = 'books/book_list.html'  # Specify your own template name/location
   
    def get_context_data(self, **kwargs):
        if not request.user.is_authenticated:
            return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
        else:
            # Call the base implementation first to get a context
            context = super(BookListView, self).get_context_data(**kwargs)
            # Get the blog from id and add it to the context
            context['some_data'] = 'This is just some data'
            return context

domingo, 6 de agosto de 2017

The Zen of Python - Factorial

def calculate_factorial(n):
    def nfatorial(current,currentfactorial):
        if current < 2:
            return currentfactorial
        else:
            current = current - 1
            currentfactorial = currentfactorial * (current)
            return nfatorial(current,currentfactorial)
    return nfatorial(n+1,1)