1

I'm receiving an unexpected indent block for this bit bunch of code:

"""
Forms and validation code for user registration.

Note that all of these forms assume Django's bundle default ``User``
model; since it's not possible for a form to anticipate in advance the
needs of custom user models, you will need to write your own forms if
you're using a custom model.

"""


from django.contrib.auth.models import User
from django.http import HttpResponse, HttpRequest
from django import forms
from django.utils.translation import ugettext_lazy as _


class RegistrationForm(forms.Form):
    """
    Form for registering a new user account.

    Validates that the requested username is not already in use, and
    requires the password to be entered twice to catch typos.

    Subclasses should feel free to add any additional validation they
    need, but should avoid defining a ``save()`` method -- the actual
    saving of collected user data is delegated to the active
    registration backend.

    """
    required_css_class = 'required'

    username = forms.RegexField(regex=r'^[\w.@+-]+$',
                            max_length=30,
                            label=_("Username"),
                            error_messages={'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})
    email = forms.EmailField(label=_("E-mail"))
    password1 = forms.CharField(widget=forms.PasswordInput,
                            label=_("Password"))
    password2 = forms.CharField(widget=forms.PasswordInput,
                            label=_("Password (again)"))
    user_ip = HttpRequest.META['REMOTE_ADDR']

    def clean_email(self):
        """
        Validate that the supplied email address is unique for the
        site.

        """
        if User.objects.filter(email__iexact=self.cleaned_data['email']):
            raise forms.ValidationError(_("This email address is already in use. Please supply a different email address."))
        return self.cleaned_data['email']

    def clean_username(self):
        """
        Validate that the username is alphanumeric and is not already
        in use.

        """
        existing = User.objects.filter(username__iexact=self.cleaned_data['username'])
        if existing.exists():
            raise forms.ValidationError(_("A user with that username already exists."))
        else:
            return self.cleaned_data['username']

    def clean(self):
        """
        Verify that the values entered into the two password fields
        match. Note that an error here will end up in
        ``non_field_errors()`` because it doesn't apply to a single
        field.

        """
        if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
            if self.cleaned_data['password1'] != self.cleaned_data['password2']:
                raise forms.ValidationError(_("The two password fields didn't match."))
        return self.cleaned_data

    def clean_ipaddress(self):
        """
        Verify that the user ip address is valid and not already in use.

        """
        existing_ip = User.objects.get(ip_address=self.cleaned_data['user_ip'])
        if HttpRequest.META['HTTP_X_FORWARDED_FOR'] or HttpRequest.META['HTTP_X_FORWARDED'] or HttpRequest.META['HTTP_FORWARDED_FOR'] or HttpRequest.META['HTTP_CLIENT_IP'] or HttpRequest.META['HTTP_VIA']:
            raise forms.ValidationError(_("We do not allow registration from users behind a proxy"))
        elif HttpRequest.META['HTTP_FORWARDED'] or HttpRequest.META['HTTP_FORWARDED_FOR_IP'] or HttpRequest.META['VIA'] or HttpRequest.META['X_FORWARDED_FOR'] or HttpRequest.META['X_FORWARDED'] or HttpRequest.META['FORWARDED'] or HttpRequest.META['FORWARDED_FOR_IP'] or HttpRequest.META['HTTP_PROXY_CONNECTION']:
            raise forms.ValidationError(_("We do not allow registration from users behind a proxy"))
        elif existing_ip.exists():
            raise forms.ValidationError(_("A user with that ip address already exists."))
        else:
            return self.cleaned_data['user_ip']

class RegistrationFormTermsOfService(RegistrationForm):
    """
    Subclass of ``RegistrationForm`` which adds a required checkbox
    for agreeing to a site's Terms of Service.

    """
    tos = forms.BooleanField(widget=forms.CheckboxInput,
                         label=_(u'I have read and agree to the Terms of Service'),
                         error_messages={'required': _("You must agree to the terms to register")})

class RegistrationFormNoFreeEmail(RegistrationForm):
    """
    Subclass of ``RegistrationForm`` which disallows registration with
    email addresses from popular free webmail services; moderately
    useful for preventing automated spam registrations.

    To change the list of banned domains, subclass this form and
    override the attribute ``bad_domains``.

    """
    bad_domains = ['aim.com', 'aol.com', 'email.com', 'gmail.com',
               'googlemail.com', 'hotmail.com', 'hushmail.com',
               'msn.com', 'mail.ru', 'mailinator.com', 'live.com',
               'yahoo.com']

    def clean_email(self):
        """
        Check the supplied email address against a list of known free
        webmail domains.

        """
        email_domain = self.cleaned_data['email'].split('@')[1]
        if email_domain in self.bad_domains:
            raise forms.ValidationError(_("Registration using free email addresses is prohibited. Please supply a different email address."))
        return self.cleaned_data['email']

Apparently on line 93, after the clean_ipaddress function definition, it picks up an error. I am consistently (to my flawed knowledge, I suppose) using 4 spaces throughout the file. What could be wrong?

Iohannes
  • 267
  • 1
  • 4
  • 13
  • You might get a better error message from `pylint`. You might also see if you have trailing whitespace on a line by running `sed`: `sed -i 's/ \+$//g' *.py` – hughdbrown Oct 12 '13 at 23:17
  • @hughdbrown I ran that line and I still get the same error. Before I ran the line and asked this question, I made sure I wasn't mixing tabs and spaces when indenting. I'm at a loss. It seems that I'm unable to install Pylint. Is it compatible with Python 3? – Iohannes Oct 13 '13 at 00:08
  • It looks to me as if pylint is compatible with python 3.x. I installed a virtualenv with python3.3 and installed pylint into it. It was fine: http://pastebin.com/cPy04hkG – hughdbrown Oct 13 '13 at 00:59
  • @hughdbrown I'm getting a 'cannot find the file specified' error http://pastebin.com/7AqyTfHM – Iohannes Oct 13 '13 at 01:03
  • Have you been holding out on us? Are you using Windows? Sounds like this: http://stackoverflow.com/questions/18755466/pylint-issue-installation-with-windows-and-python-3-2 or maybe this: http://stackoverflow.com/questions/8434981/running-python-scripts-with-subprocess-in-windows-python-code-checker-wrappers – hughdbrown Oct 13 '13 at 01:05
  • @hughdbrown Guess I have, lol. Yeah, I'm on Windows 7. I've seen the second question you posted and sadly I have not found a resolution to the issue. I've been at this for the past 5 hours lol. – Iohannes Oct 13 '13 at 02:08
  • Do yourself a favor and install a real operating system. +1 because your question does not deserve a downvote. – dan-klasson Oct 13 '13 at 20:26

0 Answers0