Skip to content

Commit

Permalink
Merge 1fde30b into 82489fe
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurek committed Mar 31, 2016
2 parents 82489fe + 1fde30b commit c0e0ad4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/ralph/dns/dnsaas.py
Expand Up @@ -97,7 +97,7 @@ def update_dns_record(self, record):
if request.status_code != 200:
return request.json()

@cache()
@cache(skip_first=True)
def get_domain(self, domain_name):
"""
Return domain URL base on record name.
Expand Down
15 changes: 14 additions & 1 deletion src/ralph/dns/tests.py
@@ -1,10 +1,11 @@
# -*- coding: utf-8 -*-
from unittest.mock import patch

from django.test import TestCase
from django.test import override_settings, TestCase

from ralph.dns.dnsaas import DNSaaS
from ralph.dns.forms import RecordType
from ralph.dns.views import DNSaaSIntegrationNotEnabledError, DNSView


class TestGetDnsRecords(TestCase):
Expand Down Expand Up @@ -32,3 +33,15 @@ def test_return_dns_records_when_api_returns_records(self, mocked):
self.assertEqual(found_dns[0]['content'], data['content'])
self.assertEqual(found_dns[0]['name'], data['name'])
self.assertEqual(found_dns[0]['type'], RecordType.a)


class TestDNSView(TestCase):
@override_settings(ENABLE_DNSAAS_INTEGRATION=False)
def test_dnsaasintegration_disabled(self):
with self.assertRaises(DNSaaSIntegrationNotEnabledError):
DNSView()

@override_settings(ENABLE_DNSAAS_INTEGRATION=True)
def test_dnsaasintegration_enabled(self):
# should not raise exception
DNSView()
7 changes: 7 additions & 0 deletions src/ralph/dns/views.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import logging

from django.conf import settings
from django.http import HttpResponseRedirect

from ralph.admin.views.extra import RalphDetailView
Expand All @@ -10,6 +11,10 @@
logger = logging.getLogger(__name__)


class DNSaaSIntegrationNotEnabledError(Exception):
pass


class DNSView(RalphDetailView):
icon = 'chain-broken'
name = 'dns_edit'
Expand All @@ -18,6 +23,8 @@ class DNSView(RalphDetailView):
template_name = 'dns/dns_edit.html'

def __init__(self, *args, **kwargs):
if not settings.ENABLE_DNSAAS_INTEGRATION:
raise DNSaaSIntegrationNotEnabledError()
self.dnsaas = DNSaaS()
return super().__init__(*args, **kwargs)

Expand Down
11 changes: 9 additions & 2 deletions src/ralph/helpers.py
Expand Up @@ -48,16 +48,23 @@ def _cache_key_hash(func, *args, **kwargs):
return pickle.dumps((func.__module__, func.__name__, args, kwargs))


def cache(seconds=300, cache_name=DEFAULT_CACHE_ALIAS):
def cache(seconds=300, cache_name=DEFAULT_CACHE_ALIAS, skip_first=False):
"""
Cache the result of a function call with particular parameters for specified
number of seconds.
Args:
* skip_first - set to True if first argument should not be considered
when calculating hash of arguments (useful when first argument
is instance of a class (self)).
"""
def _cache(func):
@wraps(func)
def wrapper(*args, **kwargs):
cache_proxy = caches[cache_name]
key = _cache_key_hash(func, *args, **kwargs)
key = _cache_key_hash(
func, *(args[1:] if skip_first else args), **kwargs
)
result = cache_proxy.get(key, default=CACHE_DEFAULT)
if result is CACHE_DEFAULT:
logger.debug('Recalculating result of {}'.format(func.__name__))
Expand Down

0 comments on commit c0e0ad4

Please sign in to comment.