コード例 #1
0
ファイル: ratelimit.py プロジェクト: mediapop/mplib
    def view_wrapper(self, request, fn, *args, **kwargs):
        if not self.should_ratelimit(request):
            return fn(request, *args, **kwargs)
        
        counts = self.get_counters(request).values()
        
        # Increment rate limiting counter
        self.cache_incr(self.current_key(request))
        
        # Have they failed?
        if sum(counts) >= self.requests:

            logger.info ('Rate limit hit by ip %s', ip_address(request), extra = {
                # Optionally pass a request and we'll grab any information we can
                'request': request,
            })
            
            return self.disallowed(request)
        
        return fn(request, *args, **kwargs)
コード例 #2
0
ファイル: ratelimit.py プロジェクト: mediapop/mplib
 def key_extra(self, request):
     # By default, their IP address is used
     return ip_address(request)
コード例 #3
0
ファイル: facebook_lib.py プロジェクト: mediapop/mplib
    def current_user(self):
        """Returns the active user, or None if the user has not logged in."""
        if not hasattr(self, "_current_user"):

            self._current_user = None

            if self.cookie:
                cookie = self.cookie
            else:
                cookie = self.parse_auth()

            if cookie:
                #                print ('we have cookie', cookie["uid"])
                # Store a local instance of the user data so we don't need
                # a round-trip to Facebook on every request
                try:
                    user = FacebookUser.objects.get(uid=cookie["uid"])
                except FacebookUser.DoesNotExist:
                    try:
                        graph = facebook.GraphAPI(cookie["access_token"])

                        attempt_counter = 0
                        ATTEMPT_LIMIT = 10

                        while attempt_counter < ATTEMPT_LIMIT:
                            try:
                                profile = graph.get_object("me")
                                break
                            except (IOError) as e:
                                attempt_counter += 1

                                if attempt_counter == ATTEMPT_LIMIT:
                                    raise

                                logger.info("mplib.current_user", extra={"data": {"exception": e}})

                    except facebook.GraphAPIError:
                        user = None
                    else:
                        user = FacebookUser(
                            pk=str(profile["id"]),
                            first_name=profile.get("first_name"),
                            last_name=profile.get("last_name"),
                            locale=profile.get("locale"),
                            gender=profile.get("gender", ""),
                            time_zone=profile.get("timezone", ""),
                            email=profile.get("email", ""),
                            access_token=cookie["access_token"],
                        )

                        try:
                            user.save()
                        except IntegrityError as e:
                            logger.info(
                                "IntegrityError saving user", extra={"data": {"exception": e, "profile": profile}}
                            )

                            # this user has already been saved somehow, let's skip
                            # over this problem and grab him from the db
                            user = FacebookUser.objects.get(pk=str(profile["id"]))

                        ip = FacebookUserIP()

                        ip.fb_user = user
                        ip.ip_address = ip_address(self.request)

                        ip.save()

                else:
                    if user.access_token != cookie["access_token"]:
                        user.access_token = cookie["access_token"]

                        ip = FacebookUserIP()

                        ip.fb_user = user
                        ip.ip_address = ip_address(self.request)

                        ip.save()
                        user.save()

                self._current_user = user
        return self._current_user