# Path: apps/wklej/forms.py
# class WklejkaForm(forms.ModelForm):
#     nickname = forms.CharField(required=False)
# 
#     class Meta:
#         model = Wklejka
#         fields = '__all__'
# 
#     def clean_nickname(self):
#         if len(self.cleaned_data['nickname']) == 0:
#             self.cleaned_data['nickname'] = 'Anonim'
#         elif len(self.cleaned_data['nickname']) > 30:
#             self.cleaned_data['nickname'] = self.cleaned_data['nickname'][:29]
# 
#         return self.cleaned_data['nickname']
# 
#     def clean_body(self):
#         if settings.USE_CAPTCHA and check_for_link_spam(self.cleaned_data['body']):
#             raise forms.ValidationError("This paste looks like spam.")
#         return self.cleaned_data['body']
# 
# class WklejkaCaptchaForm(forms.ModelForm):
#     recaptcha = ReCaptchaField()
#     has_captcha = forms.CharField(widget=forms.HiddenInput, required=False)
#     nickname = forms.CharField(required=False)
# 
#     class Meta:
#         model = Wklejka
#         fields = '__all__'
# 
#     def clean_nickname(self):
#         if len(self.cleaned_data['nickname']) == 0:
#             self.cleaned_data['nickname'] = 'Anonim'
#         elif len(self.cleaned_data['nickname']) > 30:
#             self.cleaned_data['nickname'] = self.cleaned_data['autor'][:29]
# 
#         return self.cleaned_data['nickname']


from django.http import HttpResponseRedirect
from django.conf import settings
from django.template.response import TemplateResponse
from pygments.lexers import guess_lexer
from apps.wklej.forms import WklejkaForm, WklejkaCaptchaForm
import hashlib
import random

            ## New in 04.
            ## Saves user ip (for spam reasons mostly)
            ## and where paste came from (statistics)
            formatka.ip = request.META['REMOTE_ADDR']
            formatka.wherefrom = "wklej.homepage"

            ### now the fun part - generating hash and other stuff
            if formatka.is_private:
                t = hashlib.sha1()
                salt = t.update(str(random.random()))
                salt = t.hexdigest()
                hash = t.update(salt)
                hash = t.hexdigest()[:11]  # 11 becuase older algo has 10

                formatka.hash = hash

            if request.user.is_authenticated():
                formatka.user = request.user

            temp = guess_lexer(formatka.body)
            formatka.guessed_syntax = temp.aliases[0]
            formatka.save()

            response = HttpResponseRedirect(formatka.get_absolute_url())
            response.set_cookie('highlight', formatka.hl)

            return response

        else:

# Path: apps/wklej/forms.py
# class WklejkaForm(forms.ModelForm):
#     nickname = forms.CharField(required=False)
# 
#     class Meta:
#         model = Wklejka
#         fields = '__all__'
# 
#     def clean_nickname(self):
#         if len(self.cleaned_data['nickname']) == 0:
#             self.cleaned_data['nickname'] = 'Anonim'
#         elif len(self.cleaned_data['nickname']) > 30:
#             self.cleaned_data['nickname'] = self.cleaned_data['nickname'][:29]
# 
#         return self.cleaned_data['nickname']
# 
#     def clean_body(self):
#         if settings.USE_CAPTCHA and check_for_link_spam(self.cleaned_data['body']):
#             raise forms.ValidationError("This paste looks like spam.")
#         return self.cleaned_data['body']
# 
# class WklejkaCaptchaForm(forms.ModelForm):
#     recaptcha = ReCaptchaField()
#     has_captcha = forms.CharField(widget=forms.HiddenInput, required=False)
#     nickname = forms.CharField(required=False)
# 
#     class Meta:
#         model = Wklejka
#         fields = '__all__'
# 
#     def clean_nickname(self):
#         if len(self.cleaned_data['nickname']) == 0:
#             self.cleaned_data['nickname'] = 'Anonim'
#         elif len(self.cleaned_data['nickname']) > 30:
#             self.cleaned_data['nickname'] = self.cleaned_data['autor'][:29]
# 
#         return self.cleaned_data['nickname']


# Path: homepage.py
from django.http import HttpResponseRedirect
from django.conf import settings
from django.template.response import TemplateResponse
from pygments.lexers import guess_lexer
from apps.wklej.forms import WklejkaForm, WklejkaCaptchaForm
import hashlib
import random


            ## New in 04.
            ## Saves user ip (for spam reasons mostly)
            ## and where paste came from (statistics)
            formatka.ip = request.META['REMOTE_ADDR']
            formatka.wherefrom = "wklej.homepage"

            ### now the fun part - generating hash and other stuff
            if formatka.is_private:
                t = hashlib.sha1()
                salt = t.update(str(random.random()))
                salt = t.hexdigest()
                hash = t.update(salt)
                hash = t.hexdigest()[:11]  # 11 becuase older algo has 10

                formatka.hash = hash

            if request.user.is_authenticated():
                formatka.user = request.user

            temp = guess_lexer(formatka.body)
            formatka.guessed_syntax = temp.aliases[0]
            formatka.save()

            response = HttpResponseRedirect(formatka.get_absolute_url())
            response.set_cookie('highlight', formatka.hl)

            return response

        else:

            form = WklejkaCaptchaForm(request.POST,