# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

from datetime import date, timedelta

from rest_framework.exceptions import ValidationError

DATE_FORMAT = '%m/%d/%Y'


def details(wsdl, site):
    if not wsdl:
        raise ValidationError({
            'error': 'Required argument: wsdl'})

    if not site:
        raise ValidationError({
            'error': 'Required argument: site'})

    if not wsdl.upper().endswith('?WSDL'):
        wsdl += '?WSDL'

    from ulmo.cuahsi import wof
    return wof.get_site_info(wsdl, site)


def values(wsdl, site, variable, from_date=None, to_date=None):
    if not wsdl:
        raise ValidationError({
            'error': 'Required argument: wsdl'})

    if not site:
        raise ValidationError({
            'error': 'Required argument: site'})

    if not variable:
        raise ValidationError({
            'error': 'Required argument: variable'})

    if not to_date:
        # Set to default value of today
        to_date = date.today().strftime(DATE_FORMAT)

    if not from_date:
        # Set to default value of one week ago
        from_date = (date.today() -
                     timedelta(days=7)).strftime(DATE_FORMAT)

    if not wsdl.upper().endswith('?WSDL'):
        wsdl += '?WSDL'

    from ulmo.cuahsi import wof
    return wof.get_values(wsdl, site, variable, from_date, to_date)

# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

from datetime import date, timedelta

from rest_framework.exceptions import ValidationError

DATE_FORMAT = '%m/%d/%Y'


def details(wsdl, site):
    if not wsdl:
        raise ValidationError({
            'error': 'Required argument: wsdl'})

    if not site:
        raise ValidationError({
            'error': 'Required argument: site'})

    if not wsdl.upper().endswith('?WSDL'):
        wsdl += '?WSDL'

    from ulmo.cuahsi import wof
    return wof.get_site_info(wsdl, site, None)


def values(wsdl, site, variable, from_date=None, to_date=None):
    if not wsdl:
        raise ValidationError({
            'error': 'Required argument: wsdl'})

    if not site:
        raise ValidationError({
            'error': 'Required argument: site'})

    if not variable:
        raise ValidationError({
            'error': 'Required argument: variable'})

    if not to_date:
        # Set to default value of today
        to_date = date.today().strftime(DATE_FORMAT)

    if not from_date:
        # Set to default value of one week ago
        from_date = (date.today() -
                     timedelta(days=7)).strftime(DATE_FORMAT)

    if not wsdl.upper().endswith('?WSDL'):
        wsdl += '?WSDL'

    from ulmo.cuahsi import wof
    return wof.get_values(wsdl, site, variable, from_date, to_date, None)

Stop ulmo caching for suds-jurko compliance
Stop ulmo caching for suds-jurko compliance

Previously we were using ulmo with suds-jurko 0.6, which is
the current latest release, but it is 4 years old. Most recent
work on suds-jurko has been done on the development branch,
including optimizations to memory use (which we need).
Unfortunately, the development branch also includes some
breaking changes, including one which "cleans up" the caching
module: https://bitbucket.org/jurko/suds/commits/6b24afe3206fc648605cc8d19f7c58c605d9df5f?at=default
This change renames .setduration() to .__set_duration(),
which is called by ulmo here: https://github.com/emiliom/ulmo/blob/90dbfe31f38a72ea4cee9a52e572cfa8f8484adc/ulmo/cuahsi/wof/core.py#L290

By explicitly setting the caching to None, we ensure that
line isn't executed and those errors don't crop up.

The performance improvements we get from using the development
branch of suds-jurko outweigh the benefits of caching for one
day, since it is unlikely we will be accessing the exact same
content repeatedly.
