# Path: apps/wklej/models.py
# class Wklejka(models.Model):
#     """
#     This model represents a single paste, both for anonymous and authenticated
#     pasters.
#     """
# 
#     nickname = models.CharField(max_length=30, null=True)
#     user = models.ForeignKey(User, blank=True, null=True)
#     body = models.TextField()
#     pub_date = models.DateTimeField(auto_now_add=True)
#     mod_date = models.DateTimeField(auto_now=True)
#     hash = models.CharField(max_length=45, blank=True)
# 
#     # syntax
#     syntax = models.CharField(max_length=30, choices=LEXERS,
#                               blank=True, null=True)
#     guessed_syntax = models.CharField(max_length=30, blank=True, null=True)
# 
#     # inheritance:
#     parent = models.ForeignKey('self', blank=True, null=True)
# 
#     # flags:
#     is_private = models.BooleanField(default=False)
#     is_deleted = models.BooleanField(default=False)
#     is_spam = models.BooleanField(default=False)
# 
#     # new:
#     comment = models.TextField(blank=True, null=True)
#     wherefrom = models.CharField(max_length=100, blank=True, null=True)
#     ip = models.CharField(max_length=20, blank=True, null=True)
# 
#     class Meta:
#         verbose_name = "Wklejka"
#         verbose_name_plural = "Wklejki"
#         ordering = ['-pub_date']
#         db_table = 'wklej_wklejka'
# 
#     def __unicode__(self):
#         return "%s at %s" % (self.author, str(self.pub_date))
# 
#     @property
#     def author(self):
#         return self.user if self.user else self.nickname
# 
#     @property
#     def is_public(self):
#         return not self.is_private
# 
#     @property
#     def hl(self):
#         return self.syntax or self.guessed_syntax
# 
#     def get_absolute_url(self):
#         if self.is_private:
#             return self.get_hash_url()
#         return self.get_id_url()
# 
#     def get_id_url(self):
#         return reverse('single', kwargs={'id': self.id})
# 
#     def get_hash_url(self):
#         return reverse('single', kwargs={"hash": self.hash})
# 
#     def get_del_url(self):
#         if self.is_private:
#             return reverse('delete', kwargs={"hash": self.hash})
#         return reverse('delete', kwargs={"id": self.id})
# 
#     def get_txt_url(self):
#         if self.is_private:
#             return reverse('txt', kwargs={"hash": self.hash})
#         return reverse('txt', kwargs={"id": self.id})
# 
#     def get_download_url(self):
#         if self.is_private:
#             return reverse('download', kwargs={"hash": self.hash})
#         return reverse('download', kwargs={"id": self.id})
# 
#     def is_parent(self):
#         if self.wklejka_set.all():
#             return True
#         return False
# 
#     def children(self):
#         return self.wklejka_set.all()
# 
#     def children_count(self):
#         return self.children().count()
# 
#     def is_child(self):
#         if self.parent:
#             return True
#         return False
# 
#     def get_10_lines(self):
#         return "\n".join(self.body.splitlines()[:10])
# 
# Path: lib/antispam.py
# def check_for_link_spam(code):
#     """It's spam if more than 30% of the text are links."""
#     spans = [x.span() for x in _link_re.finditer(code)]
#     if len(spans) > MAX_LINK_PERCENTAGE:
#         return True
#     return (sum(starmap(sub, spans)) * -100) / (len(code) or 1) \
#         > MAX_LINK_PERCENTAGE
# 
# Path: apps/wklej/models.py
# LEXERS = [
#     ('python', "Python"),
#     ('perl', "Perl"),
#     ('ruby', "Ruby"),
#     ('c', 'C'),
#     ('bash', "Bash"),
#     ('java', "Java"),
#     ('html+django', "Html+Django"),
#     ('xml', "XML"),
#     ('mysql', "MySQL"),
#     ('cpp', "C++"),
#     ('js', 'JavaScript'),
# ] + sorted([
#     (x[1][0], x[0]) for x in all_lexers if x[1][0] not in BANNED_LEXERS
# ])


from django import forms
from apps.wklej.models import Wklejka
from lib.antispam import check_for_link_spam
from recaptcha_app.fields import ReCaptchaField
from apps.wklej.models import LEXERS
from django.conf import settings


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']


# TODO: FIXME: Do this with inheritance
class WklejkaCaptchaForm(forms.ModelForm):
    recaptcha = ReCaptchaField()
    has_captcha = forms.CharField(widget=forms.HiddenInput, required=False)
    nickname = forms.CharField(required=False)

    class Meta:

# Path: apps/wklej/models.py
# class Wklejka(models.Model):
#     """
#     This model represents a single paste, both for anonymous and authenticated
#     pasters.
#     """
# 
#     nickname = models.CharField(max_length=30, null=True)
#     user = models.ForeignKey(User, blank=True, null=True)
#     body = models.TextField()
#     pub_date = models.DateTimeField(auto_now_add=True)
#     mod_date = models.DateTimeField(auto_now=True)
#     hash = models.CharField(max_length=45, blank=True)
# 
#     # syntax
#     syntax = models.CharField(max_length=30, choices=LEXERS,
#                               blank=True, null=True)
#     guessed_syntax = models.CharField(max_length=30, blank=True, null=True)
# 
#     # inheritance:
#     parent = models.ForeignKey('self', blank=True, null=True)
# 
#     # flags:
#     is_private = models.BooleanField(default=False)
#     is_deleted = models.BooleanField(default=False)
#     is_spam = models.BooleanField(default=False)
# 
#     # new:
#     comment = models.TextField(blank=True, null=True)
#     wherefrom = models.CharField(max_length=100, blank=True, null=True)
#     ip = models.CharField(max_length=20, blank=True, null=True)
# 
#     class Meta:
#         verbose_name = "Wklejka"
#         verbose_name_plural = "Wklejki"
#         ordering = ['-pub_date']
#         db_table = 'wklej_wklejka'
# 
#     def __unicode__(self):
#         return "%s at %s" % (self.author, str(self.pub_date))
# 
#     @property
#     def author(self):
#         return self.user if self.user else self.nickname
# 
#     @property
#     def is_public(self):
#         return not self.is_private
# 
#     @property
#     def hl(self):
#         return self.syntax or self.guessed_syntax
# 
#     def get_absolute_url(self):
#         if self.is_private:
#             return self.get_hash_url()
#         return self.get_id_url()
# 
#     def get_id_url(self):
#         return reverse('single', kwargs={'id': self.id})
# 
#     def get_hash_url(self):
#         return reverse('single', kwargs={"hash": self.hash})
# 
#     def get_del_url(self):
#         if self.is_private:
#             return reverse('delete', kwargs={"hash": self.hash})
#         return reverse('delete', kwargs={"id": self.id})
# 
#     def get_txt_url(self):
#         if self.is_private:
#             return reverse('txt', kwargs={"hash": self.hash})
#         return reverse('txt', kwargs={"id": self.id})
# 
#     def get_download_url(self):
#         if self.is_private:
#             return reverse('download', kwargs={"hash": self.hash})
#         return reverse('download', kwargs={"id": self.id})
# 
#     def is_parent(self):
#         if self.wklejka_set.all():
#             return True
#         return False
# 
#     def children(self):
#         return self.wklejka_set.all()
# 
#     def children_count(self):
#         return self.children().count()
# 
#     def is_child(self):
#         if self.parent:
#             return True
#         return False
# 
#     def get_10_lines(self):
#         return "\n".join(self.body.splitlines()[:10])
# 
# Path: lib/antispam.py
# def check_for_link_spam(code):
#     """It's spam if more than 30% of the text are links."""
#     spans = [x.span() for x in _link_re.finditer(code)]
#     if len(spans) > MAX_LINK_PERCENTAGE:
#         return True
#     return (sum(starmap(sub, spans)) * -100) / (len(code) or 1) \
#         > MAX_LINK_PERCENTAGE
# 
# Path: apps/wklej/models.py
# LEXERS = [
#     ('python', "Python"),
#     ('perl', "Perl"),
#     ('ruby', "Ruby"),
#     ('c', 'C'),
#     ('bash', "Bash"),
#     ('java', "Java"),
#     ('html+django', "Html+Django"),
#     ('xml', "XML"),
#     ('mysql', "MySQL"),
#     ('cpp', "C++"),
#     ('js', 'JavaScript'),
# ] + sorted([
#     (x[1][0], x[0]) for x in all_lexers if x[1][0] not in BANNED_LEXERS
# ])


# Path: apps/wklej/forms.py
from django import forms
from apps.wklej.models import Wklejka
from lib.antispam import check_for_link_spam
from recaptcha_app.fields import ReCaptchaField
from apps.wklej.models import LEXERS
from django.conf import settings



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']


# TODO: FIXME: Do this with inheritance
class WklejkaCaptchaForm(forms.ModelForm):
    recaptcha = ReCaptchaField()
    has_captcha = forms.CharField(widget=forms.HiddenInput, required=False)
    nickname = forms.CharField(required=False)

    class Meta:

        model = Wklejka