def register(request): username = request.data['username'] password = request.data['password'] # Authentication user = None try: user = User.objects.get(username=username) if not user.check_password(password): user = None elif not user.is_active: logger.error('Login attempt failed for deactivated user %s', username) return error('unauthorized', status=status.HTTP_401_UNAUTHORIZED) except User.DoesNotExist: pass # Filter and log failed attempts if not user: logger.error('Login attempt failed for "%s":"%s"', username, password) return error('unauthorized', status=status.HTTP_401_UNAUTHORIZED) current_time = timezone.now() station_id = None # Expire older sessions if user.kind == User.STATION: station = user.station sessions = Session.objects.filter(user=user, expired_on__gte=current_time).order_by('created_on') if len(sessions) >= station.max_sessions: old_session = sessions.first() old_session.expired_on = current_time old_session.save() name = station.name station_id = station.external_id else: # ADMIN and SUPERVISOR old_sessions = Session.objects.filter(user=user, expired_on__gte=current_time) for s in old_sessions: s.expired_on = current_time s.save() name = username # Issue session token session = Session.generate(user=user) session.save() return Response({ 'status': 'success', 'name': name, 'station_id': 0 if station_id is None else station_id, 'token': session.token, })
def initialize(self,request,response): ''' init webapp.RequestHandler ''' webapp.RequestHandler.initialize(self,request,response) self.setting = settings.Setting() self.template_value={'setting':self.setting} #handler xfox-session-key cookies = parse_cookie(self.request.headers.get("Cookie","")) self.session_key = cookies.get('xfox-session-key',None) logging.info("session_key:%s" % (self.session_key)) self.user = None self.role = ['G'] #Guest User if not self.session_key is None and len(self.session_key)==32: self.user = Session.get_user_by_session(self.session_key) if not self.user is None: self.role = self.user.role self.template_value['user']=self.user self.template_value['role']=self.role #handler os self.os = 'default' #html5 user_agent = self.request.headers.get("User-Agent",'') #if "MSIE" in user_agent: #self.os = 'ie' #handler not endswith / self.template_value['os']=self.os self.p = self.request.path.lower() #path self.template_value['tp']=self.p if not self.request.path.endswith("/"): return self.redirect(self.request.path+"/",True)