コード例 #1
0
ファイル: middleware.py プロジェクト: amnuay/biostar-central
    def process_request(self, request):
        user = request.user
        sess = Session(request)
        
        # check suspended status for users
        if user.is_authenticated() and ( user.profile.suspended ):
            logout(request)
            messages.error(request, 'Sorry, this account has been suspended. Please contact the administrators.')
            return
            
        # handle anonymous users
        if not user.is_authenticated():
            # anonymous users
            request.user.can_moderate = False
            return 
            
        # at this point we only have authenticated users
        profile = user.get_profile()
        
        # setting a handy shortcut
        request.user.can_moderate = profile.can_moderate
        
        # only write to database intermittently
        expired = (datetime.now() - profile.last_visited).seconds
            
        if expired > settings.SESSION_UPDATE_TIME:
            counts = generate_counts(request)
            sess.set_counts(counts)
            sess.save()

            # save the last update time
            profile.update_expiration()

            # try to award badges
            awards.instant(request)
コード例 #2
0
    def process_request(self, request):
        user = request.user
        sess = Session(request)
        
        # check suspended status for users
        if user.is_authenticated() and ( user.profile.suspended ):
            logout(request)
            messages.error(request, 'Sorry, this account has been suspended. Please contact the administrators.')
            return
            
        # handle anonymous users
        if not user.is_authenticated():
            # anonymous users
            request.user.can_moderate = False
            if request.path == "/":
                messages.info(request, 'Welcome to DNAnexus Answers! This site is for Questions and Answers about the DNAnexus Platform.')
            return 
            
        # at this point we only have authenticated users
        profile = user.get_profile()
        
        # setting a handy shortcut
        request.user.can_moderate = profile.can_moderate
        
        # only write to database intermittently
        expired = (datetime.now() - profile.last_visited).seconds
            
        if expired > settings.SESSION_UPDATE_TIME:
            counts = generate_counts(request)
            sess.set_counts(counts)
            sess.save()

            since = user.profile.last_visited

            # votes since the last visit
            vote_count = models.Vote.objects.filter(post__author=user, date__gt=since).count()
            if vote_count > 0:
                msg = '<i class="icon-info-sign"></i> <b>%s</b> upvotes since your last visit! \
                    See your <a href="/u/%s/votes/">recent upvoted posts</a>!' % (vote_count, user.id)
                messages.info(request, msg)

            # save the last update time
            profile.update_expiration()
            
            # create nagging message for fixme posts
            fixme = models.Post.objects.filter(type=POST_FIXME, author=user, status=POST_OPEN)
            if fixme:
                first = fixme[0]
                messages.error(request, 'You have a post that does not conform the requirements. Please edit it: <a href="%s">%s</a>' % (first.get_absolute_url(), first.title)) 

            # remind user about voting every six weeks since the last vote
            # and only nag people with lower reputations ;-)
            if user.profile.score < 300:
                since = datetime.now() - timedelta(weeks=6)
                votes = models.Vote.objects.filter(author=user, date__gt=since)[:1]
                if not votes:
                    messages.info(request, '<i class="icon-info-sign"></i> Remember to <b>vote</b> on posts that you find useful!') 
                
            # try to award badges
            awards.instant(request)
コード例 #3
0
ファイル: middleware.py プロジェクト: nuin/biostar-central
 def process_request(self, request):
     
     if request.user.is_authenticated():
         user = request.user
         profile = user.get_profile()
         
         if profile.status == USER_SUSPENDED:
             logout(request)
             messages.error(request, 'Sorry, this account has been suspended. Please contact the administrators.')
             return None
         
         now = datetime.datetime.now()
         diff = (now - profile.last_visited).seconds
         
         
         # Prevent writing to the database too often
         if diff > settings.SESSION_UPDATE_TIME:
            
            # create nagging message for fixme posts
             fixme = models.Post.objects.filter(type=POST_FIXME, author=user, status=POST_OPEN)
             if fixme:
                 first = fixme[0]
                 messages.error(request, 'You have a post that does not conform the requirements. Please edit it: <a href="%s">%s</a>' % (first.get_absolute_url(), first.title) )
         
             last = user.profile.last_visited
            
             questions = models.Post.objects.filter(type=POST_QUESTION, creation_date__gt=last).count()
             videos = models.Post.objects.filter(type=POST_VIDEO, creation_date__gt=last).count()
             unanswered = models.Post.objects.filter(type=POST_QUESTION, status=POST_OPEN, answer_count=0,  creation_date__gt=last).count()
             forum  = models.Post.objects.filter(type=POST_FORUM, answer_count=0, creation_date__gt=last).count()
             tutorials = models.Post.objects.filter(type=POST_TUTORIAL, answer_count=0, creation_date__gt=last).count()
             planet = models.Post.objects.filter(type=POST_BLOG,  creation_date__gt=last).count()
             tools  = models.Post.objects.filter(type=POST_TOOL,  creation_date__gt=last).count()
             counts = dict(planet=planet,  unanswered=unanswered, questions=questions, tutorials=tutorials, forum=forum, tools=tools, videos=videos)
             request.session[SESSION_POST_COUNT] = counts 
             
             user.profile.last_visited = now
             user.profile.save()
            
             awards.instant(request)
             
             # add the visit to the database
             try:
                 # trying to establish the IP location
                 ip1 = request.META.get('REMOTE_ADDR', '')
                 ip2 = request.META.get('HTTP_X_FORWARDED_FOR','').split(",")[0].strip()
                 ip  = ip1 or ip2 or '0.0.0.0'
                 models.Visit.objects.create(ip=ip, user=user)
             except Exception ,exc:
                 print '*** ip handling error %s' % exc
                 
         # a handy shortcut
         request.user.can_moderate = profile.can_moderate
コード例 #4
0
    def process_request(self, request):
        user = request.user
        sess = Session(request)
        
        # check suspended status for users
        if user.is_authenticated() and ( user.profile.suspended ):
            logout(request)
            messages.error(request, 'Sorry, this account has been suspended. Please contact the administrators.')
            return
            
        # handle anonymous users
        if not user.is_authenticated():
            # anonymous users
            request.user.can_moderate = False
            if request.path == "/":
                messages.info(request, 'Welcome to BioStar! Questions and Answers on Bioinformatics and Genomics!')
            return 
            
        # at this point we only have authenticated users
        profile = user.get_profile()
        
        # setting a handy shortcut
        request.user.can_moderate = profile.can_moderate
        
        # only write to database intermittently
        expired = (datetime.now() - profile.last_visited).seconds
            
        if expired > settings.SESSION_UPDATE_TIME:
            counts = generate_counts(request)
            sess.set_counts(counts)
            sess.save()
            
            # save the last update time
            profile.update_expiration()
            
            # create nagging message for fixme posts
            fixme = models.Post.objects.filter(type=POST_FIXME, author=user, status=POST_OPEN)
            if fixme:
                first = fixme[0]
                messages.error(request, 'You have a post that does not conform the requirements. Please edit it: <a href="%s">%s</a>' % (first.get_absolute_url(), first.title)) 

            # remind user about voting every six weeks since the last vote
            # and only nag people with lower reputations ;-)
            if user.profile.score < 300:
                since = datetime.now() - timedelta(weeks=6)
                votes = models.Vote.objects.filter(author=user, date__gt=since)[:1]
                if not votes:
                    messages.info(request, '<i class="icon-info-sign"></i> Remember to <b>vote</b> on posts that you find useful!') 
                
            # try to award badges
            awards.instant(request)
コード例 #5
0
 def process_request(self, request):
     
     if request.user.is_authenticated():
         user = request.user
         profile = user.get_profile()
         
         if profile.status == USER_SUSPENDED:
             logout(request)
             messages.error(request, 'Sorry, this account has been suspended. Please contact the administrators.')
             return None
         
         now = datetime.datetime.now()
         diff = (now - profile.last_visited).seconds
         
         # Prevent writing to the database too often
         if diff > MINIMUM_TIME:
            
             last = user.profile.last_visited
            
             questions = models.Post.objects.filter(type=POST_QUESTION, creation_date__gt=last).count()
             unanswered = models.Post.objects.filter(type=POST_QUESTION, status=POST_OPEN, answer_count=0,  creation_date__gt=last).count()
             forum  = models.Post.objects.filter(type=POST_FORUM, answer_count=0, creation_date__gt=last).count()
             tutorials = models.Post.objects.filter(type=POST_TUTORIAL, answer_count=0, creation_date__gt=last).count()
             planet = models.Post.objects.filter(type=POST_BLOG,  creation_date__gt=last).count()
             
             counts = dict(planet=planet,  unanswered=unanswered, questions=questions, tutorials=tutorials, forum=forum)
             request.session[SESSION_POST_COUNT] = counts 
             models.UserProfile.objects.filter(user=user).update(last_visited=now)
            
             awards.instant(request)
             
             # add the visit to the database
             try:
                 # trying to establish the IP location
                 ip1 = request.META.get('REMOTE_ADDR', '')
                 ip2 = request.META.get('HTTP_X_FORWARDED_FOR','').split(",")[0].strip()
                 ip  = ip1 or ip2 or '0.0.0.0'
                 models.Visit.objects.create(ip=ip, user=user)
             except Exception ,exc:
                 print '*** ip handling error %s' % exc
                 
         # a handy shortcut
         request.user.can_moderate = profile.can_moderate
コード例 #6
0
    def process_request(self, request):

        if request.user.is_authenticated():
            user = request.user
            profile = user.get_profile()

            if profile.status == USER_SUSPENDED:
                logout(request)
                messages.error(
                    request,
                    'Sorry, this account has been suspended. Please contact the administrators.'
                )
                return None

            now = datetime.datetime.now()
            diff = (now - profile.last_visited).seconds

            # Prevent writing to the database too often
            if diff > settings.SESSION_UPDATE_TIME:

                # create nagging message for fixme posts
                fixme = models.Post.objects.filter(type=POST_FIXME,
                                                   author=user,
                                                   status=POST_OPEN)
                if fixme:
                    first = fixme[0]
                    messages.error(
                        request,
                        'You have a post that does not conform the requirements. Please edit it: <a href="%s">%s</a>'
                        % (first.get_absolute_url(), first.title))

                last = user.profile.last_visited

                questions = models.Post.objects.filter(
                    type=POST_QUESTION, creation_date__gt=last).count()
                videos = models.Post.objects.filter(
                    type=POST_VIDEO, creation_date__gt=last).count()
                unanswered = models.Post.objects.filter(
                    type=POST_QUESTION,
                    status=POST_OPEN,
                    answer_count=0,
                    creation_date__gt=last).count()
                forum = models.Post.objects.filter(
                    type=POST_FORUM, answer_count=0,
                    creation_date__gt=last).count()
                tutorials = models.Post.objects.filter(
                    type=POST_TUTORIAL, answer_count=0,
                    creation_date__gt=last).count()
                planet = models.Post.objects.filter(
                    type=POST_BLOG, creation_date__gt=last).count()
                tools = models.Post.objects.filter(
                    type=POST_TOOL, creation_date__gt=last).count()
                counts = dict(planet=planet,
                              unanswered=unanswered,
                              questions=questions,
                              tutorials=tutorials,
                              forum=forum,
                              tools=tools,
                              videos=videos)
                request.session[SESSION_POST_COUNT] = counts

                user.profile.last_visited = now
                user.profile.save()

                awards.instant(request)

                # add the visit to the database
                try:
                    # trying to establish the IP location
                    ip1 = request.META.get('REMOTE_ADDR', '')
                    ip2 = request.META.get('HTTP_X_FORWARDED_FOR',
                                           '').split(",")[0].strip()
                    ip = ip1 or ip2 or '0.0.0.0'
                    models.Visit.objects.create(ip=ip, user=user)
                except Exception, exc:
                    print '*** ip handling error %s' % exc

            # a handy shortcut
            request.user.can_moderate = profile.can_moderate