コード例 #1
0
        def wrapper(request, *args, **kwargs):
            """The inner wrapper, which wraps the view function."""
            # Certificate authentication uses anonymous pages,
            # specifically the branding index, to do authentication.
            # If that page is cached the authentication doesn't
            # happen, so we disable the cache when that feature is enabled.
            if (not request.user.is_authenticated()
                    and not settings.FEATURES['AUTH_USE_CERTIFICATES']):
                # Use the cache. The same view accessed through different domain names may
                # return different things, so include the domain name in the key.
                domain = str(request.META.get('HTTP_HOST')) + '.'
                cache_key = domain + "cache_if_anonymous." + get_language(
                ) + '.' + request.path

                # Include the values of GET parameters in the cache key.
                for get_parameter in get_parameters:
                    parameter_value = request.GET.get(get_parameter)
                    if parameter_value is not None:
                        # urlencode expects data to be of type str, and doesn't deal well with Unicode data
                        # since it doesn't provide a way to specify an encoding.
                        cache_key = cache_key + '.' + urllib.urlencode({
                            get_parameter:
                            unicode(parameter_value).encode('utf-8')
                        })

                response = cache.get(cache_key)  # pylint: disable=maybe-no-member
                if not response:
                    response = view_func(request, *args, **kwargs)
                    cache.set(cache_key, response, 60 * 3)  # pylint: disable=maybe-no-member

                return response

            else:
                # Don't use the cache.
                return view_func(request, *args, **kwargs)
コード例 #2
0
def _transform_thumbnail(value):
    m = re.match('^https?://s.xuetangx.com/files/course/image/(.*)$', value)
    if m:
        return "http://s.xuetangx.com/files/course/image/large/%s" % m.group(1)

    if value.startswith('http'):
        return value

    cache_key = "api_thumbnail_cache." + value
    url = cache.get(cache_key)
    if url:
        return url
    # log.info("Thumbnail cache not hit: %s" % value)

    try:
        url = "http://s.xuetangx.com/%s" % value.lstrip('/')
        r = requests.head(url)
        if r.status_code == status.HTTP_200_OK:
            cache.set(cache_key, url, 60 * 60 * 24)
            return url
    except:
        pass

    url = "http://www.xuetangx.com/%s" % value.lstrip("/")
    cache.set(cache_key, url, 60 * 60 * 24)
    return url
コード例 #3
0
ファイル: cache.py プロジェクト: jolyonb/edx-platform
        def wrapper(request, *args, **kwargs):
            """The inner wrapper, which wraps the view function."""
            # Certificate authentication uses anonymous pages,
            # specifically the branding index, to do authentication.
            # If that page is cached the authentication doesn't
            # happen, so we disable the cache when that feature is enabled.
            if (
                not request.user.is_authenticated
            ):
                # Use the cache. The same view accessed through different domain names may
                # return different things, so include the domain name in the key.
                domain = str(request.META.get('HTTP_HOST')) + '.'
                cache_key = domain + "cache_if_anonymous." + get_language() + '.' + request.path

                # Include the values of GET parameters in the cache key.
                for get_parameter in get_parameters:
                    parameter_value = request.GET.get(get_parameter)
                    if parameter_value is not None:
                        # urlencode expects data to be of type str, and doesn't deal well with Unicode data
                        # since it doesn't provide a way to specify an encoding.
                        cache_key = cache_key + '.' + urllib.urlencode({
                            get_parameter: unicode(parameter_value).encode('utf-8')
                        })

                response = cache.get(cache_key)  # pylint: disable=maybe-no-member
                if not response:
                    response = view_func(request, *args, **kwargs)
                    cache.set(cache_key, response, 60 * 3)  # pylint: disable=maybe-no-member

                return response

            else:
                # Don't use the cache.
                return view_func(request, *args, **kwargs)
コード例 #4
0
    def get_payment_type(self, obj):
        try:
            course_id = obj.course_id
            cache_key = "api_course_payment_cache.{}".format(course_id)
            payment_type = cache.get(cache_key, "")
            if payment_type:
                return payment_type.split("|")
            course_key = CourseKey.from_string(course_id)
            course_modes = CourseMode.modes_for_course(course_key)
            course_modes_dict = CourseMode.modes_for_course_dict(course_id, course_modes)
            has_verified_mode = CourseMode.has_verified_mode(course_modes_dict)
            SA = True
            ST = True if has_verified_mode else False
            if ST and not "honor" in course_modes_dict:
                SA = False
            if SA and ST:
                payment_type = "SA|ST"
            elif SA:
                payment_type = "SA"
            else:
                payment_type = "ST"
            cache.set(cache_key, payment_type, 60 * 60 * 6)
        except Exception as ex:
            # import traceback
            # traceback.print_exc()
            log.error(ex)
            payment_type = "SA"

        return payment_type.split("|")
コード例 #5
0
 def cached(ua):
     cachestr = ua.replace(' ', '_')
     x = cache.get("MobileUA-%s" % cachestr)
     if x is None:
         x = strategy(ua)
         cache.set("MobileUA-%s" % cachestr, x)
     return x
コード例 #6
0
 def cached(ua):
     cachestr = ua.replace(' ','_')
     x = cache.get("MobileUA-%s" % cachestr)
     if x is None:
         x = strategy(ua)
         cache.set("MobileUA-%s" % cachestr, x)
     return x
コード例 #7
0
        def wrapper(request, *args, **kwargs):
            """The inner wrapper, which wraps the view function."""
            if not request.user.is_authenticated():
                # Use the cache. The same view accessed through different domain names may
                # return different things, so include the domain name in the key.
                domain = str(request.META.get('HTTP_HOST')) + '.'
                cache_key = domain + "cache_if_anonymous." + get_language(
                ) + '.' + request.path

                # Include the values of GET parameters in the cache key.
                for get_parameter in get_parameters:
                    parameter_value = request.GET.get(get_parameter)
                    if parameter_value is not None:
                        # urlencode expects data to be of type str, and doesn't deal well with Unicode data
                        # since it doesn't provide a way to specify an encoding.
                        cache_key = cache_key + '.' + urllib.urlencode({
                            get_parameter:
                            unicode(parameter_value).encode('utf-8')
                        })

                response = cache.get(cache_key)  # pylint: disable=maybe-no-member
                if not response:
                    response = view_func(request, *args, **kwargs)
                    cache.set(cache_key, response, 60 * 3)  # pylint: disable=maybe-no-member

                return response

            else:
                # Don't use the cache.
                return view_func(request, *args, **kwargs)
コード例 #8
0
ファイル: api.py プロジェクト: zhuxiaoliang1991/swiper
def get_profile(request):
    '''获取个人资料'''
    user = request.user
    key = 'Profile-%s' % user.id
    user_profile = cache.get(key)
    if not user_profile:
        user_profile = user.profile.to_dict()
        cache.set(key, user_profile)
    return render_json(user_profile)
コード例 #9
0
ファイル: views.py プロジェクト: arturgafizov/food_box_week2
 def retrieve(self, request, *args, **kwargs):
     key = ITEM_CACHE_KEY.format(request.item.id)
     cached_response = cache.get(key)
     if cached_response:
         return Response(json.loads(cached_response), status=status.HTTP_200_OK)
     else:
         response = super().retrieve(request, *args, **kwargs)
         cache.set(key, json.dumps(response.data), ITEM_CACHE_TTL)
         return response
コード例 #10
0
def get_mtime(filename):
    if settings.MTIME_DELAY:
        key = get_mtime_cachekey(filename)
        cache = get_cache()
        mtime = cache.get(key)
        if mtime is None:
            mtime = os.path.getmtime(filename)
            cache.set(key, mtime, settings.MTIME_DELAY)
        return mtime
    return os.path.getmtime(filename)
コード例 #11
0
def get_mtime(filename):
    if settings.MTIME_DELAY:
        key = get_mtime_cachekey(filename)
        cache = get_cache()
        mtime = cache.get(key)
        if mtime is None:
            mtime = os.path.getmtime(filename)
            cache.set(key, mtime, settings.MTIME_DELAY)
        return mtime
    return os.path.getmtime(filename)
コード例 #12
0
ファイル: views.py プロジェクト: StanLivitski/Comety
    def updateSessionKey(class_, userIdString, session):
        """
        Update the session key for a specific user.
        
        Maps a user's identity to the key of a session object
        in the cache configured for this object or its class.
        A mapping will last until its session expires,
        plus `SESSION_KEY_CACHE_EXPIRATION_MARGIN`, but not shorter
        than `HEARTBEAT_TIMEOUT` ``* 10`` seconds. For sessions
        that expire on browser close, mappings will be
        cached forever.
        
        Parameters
        ----------
        userIdString : str
            Identity of the user that owns the session, converted
            to a unique string.
        session : SessionBase
            The session object to retrieve the key from.
    
        Raises
        ------
        TypeError
            If ``userIdString`` is not a string, or ``session`` is not
            a `SessionBase` object.
    
        See Also
        --------
        SESSION_KEY_CACHE_ALIAS : TODOdoc 
        SESSION_KEY_CACHE_EXPIRATION_MARGIN : TODOdoc 
        SESSION_KEY_CACHE_PREFIX : TODOdoc 
    
    [   Examples
        --------
        <In the doctest format, illustrate how to use this method.>
         ]
        """

        if not isinstance(session, SessionBase):
            raise TypeError('Object of type %s is not a session' %
                            type(session).__name__)
        key = class_.SESSION_KEY_CACHE_PREFIX + userIdString
        session_key = session.session_key
        cache = django.core.cache.caches[class_.SESSION_KEY_CACHE_ALIAS]
        try:
            expiry = None if session.get_expire_at_browser_close() \
                else session.get_expiry_age()
            if expiry is not None:
                expiry += class_.SESSION_KEY_CACHE_EXPIRATION_MARGIN(expiry)
                lowLimit = math.ceil(class_.HEARTBEAT_TIMEOUT * 10.)
            cache.set(
                key, session_key,
                expiry if expiry is None or lowLimit < expiry else lowLimit)
        finally:
            cache.close()
コード例 #13
0
def recent_posts_receiever(sender, **kwargs):
  """ simple receiver to update our cache for recent posts after any article
  has been saved.  We will update a bit too frequently, but how often
  do you write blog articles?"""
  instance = kwargs['instance']
  #NOTE: Temporary hack since render_article raises a 404 if the article isn't live
  if not instance.is_live:
    return
  key = django.core.urlresolvers.reverse(lookup_article, kwargs={'slug':instance.slug})
  fakeRequest = django.http.HttpRequest()
  articleHTML = render_article(fakeRequest, instance.slug)
  cache.set(key, articleHTML, 60*60*24*7)
コード例 #14
0
def recent_posts_receiever(sender, **kwargs):
    """ simple receiver to update our cache for recent posts after any article
  has been saved.  We will update a bit too frequently, but how often
  do you write blog articles?"""
    instance = kwargs['instance']
    #NOTE: Temporary hack since render_article raises a 404 if the article isn't live
    if not instance.is_live:
        return
    key = django.core.urlresolvers.reverse(lookup_article,
                                           kwargs={'slug': instance.slug})
    fakeRequest = django.http.HttpRequest()
    articleHTML = render_article(fakeRequest, instance.slug)
    cache.set(key, articleHTML, 60 * 60 * 24 * 7)
コード例 #15
0
    def process_request(self, request):
        if getattr(settings, 'SITE_NOTIFICATIONS_ENABLE_CACHE', SITE_NOTIFICATIONS_ENABLE_CACHE):
            if not cache.get('site-notifications-notifications'):
                notifications = Notification.objects.active_notifications()
                cache.set('site-notifications-notifications', notifications, getattr(settings, 'SITE_NOTIFICATIONS_CACHE', SITE_NOTIFICATIONS_CACHE))
            else:
                notifications = cache.get('site-notifications-notifications')
        else:
            notifications = Notification.objects.active_notifications()

        for notify in notifications:
            messages.add_message(request, notify.status, notify.message)

        return None
コード例 #16
0
ファイル: api.py プロジェクト: zhuxiaoliang1991/swiper
def modify_profile(request):
    '''修改个人资料'''
    form = ProfileForm(request.POST)
    if form.is_valid():
        user = request.user
        user.profile.__dict__.update(form.cleaned_data)
        user.profile.save()
        #修改缓存
        key = 'Profile-%s' % user.id
        cache.set(key, user.profile.to_dict())

        return render_json(None)
    else:
        return render_json(form.errors, error.PROFILE_ERROR)
コード例 #17
0
ファイル: models.py プロジェクト: UQ-UQx/xygrapher
 def getall():
     """
     Gives back all existing plotpoints, caches the plotpoints
     :return: a list of all plotpoints
     """
     response_data = []
     if cache.get('plotpoint_cache'):
         response_data = cache.get('plotpoint_cache')
     else:
         existingcoords = Plotpoint.objects()
         for existingcoord in existingcoords:
             response_data.append({"x": existingcoord.x, "y": existingcoord.y})
         #Set the cache
         cache.set('plotpoint_cache', response_data, 30)
     return response_data
コード例 #18
0
ファイル: tv.py プロジェクト: blendedlearn/blended_learning
    def get(self, request, format=None):
        cache_key = 'api.v2.wisdoms.list'

        cache_result = None
        if cache_result:
            return Response(cache_result)

        """ Get the wisdom objects. """
        wisdoms = Wisdom.objects.filter(enabled=True).order_by('-id')
        result = {
            "wisdoms": WisdomSerializer(wisdoms, many=True).data
        }

        cache.set(cache_key, result, 60 * 60)
        return Response(result)
コード例 #19
0
 def get_org_name(self, obj):
     cache_key = "api_org_name_cache.{}".format(obj.org)
     name = cache.get(cache_key)
     if name:
         return name
     if not obj.org:
         return ''
     orgs = Organization.objects.filter(org=obj.org).order_by('-id')
     if orgs.exists():
         org = orgs[0]
         name = org.name if org.name else org
         cache.set(cache_key, name, 60 * 60 * 12)
         return name
     else:
         log.error('找不到此org信息: {}'.format(obj.org))
         return ''
コード例 #20
0
 def send_email_code(self, request):
     """
     发送邮箱验证码
     """
     serializer = self.get_serializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     email = serializer.validated_data["email"]
     char_list = map(str, random_char_list(4))
     code = ''.join(char_list)
     try:
         cache.set(email, code, 3 * 60)
         subject = content = '您的验证码是: %s ,该验证码有效期3分钟, 如非本人操作请忽略此邮件!' % code
         send_html_mail(email, subject, content)
     except Exception as e:
         return Response({"detail": e[0]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
     return Response({"email": email}, status=status.HTTP_201_CREATED)
コード例 #21
0
ファイル: utils.py プロジェクト: radixpa/degidde
 def w(*args, **kwargs):
     try:
         key = namespace + _namespace_sep + key_func(*args, **kwargs)
     except TypeError:
         key = None
     if _force_set or not key:
         data = None
     else:
         data = _cache.get(key)
     if data is None:
         data = func(*args, **kwargs)
         if key:
             if data is None:
                 _cache.delete(key)
             else:
                 _cache.set(key, data, timeout)
     return data
コード例 #22
0
ファイル: cache.py プロジェクト: CDOT-EDX/edx-platform
    def _decorated(request, *args, **kwargs):
        if not request.user.is_authenticated():
            #Use the cache
            # same view accessed through different domain names may
            # return different things, so include the domain name in the key.
            domain = str(request.META.get('HTTP_HOST')) + '.'
            cache_key = domain + "cache_if_anonymous." + get_language() + '.' + request.path
            response = cache.get(cache_key)
            if not response:
                response = view_func(request, *args, **kwargs)
                cache.set(cache_key, response, 60 * 3)

            return response

        else:
            #Don't use the cache
            return view_func(request, *args, **kwargs)
コード例 #23
0
    def _decorated(request, *args, **kwargs):
        if not request.user.is_authenticated():
            #Use the cache
            # same view accessed through different domain names may
            # return different things, so include the domain name in the key.
            domain = str(request.META.get('HTTP_HOST')) + '.'
            cache_key = domain + "cache_if_anonymous." + request.path
            response = cache.get(cache_key)
            if not response:
                response = view_func(request, *args, **kwargs)
                cache.set(cache_key, response, 60 * 3)

            return response

        else:
            #Don't use the cache
            return view_func(request, *args, **kwargs)
コード例 #24
0
ファイル: views.py プロジェクト: Gaspiller/kindergarten
def sms_view(request):
    if request.method != 'POST':
        result = {'code': 10105, 'error': 'please use POST'}
        return JsonResponse(result)

    json_str = request.body
    json_obj = json.loads(json_str)
    phone = json_obj['phone']
    code = random.randint(1000, 9999)
    cache_key = 'sms_%s' % (phone)
    old_code = cache.get(cache_key)
    if old_code:
        result = {'code': 10107, 'error': 'The code is already'}
        return JsonResponse(result)
    cache.set(cache_key, code, 180)
    send_sms.delay(phone, code)
    return JsonResponse({'code': 200})
コード例 #25
0
def get_jsapi_ticket():
    cache_key = 'weixinapp_jsapi_ticket'
    access_token = get_weixin_accesstoken()
    url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={}&type=jsapi'.format(
            access_token
    )
    try:
        response = requests.get(url).json()
    except Exception as ex:
        log.info(ex)
        return ''
    ticket = response.get('ticket')
    expires_in = response.get('expires_in')
    if ticket:
        expires_in = int(expires_in)
        cache.set(cache_key, ticket, int(expires_in/4.0*3))
        return ticket
    return ''
コード例 #26
0
ファイル: models.py プロジェクト: simultech/uqx-xygrapher
 def getall():
     """
     Gives back all existing plotpoints, caches the plotpoints
     :return: a list of all plotpoints
     """
     response_data = []
     if cache.get('plotpoint_cache'):
         response_data = cache.get('plotpoint_cache')
     else:
         existingcoords = Plotpoint.objects()
         for existingcoord in existingcoords:
             response_data.append({
                 "x": existingcoord.x,
                 "y": existingcoord.y
             })
         #Set the cache
         cache.set('plotpoint_cache', response_data, 30)
     return response_data
コード例 #27
0
    def get_intro_video_caption(self, course):
        caption = "http://s.xuetangx.com/files/course/caption/%s.srt" % course.intro_video if course.intro_video else ''
        if not caption:
            return caption
        cache_key = "api_intro_video_caption_cache.{}".format(course.id)
        has_caption = cache.get(cache_key)
        if has_caption:
            return caption
        log.info("caption cache not hit: {}".format(caption))

        try:
            r = requests.head(caption)
            if r.status_code == status.HTTP_200_OK:
                cache.set(cache_key, caption, 60 * 60 * 24)
                return caption
        except:
            pass
        return ''
コード例 #28
0
def lookup_article(request, slug):
  """ returns a blog article
  NOTE: we accept sprinkling cache stuff in this view and this view only because
  it is so easy to do in this case because there is no need to worry about
  everything it touches.  This is probably a premature optimization and caching this page could
  be handled using the timeout method employed elsewhere."""

  #try cache first if anon
  useCache = request.user.is_anonymous()
  if useCache:
    key = request.path
    response = cache.get(key, None)
    if response is not None:
      return django.http.HttpResponse(response)

  response = render_article(request, slug)
  if useCache:
    cache.set(key, response, 60*60*24*7)
  return django.http.HttpResponse(response)
コード例 #29
0
ファイル: cache.py プロジェクト: fccn/edx-platform
        def wrapper(request, *args, **kwargs):
            """The inner wrapper, which wraps the view function."""
            # Certificate authentication uses anonymous pages,
            # specifically the branding index, to do authentication.
            # If that page is cached the authentication doesn't
            # happen, so we disable the cache when that feature is enabled.
            if (not request.user.is_authenticated
                    and settings.FEATURES['ENABLE_CACHE_IF_ANONYMOUS']):
                # Use the cache. The same view accessed through different domain names may
                # return different things, so include the domain name in the key.
                domain = str(request.META.get('HTTP_HOST')) + '.'
                cache_key = domain + "cache_if_anonymous." + get_language(
                ) + '.' + request.path

                # Include the values of GET parameters in the cache key.
                for get_parameter in get_parameters:
                    parameter_value = request.GET.get(get_parameter)
                    if parameter_value is not None:
                        # urlencode expects data to be of type str, and doesn't deal well with Unicode data
                        # since it doesn't provide a way to specify an encoding.
                        cache_key = cache_key + '.' + six.moves.urllib.parse.urlencode(
                            {
                                get_parameter:
                                six.text_type(parameter_value).encode('utf-8')
                            })

                response = cache.get(cache_key)

                if response:
                    # A hack to ensure that the response data is a valid text type for both Python 2 and 3.
                    response_content = list(response._container)  # pylint: disable=protected-member
                    response.content = b''
                    for item in response_content:
                        response.write(item)
                else:
                    response = view_func(request, *args, **kwargs)
                    cache.set(cache_key, response, 60 * 3)

                return response

            else:
                # Don't use the cache.
                return view_func(request, *args, **kwargs)
コード例 #30
0
def lookup_article(request, slug):
    """ returns a blog article
  NOTE: we accept sprinkling cache stuff in this view and this view only because
  it is so easy to do in this case because there is no need to worry about
  everything it touches.  This is probably a premature optimization and caching this page could
  be handled using the timeout method employed elsewhere."""

    #try cache first if anon
    useCache = request.user.is_anonymous()
    if useCache:
        key = request.path
        response = cache.get(key, None)
        if response is not None:
            return django.http.HttpResponse(response)

    response = render_article(request, slug)
    if useCache:
        cache.set(key, response, 60 * 60 * 24 * 7)
    return django.http.HttpResponse(response)
コード例 #31
0
    def get(self, request, format=None):
        belong_str = request.GET.get('belong', 'mobile')
        channel_str = request.GET.get('channel', 'all')
        if 'xiaomi' in channel_str.lower():
            channel_str = 'xiaomi'
        cache_key = 'api.v2.banners.list.{}.{}'.format(belong_str, channel_str)

        cache_result = cache.get(cache_key)
        if cache_result:
            return Response(cache_result)

        """ Get the banner objects. """
        banners = Banner.objects.filter(is_active=True, belong=belong_str,
                                        channel__in=['all', channel_str]).order_by('order')
        result = {
            "banners": BannerSerializer(banners, many=True).data
        }

        cache.set(cache_key, result, 60 * 60)
        return Response(result)
コード例 #32
0
ファイル: views.py プロジェクト: zwcdp/original
    def post(self, request, phone_number, verify):
        captcha = verify
        verify = request.session.get(constants.VALIDATION_SESSION_KEY, '')
        request.session[constants.VALIDATION_SESSION_KEY] = '!'
        if not captcha or verify.lower() != captcha.lower():
            raise exceptions.APIValidationCodeException(message=u'验证码错误')
        cache_key = 'sms_{}'.format(phone_number)
        phone_number_cached = cache.get(cache_key, None)
        if phone_number_cached:
            raise exceptions.APISMSException(
                code=exceptions.ErrorCode.sms_too_frequently,
                message=u'验证码请求过于频繁')
        else:
            cache.set(cache_key, 1, SMSValidate.FREQUENTLY_TIME - 1)

        if SMSValidate.is_out_of_limit(phone_number):
            raise exceptions.APISMSException(
                code=exceptions.ErrorCode.sms_out_daily_number_limit,
                message=u'手机号验证码达到当日上限')

        sms_list = SMSValidate.objects.filter(
            status=SMS_WAIT_TO_CHECK,
            phone_number=phone_number).order_by('-created_at')
        # 防止用户恶意注册
        if sms_list.exists():
            sms_obj = sms_list[0]
            if sms_obj.is_too_frequently():
                raise exceptions.APISMSException(
                    code=exceptions.ErrorCode.sms_too_frequently,
                    message=u'验证码请求过于频繁')
        try:
            obj = SMSValidate.new(phone_number)
        except Exception as ex:
            log.warning(ex, exc_info=1)
            raise exceptions.APISMSException()
        sms_handler.send_with_template(
            phone_number,
            settings.SMS_QCLOUD_DEFAULT_TEMPLATE_ID,
            template_params=[obj.validate,
                             str(SMSValidate.EXPIRE_TIME / 60)])
        return Response()
コード例 #33
0
def get_weixin_accesstoken():
    cache_key = 'weixinapp_access_token'
    access_token = cache.get(cache_key)
    if access_token:
        return access_token
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}'.format(
        settings.SOCIAL_AUTH_WEIXINAPP_KEY,
        settings.SOCIAL_AUTH_WEIXINAPP_SECRET
    )
    try:
        response = requests.get(url).json()
    except Exception as ex:
        log.info(ex)
        return ''
    access_token = response.get('access_token')
    expires_in = response.get('expires_in')
    if access_token and expires_in:
        expires_in = int(expires_in)
        cache.set(cache_key, access_token, int(expires_in/4.0*3))
        return access_token
    return ''
コード例 #34
0
def get_weixin_accesstoken(key, secret):
    cache_key = 'weixinapp_access_token:{}'.format(key)
    access_token = cache.get(cache_key)
    if access_token:
        return access_token
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}'.format(key, secret)
    try:
        log.info('try get weixin access_token from wechat server')
        response = requests.get(url).json()
    except Exception as ex:
        log.info(ex)
        return ''
    access_token = response.get('access_token')
    expires_in = response.get('expires_in')
    if access_token and expires_in:
        expires_in = int(expires_in)
        cache.set(cache_key, access_token, int(expires_in/4.0*3))
        log.info('cache weixin access_token, key: %s, ticket: %s', cache_key, access_token)
        return access_token
    log.warning(response)
    return ''
コード例 #35
0
ファイル: exporter.py プロジェクト: ii0/notex-v1.0
def compress (request, id, translate, ext = 'zip', hook = None):

    (type, ids) = json.loads (base64.b32decode (id))
    if type == 'leaf':
        leaf = LEAF.objects.get (id = ids[1])
        node = leaf.node
    else:
        node = NODE.objects.get (id = ids[0])

    while node.node: node = node.node
    object_key = hex (hash ((request.session.session_key, translate, node.id)))
    object_val = cache.get (object_key)

    if object_val:
        if 'fetch' in request.GET:
            if hook: object_val = hook (object_val)

            size = len (object_val)
            temp = tempfile.SpooledTemporaryFile (max_size = size)
            temp.write (object_val)

            http_response = HttpResponse (
                FileWrapper (temp), content_type = 'application/%s' % ext)
            http_response['Content-Disposition'] = \
                'attachment;filename="%s.%s"' % (node.name.encode ("utf-8"), ext)
            http_response['Content-Length'] = size

            temp.seek (0);

        else:
            js_string = json.dumps ([{'id' : node.id, 'name' : node.name, 'success' : True}])
            http_response = HttpResponse (js_string, mimetype='application/json')
            cache.set (object_key, object_val, timeout=15*60) ## refresh

    else:
        http_response, object_val, success = to_zip (request, translate, node)
        if success: cache.set (object_key, object_val, timeout=15*60) ## 15 mins

    return http_response
コード例 #36
0
ファイル: cache.py プロジェクト: IET-OU/edx-platform
        def wrapper(request, *args, **kwargs):
            """The inner wrapper, which wraps the view function."""
            if not request.user.is_authenticated():
                # Use the cache. The same view accessed through different domain names may
                # return different things, so include the domain name in the key.
                domain = str(request.META.get('HTTP_HOST')) + '.'
                cache_key = domain + "cache_if_anonymous." + get_language() + '.' + request.path

                # Include the values of GET parameters in the cache key.
                for get_parameter in get_parameters:
                    cache_key = cache_key + '.' + unicode(request.GET.get(get_parameter))

                response = cache.get(cache_key)  # pylint: disable=maybe-no-member
                if not response:
                    response = view_func(request, *args, **kwargs)
                    cache.set(cache_key, response, 60 * 3)  # pylint: disable=maybe-no-member

                return response

            else:
                # Don't use the cache.
                return view_func(request, *args, **kwargs)
コード例 #37
0
def get_jsapi_ticket(key, secret):
    cache_key = 'weixinapp_jsapi_ticket:{}'.format(key)
    ticket = cache.get(cache_key)
    if ticket:
        return ticket
    access_token = get_weixin_accesstoken(key, secret)
    url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={}&type=jsapi'.format(
            access_token
    )
    try:
        log.info('try get weixin ticket from wechat server')
        response = requests.get(url).json()
    except Exception as ex:
        log.info(ex)
        return ''
    ticket = response.get('ticket')
    expires_in = response.get('expires_in')
    if ticket:
        expires_in = int(expires_in)
        cache.set(cache_key, ticket, int(expires_in/4.0*3))
        log.info('cache weixin ticket, key: %s, ticket: %s', cache_key, ticket)
        return ticket
    log.warning(response)
    return ''
コード例 #38
0
    def transform_thumbnail(self, obj, value):
        # new storage
        if value.startswith(settings.COURSE_COMPRESSED_COVER_PATH):
            return value

        # Return directly if hit in cache
        cache_key = "api_thumbnail_cache." + value
        url = cache.get(cache_key)
        if url:
            return url
        log.info("Thumbnail cache not hit: %s" % value)

        m = re.match(
            '^https?://s.xuetangx.com/files/course/image/(.*)$', value)
        if m:
            url = "http://s.xuetangx.com/files/course/image/large/%s" % m.group(1)
            if _url_exists(url):
                cache.set(cache_key, url, 60 * 60 * 24)
                return url

        if value.startswith('http'):
            url = value
            if _url_exists(url):
                cache.set(cache_key, url, 60 * 60 * 24)
                return url

        try:
            url = "http://s.xuetangx.com/%s" % value.lstrip('/')
            if _url_exists(url):
                cache.set(cache_key, url, 60 * 60 * 24)
                return url
        except:
            pass

        DOMAIN = 'http://{}'.format(settings.LMS_BASE)
        url = "{}/{}".format(DOMAIN,  value.lstrip("/"))
        cache.set(cache_key, url, 60 * 60 * 24)
        return url
コード例 #39
0
    def test_max_connections(self):
        pool._connection_pools = {}
        cache = get_cache('default')

        def noop(*args, **kwargs):
            pass

        release = cache._client.connection_pool.release
        cache._client.connection_pool.release = noop
        cache.set('a', 'a')
        cache.set('a', 'a')

        with self.assertRaises(redis.ConnectionError):
            cache.set('a', 'a')

        cache._client.connection_pool.release = release
        cache._client.connection_pool.max_connections = 2**31
コード例 #40
0
ファイル: allPythonContent.py プロジェクト: Mondego/pyreco
    def test_max_connections(self):
        pool._connection_pools = {}
        cache = get_cache('default')

        def noop(*args, **kwargs):
            pass

        release = cache._client.connection_pool.release
        cache._client.connection_pool.release = noop
        cache.set('a', 'a')
        cache.set('a', 'a')

        with self.assertRaises(redis.ConnectionError):
            cache.set('a', 'a')

        cache._client.connection_pool.release = release
        cache._client.connection_pool.max_connections = 2**31
コード例 #41
0
ファイル: allPythonContent.py プロジェクト: Mondego/pyreco
def someview(request):
    cache = get_cache('redis_cache.cache://127.0.0.1')
    cache.set("foo", "bar")
    return HttpResponse("Pants")
コード例 #42
0
def get_active_clients(request):
    start_time = int(ClientConnection.objects.order_by("time").first().time)-1
    # TODO: figure out if rumman is sending current UTC epoch or not
    end_time = int(time.time())

    if request.GET.get('start_time') and int(request.GET.get('start_time')) > start_time:
        start_time = int(request.GET.get('start_time'))

    if request.GET.get('end_time') and int(request.GET.get('end_time')) <= time.time():
        end_time = int(request.GET.get('end_time'))

    valid_clients = WirelessClient.objects.all()\
                                  .annotate(first_connection=Min("connections__time"),
                                            last_connection=Max("connections__time"))\
                                  .annotate(duration=F("last_connection") - F("first_connection"))\
                                  .order_by('-duration')\
                                  .filter(duration__gt=0)

    COUNT_INTERVAL = 60

    count_over_time = list()
    current_processing_time = start_time

    # Get latest cache:
    cache = caches['default']
    client_history = cache.get('client_history')

    if not client_history:
        cache.set('client_history', dict(), timeout=0)
        client_history = dict()

    while current_processing_time < end_time-COUNT_INTERVAL+1:
        # Check cache:
        if current_processing_time in client_history:
            history_point = client_history[current_processing_time]
            # Check zero:
            zeroflag = True
            for station in history_point['count']:
                if history_point['count'][station] > 0:
                    zeroflag = False

#            if not zeroflag:
            count_over_time.append(history_point)

        else:
            current_time_dict = dict()
            current_time_dict['start_time'] = current_processing_time

            connections_in_interval = ClientConnection.objects.filter(time__gte=current_processing_time,
                                                                      time__lte=current_processing_time+COUNT_INTERVAL)
            # optional gain filter:
            if request.GET.get('gain'):
                connections_in_interval = connections_in_interval.filter(gain__gte=request.GET.get('gain'))

            current_time_dict['count'] = dict()

            all_zero_flag = True

            for station in BaseStation.objects.all():
                connections_in_interval_station = connections_in_interval.filter(station=station)
                clients_in_interval = valid_clients.filter(connections__in=connections_in_interval_station)
                current_time_dict['count'][station.token] = int(clients_in_interval.count())

                if clients_in_interval.count() > 0:
                    all_zero_flag = False
            # current_time_dict['clients'] = list(clients_in_interval.values_list('id', flat=True))

            if not all_zero_flag:
                count_over_time.append(current_time_dict)

            # TODO Get new clients since last interval & clients that disappeared since last interval

            client_history[current_processing_time] = current_time_dict

        current_processing_time = current_processing_time + COUNT_INTERVAL

    cache.set('client_history', client_history)

    return JsonResponse(count_over_time, safe=False, json_dumps_params={'indent': 2})
コード例 #43
0
    def get(self, request, course_id, format=None):
        """ Get course chapter list.
        注意:如果移动端需要的vertical_types需要video之外的东西,整个方法需要重构
        """
        if not Course.objects.filter(course_id=course_id).exists():
            return Response(status=status.HTTP_404_NOT_FOUND)

        if not CourseEnrollment.is_enrolled(request.user, course_id):
            return Response(status=status.HTTP_403_FORBIDDEN)
        user_agent = request.META.get('HTTP_USER_AGENT', '').lower()
        if 'androidtv' in user_agent:
            is_tv = True
        else:
            is_tv = False

        show_sequentials = request.GET.get('show_sequentials')
        if show_sequentials:
            if show_sequentials == '1' or show_sequentials.lower() == 'true':
                show_sequentials = True
            else:
                show_sequentials = False
        # 首先取一下缓存
        if show_sequentials:
            # 手动清除缓存的后面
            invalite_cache = request.GET.get('cache', None)
            if invalite_cache:
                cache_key_tv = 'api.course.{}.chapters_with_seq.{}'.format(course_id, True)
                cache_key = 'api.course.{}.chapters_with_seq.{}'.format(course_id, False)
                cache.delete(cache_key_tv)
                cache.delete(cache_key)

            if settings.DEBUG:
                cache_key = 'api.course.{}.chapters_with_seq.{}'.format(course_id, is_tv) + str(time.time())
            else:
                cache_key = 'api.course.{}.chapters_with_seq.{}'.format(course_id, is_tv)

            cache_result = cache.get(cache_key)
            if cache_result:
                return Response(cache_result)

        course = get_course(course_id)
        chapters = [get_item(course_id, "chapter", chapter_id)
                    for chapter_id in _get_item_id_list(course.children, "chapter")]
        now = datetime.now(UTC)
        chapters = filter(lambda x: x and x.start < now, chapters)

        if show_sequentials:
            # key sequential, value {video:0}
            seq_type_dict = defaultdict(lambda : defaultdict(int))

            # 首先查出vertical需要的block列表
            vertical_types = ['video']
            vertical_dict = {vt: set() for vt in vertical_types}
            for vtype in vertical_types:
                blocks = get_items(course_id, vtype)
                blocks = filter(lambda x: x and x.start < now, blocks)
                vtype_set = _get_vertical_set(blocks)
                vertical_dict[vtype] = vtype_set

            for chapter in chapters:
                sequentials = [get_item(course_id, "sequential", sequential_id)
                               for sequential_id in _get_item_id_list(chapter.children, "sequential")]
                sequentials = filter(lambda x: x and x.start < now, sequentials)
                chapter.sequentials = sequentials

                for sequential in sequentials:
                    verticals = [get_item(course_id, "vertical", vertical_id)
                                 for vertical_id in _get_item_id_list(sequential.children, "vertical")]
                    verticals = filter(lambda x: x and x.start < now, verticals)
                    sequential.verticals = verticals

                    # 通过之前查出的block集合
                    for vertical in verticals:
                        blocks = vertical.children
                        for block in blocks:
                            category = _get_location_category(block)
                            block_location_id = _get_location_id(block)
                            if category in vertical_dict and block_location_id in vertical_dict[category]:
                                seq_type_dict[sequential][category] += 1

            for sequential, types in seq_type_dict.iteritems():
                sequential.type = dict(types)

            chapters_array = ChapterWithSequentialSerializer(chapters, many=True).data

            if is_tv:
                cp_array = []
                for chapters_temp in chapters_array:
                    sq_array = []
                    for sq_temp in chapters_temp['sequentials']:
                        if sq_temp['type'].get('video', None):  # tv端过滤非video
                            sq_array.append(sq_temp)
                    chapters_temp['sequentials'] = sq_array
                    if chapters_temp['sequentials'] !=[]:
                        cp_array.append(chapters_temp)
                chapters_array = cp_array

            result = {
                "chapters": chapters_array,
            }
            if is_tv:
                cache.set(cache_key, result, 60 * 60 * 24 * 7)
            else:
                cache.set(cache_key, result, 60 * 60)
        else:
            result = {
                "chapters": ChapterSerializer(chapters, many=True).data
            }
        return Response(result)
コード例 #44
0
    def _get_course_info(self, request, course, chapter=None, sequential=None):
        """
        Returns the course status

        edx:

        path_ids = [unicode(module.location) for module in path]
        return Response({
           "last_visited_module_id": path_ids[0],
           "last_visited_module_path": path_ids,
        })

        {
            "last_visited_module_id": "i4x://apitestorg/apitestcourse/sequential/624e6b343d5e4b319a6a8b7fe63c9262",
            "last_visited_module_path": [
                "i4x://apitestorg/apitestcourse/sequential/624e6b343d5e4b319a6a8b7fe63c9262",
                "i4x://apitestorg/apitestcourse/chapter/a9ae78343c0f47ad91159d3b9035ea9c",
                "i4x://apitestorg/apitestcourse/course/2015_3"
            ]
        }
        """
        path = self._last_visited_module_path(request, course)
        path_ids = [(module.location) for module in path]
        course_id = course.id
        chapter_id = ''
        sequential_id = ''
        chapter_location = ''
        sequential_location = ''
        result = {'course_id': _get_course_id(course_id)}
        for path_id in path_ids:
            if u'chapter' == path_id.block_type:
                chapter_id = path_id.block_id
                chapter_location = _get_location_id(path_id)
                continue
            if u'sequential' == path_id.block_type:
                sequential_id = path_id.block_id
                sequential_location =_get_location_id(path_id)
                continue
        # chapter
        if not chapter_id:
            return result

        cache_course_children_key = 'api_course_sync.course.{}.chapter'.format(course.id)
        # 如果缓存有course_children,则说明下面的mongo查chapter查到了
        course_children = cache.get(cache_course_children_key, [])
        if not course_children:
            try:
                chapter = get_item(course.id, 'chapter', chapter_id) if not chapter else chapter
                if not chapter:
                    return result
            except ItemNotFoundError:
                return result
            course_children = get_obj_children_ids(course)
            cache.set(cache_course_children_key, course_children, 60 * 60)
        chapter_position = 0 if chapter_location not in course_children else course_children.index(chapter_location)
        result['chapter_id'] = chapter_id
        result['chapter_position'] = chapter_position
        # sequential
        if sequential_id:
            try:
                cache_chapter_children_key = 'api_course_sync.course.{}.chapter.{}.sequential'.format(course.id,
                                                                                                      chapter_id)
                visit_chapter_sequentials = cache.get(cache_chapter_children_key, [])
                if not visit_chapter_sequentials:
                    chapter = get_item(course.id, 'chapter', chapter_id) if not chapter else chapter
                    sequential = get_item(course.id, 'sequential', sequential_id) if not sequential else sequential
                    if sequential:
                        visit_chapter_sequentials = get_obj_children_ids(chapter)
                        cache.set(cache_chapter_children_key, visit_chapter_sequentials, 60 * 60)

                sequential_position = 0 if sequential_location not in visit_chapter_sequentials \
                    else visit_chapter_sequentials.index(sequential_location)
                result['sequential_id'] = sequential_id
                result['sequential_position'] = sequential_position
            except ItemNotFoundError:
                raise error.Error(status=status.HTTP_404_NOT_FOUND)
        return result
コード例 #45
0
def someview(request):
    cache = get_cache('redis_cache.cache://127.0.0.1')
    cache.set("foo", "bar")
    return HttpResponse("Pants")
コード例 #46
0
ファイル: mykwips.py プロジェクト: kwippy-com/kwippycore
def details_for_kwips_page(request,user_login):
  cursor = connection.cursor()
  user = get_object_or_404(User, username=user_login)
  if request.user.is_authenticated():
    logged_in_user_profile = User_Profile.objects.get(user=request.user.id)
  else:
    logged_in_user_profile = False
  if request.user.is_authenticated():
    is_following = isfollowing(request.user,user)
    are_friends = arefriends(request.user,user)
    if is_following:
      is_following_on_im = get_object_or_404(Follower, follower=request.user,followee=user).im_notification
    else:
      is_following_on_im = False
    cache_key = '%s_follow%dto%d' % (settings.CACHE_MIDDLEWARE_KEY_PREFIX,user.id,request.user.id,)
    is_receiver_following = cache.get(cache_key)
    if is_receiver_following is None:
        is_receiver_following = isfollowing(user,request.user)
        cache.set(cache_key,is_receiver_following,CACHE_EXPIRES)
  else:
    is_following_on_im = False 
    is_following = False
    is_receiver_following = False
    are_friends = False  
  
  cache_key = '%s_profilequery%d' % (settings.CACHE_MIDDLEWARE_KEY_PREFIX,user.id,)
  user_profile = cache.get(cache_key)
  if user_profile is None:
    user_profile = get_object_or_404(User_Profile, user=user)
    #user_profile = User_Profile.objects.filter(user=user.id)
    cache.set(cache_key, user_profile, CACHE_EXPIRES)
  profile_for_display = {'gender':user_profile.get_gender_display(), 'relationship': user_profile.get_relationship_status_display(), 'birth_month':user_profile.get_birth_month_display()}
  
  cache_key = '%s_userfollowerquery%d' % (settings.CACHE_MIDDLEWARE_KEY_PREFIX,user.id,)
  users_followers = cache.get(cache_key)
  if users_followers is None:
    cursor.execute("select auth_user.id from auth_user,kwippy_follower where kwippy_follower.follower_id=auth_user.id and kwippy_follower.followee_id=%s \
    and auth_user.id not in (select receiver_id from kwippy_friend where sender_id=%s and status=1 union \
    select sender_id from kwippy_friend where receiver_id=%s and status=1 )order by auth_user.last_login desc limit 24",(user.id,user.id,user.id,))
    followers_ids = [item[0] for item in cursor.fetchall()]
    users_followers = User.objects.filter(id__in=followers_ids).order_by('-last_login')
    cache.set(cache_key,users_followers,CACHE_EXPIRES)
  
  cache_key = '%s_followercount%d' % (settings.CACHE_MIDDLEWARE_KEY_PREFIX,user.id,)
  followers_count = cache.get(cache_key)
  if followers_count is None:
    cursor.execute('select count(*) from kwippy_follower where followee_id=%s and follower_id not in (select receiver_id from kwippy_friend where sender_id=%s and status=1 union \
    select sender_id from kwippy_friend where receiver_id=%s and status=1)',(user.id,user.id,user.id,))
    (followers_count,)=cursor.fetchone()
    cache.set(cache_key,followers_count,CACHE_EXPIRES)

  #cursor.execute("select id from kwippy_friend where status=1 and (sender_id=%s or receiver_id=%s )",(user.id,user.id,))
  #friend_ids = [item[0] for item in cursor.fetchall()]
  user_frenz = Friend.objects.filter(sender=user, status=1) | Friend.objects.filter(receiver=user, status=1)
  friend_ids = []
  for item in user_frenz:
    if item.sender == user:
      friend_ids.append(int(item.receiver.id))
    else:
      friend_ids.append(int(item.sender.id))
  friends_count = User.objects.filter(id__in=friend_ids).count()
  users_friends = User.objects.filter(id__in=friend_ids).order_by('-last_login')[:9]    

  cache_key = '%s_userfolloweequery%d' % (settings.CACHE_MIDDLEWARE_KEY_PREFIX,user.id,)
  users_followees = cache.get(cache_key)
  if users_followees is None:
    cursor.execute("select auth_user.id from auth_user,kwippy_follower where kwippy_follower.followee_id=auth_user.id and kwippy_follower.follower_id=%s \
    and auth_user.id not in (select receiver_id from kwippy_friend where sender_id=%s and status=1 union \
    select sender_id from kwippy_friend where receiver_id=%s and status=1 )order by auth_user.last_login desc limit 9",(user.id,user.id,user.id,))
    followees_ids = [item[0] for item in cursor.fetchall()]
    users_followees = User.objects.filter(id__in=followees_ids).order_by('-last_login')
    cache.set(cache_key,users_followees,CACHE_EXPIRES)
  
  cache_key = '%s_followeecount%d' % (settings.CACHE_MIDDLEWARE_KEY_PREFIX,user.id,)
  followees_count = cache.get(cache_key)
  if followees_count is None:
    cursor.execute('select count(*) from kwippy_follower where follower_id=%s and followee_id not in (select receiver_id from kwippy_friend where sender_id=%s and status=1 union \
    select sender_id from kwippy_friend where receiver_id=%s and status=1 )',(user.id,user.id,user.id,))
    (followees_count,)=cursor.fetchone()
    cache.set(cache_key,followees_count,CACHE_EXPIRES)
  
  cache_key = '%s_favcount%d' %(settings.CACHE_MIDDLEWARE_KEY_PREFIX,user.id,)
  favs_count = cache.get(cache_key)
  if favs_count is None:
    favs_count = Favourite.objects.filter(user=user).count()
    cache.set(cache_key,favs_count,CACHE_EXPIRES)
  
  user_age = user_profile.get_age()

  location = user_profile.location_city 
  if request.user.is_authenticated():
      hasfollowers = has_followers(request.user)
      fe = Fireeagle.objects.filter(user=user,integrated=1)
      if fe:
          location = fe[0].location
  else:
      hasfollowers = None
  #connection.close()
  dict = {'user': user,'user_profile': user_profile,'displayname': get_display_name(user), 'location':location,
          'users_followers': users_followers,'users_followees': users_followees,'followees_count':followees_count, 'followers_count':followers_count,'is_receiver_following':is_receiver_following,
           'is_following':is_following,'is_following_on_im':is_following_on_im,'are_friends':are_friends,'users_friends':users_friends,'user_age':user_age,'has_followers':hasfollowers,
          'logged_in_user_profile':logged_in_user_profile,'profile_for_display':profile_for_display,'favs_count':favs_count,'friends_count':friends_count,} 
  return dict