49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
from django.core import exceptions as djangocore_exceptions
|
|
from django.contrib.auth.context_processors import auth
|
|
from django.utils.html import strip_tags
|
|
from django.contrib.auth import logout
|
|
from django.utils.timezone import now
|
|
from django.http import Http404
|
|
from django.db import transaction
|
|
import application.models as models
|
|
|
|
class Objectify(object):
|
|
def __init__(self,d):
|
|
self.__dict__=d
|
|
|
|
acceptableCharsInNames = [chr(i) for i in list(range(ord('A'),ord('Z')+1))+list(range(ord('a'),ord('z')+1))+list(range(ord('0'),ord('9')+1))]
|
|
|
|
def getIpFromRequest(rq):
|
|
ipaddress = ''
|
|
x_forwarded_for = rq.META.get('HTTP_X_FORWARDED_FOR')
|
|
if x_forwarded_for:
|
|
ipaddress = x_forwarded_for.split(',')[-1].strip()
|
|
else:
|
|
ipaddress = rq.META.get('REMOTE_ADDR')
|
|
return ipaddress
|
|
|
|
class BusinessException(Exception): pass
|
|
class SiteBannedException(BusinessException): pass
|
|
class NotFoundException(Http404,BusinessException): pass
|
|
class UnexpectedValue(BusinessException,ValueError): pass
|
|
|
|
class BusinessLogic(object):
|
|
_request = None
|
|
_ip = None
|
|
_user = None
|
|
def __init__(self, request):
|
|
self._request = request
|
|
self._ip = getIpFromRequest(request)
|
|
user = auth(request)['user']
|
|
bans = models.SiteBan.objects.filter(active=True).filter(user__pk=user.pk).order_by('-modified')
|
|
if len(bans) > 0:
|
|
logout(request)
|
|
raise SiteBannedException(bans[0].notes)
|
|
self._user = user
|
|
@property
|
|
def user(self):
|
|
return self._user
|
|
@property
|
|
def logged_in(self):
|
|
return self.user.is_authenticated
|