Example #1
0
 def template_context(self):
     event = self.messaging_event
     date = ServerTime(event.date).user_time(self.timezone).done()
     return {
         'messaging_event_date': date.strftime(SERVER_DATETIME_FORMAT),
         'messaging_event_type': self.get_source_display(event, display_only=True),
     }
Example #2
0
 def template_context(self):
     event = self.messaging_event
     date = ServerTime(event.date).user_time(self.timezone).done()
     return {
         'messaging_event_date': date.strftime(SERVER_DATETIME_FORMAT),
         'messaging_event_type': self.get_source_display(event, display_only=True),
     }
Example #3
0
def _update_tech_issue_for_escalation(case, escalated_ticket_level):
    today = ServerTime(datetime.utcnow()).user_time(pytz.timezone('Asia/Kolkata')).done().date()

    return update_case(
        case.domain,
        case.case_id,
        case_properties={
            'ticket_level': escalated_ticket_level,
            'change_in_level': '1',
            'touch_case_date': today.strftime('%Y-%m-%d'),
        },
        close=False,
        xmlns=AUTO_UPDATE_XMLNS,
        device_id=__name__ + "._update_tech_issue_for_escalation",
    )
Example #4
0
def render_form(form, domain, options):
    """
    Uses options since Django 1.3 doesn't seem to support templatetag kwargs.
    Change to kwargs when we're on a version of Django that does.

    """
    case_id = options.get('case_id')
    side_pane = options.get('side_pane', False)
    user = options.get('user', None)
    request = options.get('request', None)
    support_enabled = toggle_enabled(request, toggles.SUPPORT)

    form_data, question_list_not_found = get_readable_data_for_submission(form)

    timezone = get_timezone_for_request()

    context = {
        "context_case_id": case_id,
        "instance": form,
        "is_archived": form.is_archived,
        "edit_info": _get_edit_info(form),
        "domain": domain,
        'question_list_not_found': question_list_not_found,
        "form_data": form_data,
        "side_pane": side_pane,
        "tz_abbrev": timezone.localize(datetime.utcnow()).tzname(),
    }

    context.update(_get_cases_changed_context(domain, form, case_id))
    context.update(_get_form_metadata_context(domain, form, timezone, support_enabled))
    context.update(_get_display_options(request, domain, user, form, support_enabled))
    context.update(_get_edit_info(form))
    instance_history = []
    if form.history:
        for operation in form.history:
            user_date = ServerTime(operation.date).user_time(timezone).done()
            instance_history.append({
                'readable_date': user_date.strftime("%Y-%m-%d %H:%M"),
                'readable_action': FORM_OPERATIONS.get(operation.operation, operation.operation),
                'user_info': get_doc_info_by_id(domain, operation.user),
            })
    context['instance_history'] = instance_history
    return render_to_string("reports/form/partials/single_form.html", context, request=request)
Example #5
0
class OutboundDailyCounter(object):
    def __init__(self, domain_object=None):
        self.domain_object = domain_object

        if domain_object:
            self.date = ServerTime(datetime.utcnow()).user_time(
                domain_object.get_default_timezone()).done().date()
        else:
            self.date = datetime.utcnow().date()

        self.key = 'outbound-daily-count-for-%s-%s' % (
            domain_object.name if domain_object else '',
            self.date.strftime('%Y-%m-%d'))

        # We need access to the raw redis client because calling incr on
        # a django_redis RedisCache object raises an error if the key
        # doesn't exist.
        self.client = get_redis_client().client.get_client()

    def increment(self):
        # If the key doesn't exist, redis will set it to 0 and then increment.
        value = self.client.incr(self.key)

        # If it's the first time we're calling incr, set the key's expiration
        if value == 1:
            self.client.expire(self.key, 24 * 60 * 60)

        return value

    def decrement(self):
        return self.client.decr(self.key)

    @property
    def current_usage(self):
        current_usage = self.client.get(self.key)
        if isinstance(current_usage, bytes):
            current_usage = int(current_usage.decode('utf-8'))
        return current_usage or 0

    @property
    def daily_limit(self):
        if self.domain_object:
            return self.domain_object.get_daily_outbound_sms_limit()
        else:
            # If the message isn't tied to a domain, still impose a limit.
            # Outbound messages not tied to a domain can happen when unregistered
            # contacts opt in or out from a gateway.
            return 10000

    def can_send_outbound_sms(self, queued_sms):
        """
        Returns False if the outbound daily limit has been exceeded.
        """
        value = self.increment()

        if value > self.daily_limit:
            # Delay processing by an hour so that in case the
            # limit gets increased within the same day, we start
            # processing the backlog right away.
            self.decrement()
            delay_processing(queued_sms, 60)

            # Log the fact that we reached this limit
            DailyOutboundSMSLimitReached.create_for_domain_and_date(
                self.domain_object.name if self.domain_object else '',
                self.date)
            return False

        return True
Example #6
0
 def _fmt_date(somedate):
     time = ServerTime(somedate).user_time(self.timezone).done()
     return time.strftime(SERVER_DATETIME_FORMAT)
Example #7
0
 def todays_date(self):
     date = ServerTime(datetime.utcnow()).user_time(
         pytz.timezone('Asia/Kolkata')).done().date()
     return date.strftime('%Y-%m-%d')
        device_id=__name__ + "._update_existing_tech_issue_delegate",
        owner_id=owner_id,
    )


def _update_tech_issue_for_escalation(case, escalated_ticket_level):
    today = ServerTime(datetime.utcnow()).user_time(
        pytz.timezone('Asia/Kolkata')).done().date()

    return update_case(
        case.domain,
        case.case_id,
        case_properties={
            'ticket_level': escalated_ticket_level,
            'change_in_level': '1',
            'touch_case_date': today.strftime('%Y-%m-%d'),
        },
        close=False,
        xmlns=AUTO_UPDATE_XMLNS,
        device_id=__name__ + "._update_tech_issue_for_escalation",
    )


def _get_escalated_tech_issue_delegate(tech_issue, location_id):
    for subcase in tech_issue.get_subcases(index_identifier='parent'):
        if (subcase.type == 'tech_issue_delegate'
                and subcase.owner_id == location_id and not subcase.closed):
            return subcase

    return None
Example #9
0
 def _fmt_date(somedate):
     time = ServerTime(somedate).user_time(self.timezone).done()
     return time.strftime(SERVER_DATETIME_FORMAT)
Example #10
0
def server_to_user_time(server_time, timezone):
    user_time = ServerTime(server_time).user_time(timezone).done()
    return user_time.strftime("%Y-%m-%d %H:%M")
Example #11
0
class OutboundDailyCounter(object):

    def __init__(self, domain_object=None):
        self.domain_object = domain_object

        if domain_object:
            self.date = ServerTime(datetime.utcnow()).user_time(domain_object.get_default_timezone()).done().date()
        else:
            self.date = datetime.utcnow().date()

        self.key = 'outbound-daily-count-for-%s-%s' % (
            domain_object.name if domain_object else '',
            self.date.strftime('%Y-%m-%d')
        )

        # We need access to the raw redis client because calling incr on
        # a django_redis RedisCache object raises an error if the key
        # doesn't exist.
        self.client = get_redis_client().client.get_client()

    def increment(self):
        # If the key doesn't exist, redis will set it to 0 and then increment.
        value = self.client.incr(self.key)

        # If it's the first time we're calling incr, set the key's expiration
        if value == 1:
            self.client.expire(self.key, 24 * 60 * 60)

        return value

    def decrement(self):
        return self.client.decr(self.key)

    @property
    def current_usage(self):
        return self.client.get(self.key) or 0

    @property
    def daily_limit(self):
        if self.domain_object:
            return self.domain_object.get_daily_outbound_sms_limit()
        else:
            # If the message isn't tied to a domain, still impose a limit.
            # Outbound messages not tied to a domain can happen when unregistered
            # contacts opt in or out from a gateway.
            return 10000

    def can_send_outbound_sms(self, queued_sms):
        """
        Returns False if the outbound daily limit has been exceeded.
        """
        value = self.increment()

        if value > self.daily_limit:
            # Delay processing by an hour so that in case the
            # limit gets increased within the same day, we start
            # processing the backlog right away.
            self.decrement()
            delay_processing(queued_sms, 60)

            # Log the fact that we reached this limit
            DailyOutboundSMSLimitReached.create_for_domain_and_date(
                self.domain_object.name if self.domain_object else '',
                self.date
            )
            return False

        return True
Example #12
0
def server_to_user_time(server_time, timezone):
    user_time = ServerTime(server_time).user_time(timezone).done()
    return user_time.strftime("%Y-%m-%d %H:%M")
 def format_timestamp(self, utc_timestamp):
     ist_timestamp = ServerTime(utc_timestamp).user_time(
         self.timezone).done()
     return ist_timestamp.strftime('%Y-%m-%d %H:%M:%S')
Example #14
0
    def _fmt_timestamp(self, timestamp):
        if not isinstance(timestamp, datetime):
            return '-'

        timestamp = ServerTime(timestamp).user_time(self.timezone).done()
        return timestamp.strftime(SERVER_DATETIME_FORMAT)
Example #15
0
    def _fmt_timestamp(self, timestamp):
        if not isinstance(timestamp, datetime):
            return '-'

        timestamp = ServerTime(timestamp).user_time(self.timezone).done()
        return timestamp.strftime(SERVER_DATETIME_FORMAT)
 def format_timestamp(self, utc_timestamp):
     ist_timestamp = ServerTime(utc_timestamp).user_time(self.timezone).done()
     return ist_timestamp.strftime('%Y-%m-%d %H:%M:%S')