Search code examples
pythondjangopylint

How to clear some warning messages using Django and Python


I need some help. I am getting some warnings while running my code using pylint and I need to solve those. I am explaining my code below.

W: 11, 0: String statement has no effect (pointless-string-statement)
C: 15, 4: Missing method docstring (missing-docstring)
R: 15, 4: Method could be a function (no-self-use)
W: 18, 4: String statement has no effect (pointless-string-statement)
C: 19, 4: Missing method docstring (missing-docstring)
E: 21,11: Instance of 'UserCreationForm' has no 'is_valid' member (no-member)
R: 19, 4: Method could be a function (no-self-use)
C: 28, 4: Missing method docstring (missing-docstring)
R: 28, 4: Method could be a function (no-self-use)
W: 31, 4: String statement has no effect (pointless-string-statement)
C: 32, 4: Missing method docstring (missing-docstring)
E: 34,11: Instance of 'AuthenticationForm' has no 'is_valid' member (no-member)
R: 32, 4: Method could be a function (no-self-use)
C: 38, 0: Missing function docstring (missing-docstring)
W: 41, 0: String statement has no effect (pointless-string-statement)
C: 43, 0: Missing function docstring (missing-docstring)
C: 72, 0: Missing function docstring (missing-docstring)
W: 75, 0: String statement has no effect (pointless-string-statement)
C: 77, 0: Missing function docstring (missing-docstring)
W: 98, 0: String statement has no effect (pointless-string-statement)
C:100, 0: Missing function docstring (missing-docstring)

My python file is given below.

views.py:

""" coding: utf-8 """
from __future__ import unicode_literals
from django.shortcuts import render, redirect
from django.contrib.auth import login
from django.views.generic import View
from django.contrib.auth.forms import (UserCreationForm, AuthenticationForm)
from lxml import etree
import random
import xml.dom.minidom as m

""" this class is used for user signup """

class Signup(View):
    """ this function used to get the sign up form """
    def get(self, request):
        form = UserCreationForm()
        return render(request, 'booking/signup.html', {'form': form})
    """ this function used for post the sign up data """
    def post(self, request):
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('login')


class AuthLogin(View):
    """ this function used to get the login form """
    def get(self, request):
        form = AuthenticationForm()
        return render(request, 'booking/login.html', {'form': form})
    """ this function used for post the login data """
    def post(self, request):
        form = AuthenticationForm(None, request.POST or None)
        if form.is_valid():
            login(request, form.get_user())
        return redirect('/')

def bmr(request):
    return render(request, 'booking/bmr.html', {})

""" this function is used for save all the data """

def insert(request):
    if request.method == 'POST':
        location_name = request.POST.get('lname')
        rname = request.POST.get('rname')
        seat = request.POST.get('seat')
        projector = request.POST.get('projector')
        video = request.POST.get('video')
        num = str(random.randint(100000000000, 999999999999))
        location_name = location_name[0:255]
        rname = rname[0:255]
        seat = seat[0:10]
        doc = m.parse("roomlist.xml")
        valeurs = doc.getElementsByTagName("roomlist")[0]
        element = doc.createElement("location")
        element.setAttribute("name", location_name)
        el1 = element.appendChild(doc.createElement("room"))
        el1.setAttribute("id", num)
        el2 = el1.appendChild(doc.createElement("roomname"))
        el2.appendChild(doc.createTextNode(rname))
        el3 = el1.appendChild(doc.createElement("noseats"))
        el3.appendChild(doc.createTextNode(seat))
        el4 = el1.appendChild(doc.createElement("projectorscreen"))
        el4.appendChild(doc.createTextNode(projector))
        el5 = el1.appendChild(doc.createElement("videoconf"))
        el5.appendChild(doc.createTextNode(video))
        valeurs.appendChild(element)
        doc.writexml(open("roomlist.xml", "w"))
    return render(request, 'booking/bmr.html', {})

def home(request):
    return render(request, 'booking/home.html', {})

""" This function is used for disply all the data """

def view_book(request):
    doc = m.parse("roomlist.xml")
    staffs = doc.getElementsByTagName("location")
    root = []
    for staff in staffs:
        lname = staff.getAttribute("name")
        roomname = staff.getElementsByTagName(
            "roomname")[0].firstChild.nodeValue
        seat = staff.getElementsByTagName("noseats")[0].firstChild.nodeValue
        project = staff.getElementsByTagName(
            "projectorscreen")[0].firstChild.nodeValue
        video = staff.getElementsByTagName("videoconf")[0].firstChild.nodeValue
        root.append(
            {'lname': lname,
             'roomname': roomname,
             'seat': seat,
             'project': project,
             'video': video})
    return render(request, 'booking/view_book.html', {'people': root})


""" This function is used to sear the data as per room name """

def search(request):
    per = []
    if request.method == 'POST':
        rname = request.POST.get('rname')
        tree = etree.parse('roomlist.xml')
        result = tree.xpath(".//roomname[text()=$rmname]/./..", rmname=rname)
        for user in result:
            per.append(
                {'roomname': user.find('roomname').text,
                 'seat': user.find('noseats').text,
                 'project': user.find('projectorscreen').text,
                 'video': user.find('videoconf').text})
    return render(request, 'booking/home.html', {'people': per})

Here I need to close maximum warning messages so that the rating will up. Please do the needfull help.


Solution

  • It's a trival fix. Your doc strings are misplaced. They should look more like this:

    class Signup(View):
        """ this class is used for user signup """
    
        def get(self, request):
            """ this class is used for user signup """
            form = UserCreationForm()
            return render(request, 'booking/signup.html', {'form': form})
    
        def post(self, request):
            """ this function used for post the sign up data """
            form = UserCreationForm(request.POST)
            if form.is_valid():
                form.save()
                return redirect('login')
    

    E: 34,11: Instance of 'AuthenticationForm' has no 'is_valid' member (no-member)

    As for this one , pylint is clearly mistaken (assuming of course that your AuthenticationForm is a sublcass of forms.Form)

    R: 28, 4: Method could be a function (no-self-use)

    If you are wondering about this, pylint thinks that get and post shouldn't be methods of Signup. That's because you are not using the self parameter that is required to be the first parameter of any method in a class. But once again pylint is clearly wrong because class based views work exactly like this. You might want to customize your pylint installation for django. or add this

    def get(self, request):  #pylint disable=no-self-use