0

我编写了一个自定义身份验证脚本,它给了我一个内部服务器错误,不太确定是什么导致了这个或如何获得详细的错误报告。

allowed_hosts设置为[]但 DEBUG 设置为 True 所以这无关紧要。我通过 IP 地址直接访问该站点。

这是一个创业周末,所以快速的帮助将是最好的!谢谢你!使用Django 1.5.5

import uuid, datetime, pickle
import project.models as models
from django.contrib import auth

from django.template import Context

from django.template.loader import get_template
from django.http import HttpResponse, Http404, HttpResponseRedirect, HttpResponseServerError, HttpResponseRedirect
from django.shortcuts import render_to_response, redirect

class user(object):
    userName        = str()
    userID          = uuid.UUID(int=0)

    _exists         = False
    _authenticated  = False

    def is_authenticated(self):
        return self._authenticated

    def __str__(self):
        return str(self.__dict__.copy())

    def __getstate__(self):
        return self.__dict__.copy()

    def __setstate__(self, dict):
        self.__dict__ = dict

    def __init__(self, username=None):
        if username:
            self.initialize(username)

    def initialize(self, username):
        self.userName = username
        model = models.User.objects.filter(userName=self.userName).all()
        if len(model) == 1:
            model = model[0]
            self.data = model
            self._exists = True
            self.userID = model.id          
        else:
            self._exists = False

    def authenticate(self, password):
        self._authenticated = False
        if self._exists:
            import hashlib
            hash = hashlib.md5('%s%s' % (str(password), self.data.pwSalt)).hexdigest()
            if hash == self.data.pwHash:
                self._authenticated = True
                return True
        return False

    def updateUser(self):
        self.initialize(models.User.objects.filter(id=self.userID).get().userName)

    def mkContext(self, context):
        c = context
        c['user'] = self
        return c

class userMiddleWare(object):

    def authenticated(self, request):
        if (request.session.get('user', False)):
            request.user = request.session["user"]
            return True
        return False

    def authenticate(self, request):
        if (request.method == 'POST' and ('username' in request.POST) and ('password' in request.POST)):
            u = user(str(request.POST['username']))
            if (u.authenticate(str(request.POST['password']))):
                request.session['user'] = u
                request.user = u
                try:
                    request.session.save()
                except:
                    e = "Session wasn't saved or something?"
                return True
        request.user = user()
        return False

    def authorize(self, request):
        return True

    def process_request(self, request):

        next = False

        if request.method == 'POST':
            if request.POST.get('username', False) and request.POST.get('password', False):
                next = request.POST.get('next', False)
                if not self.authenticated(request):
                    self.authenticate(request)
                    if request.session.has_key('user'):
                        if not request.session['user']._authenticated:
                            return redirect('project.views.main')
                    else:
                        return redirect('project.views.main')

        # context = Context(request)

        if request.session.has_key('user'):
            request.user = request.session['user']
            if next:
                return redirect(next)
        else:
            request.user = user()

    def process_response(self, request, response):
        try:
            if hasattr(request, 'session'):
                request.session.save()
        except:
            e = "Session wasn't saved or something?"    
        return response

    def process_exception(self, request, exception):
        return

        # import settings
        # # if getattr(settings, 'DEBUG', False):
        # #     return

        # import traceback

        # timestamp = str(datetime.datetime.now())
        # if exception == 'process_request' or exception=='process_response':
        #   errtype = exception
        # else:
        #   errtype = 'Application Error'

        # if hasattr(exception, 'args'):
        #   message = getattr(exception, 'args')
        # else:
        #   message = 'Unknown'

        # if hasattr(request, 'path'):
        #   path = getattr(request, 'path', None)
        # else:
        #   path = 'Unknown'

        # if (hasattr(request, 'session') and getattr(request, 'session').get('user', None)):
        #   user = request.session['user'].userName

        # try:
        #   tb = ''
        #   for l in traceback.format_stack():
        #       tb += l
        #   trace = tb
        # except:
        #   trace = []

        # context = Context({'errtype':errtype, 'message':message, 'path':path, 'trace':trace, 'user':user, 'client':client})

        # return HttpResponseServerError(get_template('error.html').render(context))
4

0 回答 0