Example #1
0
def previous_report_date(start_date, end_date, period=None):
    """
    >>> previous_report_date('2013-01-01', '2013-01-30')
    1
    """
    start = datetime.datetime.strptime(start_date, '%Y-%m-%d')
    end = datetime.datetime.strptime(end_date, '%Y-%m-%d')

    diff = abs((end - start).days)

    if period:
        if period == 'monthly':
            new_start = monthdelta(start, -1)
        elif period == 'weekly':
            new_start = start - datetime.timedelta(weeks=1)
        elif period == 'daily':
            new_start = start - datetime.timedelta(days=1)
        else:
            raise RipeApiException(
                *RipeApiException.INCORRECT_PERIOD_PREVIOUS_REPORT,
                debug_message=u'Incorrect Period set for previous report date')
    else:
        new_start = start - datetime.timedelta(days=diff)

    new_start = new_start.strftime("%Y-%m-%d")
    new_end = start.strftime("%Y-%m-%d")

    return new_start, new_end
Example #2
0
def get_date_range(start, end, cube=None, interval=None):
    # trace.info('{} {} inter: {}'.format(start, end, cube, interval))
    if not cube or not isinstance(cube, ModelBase):
        message = u"Incorrect cube type: '{}'.".format(type(cube))
        raise RipeApiException(*RipeApiException.BAD_CUBE,
                               debug_message=message)

    if start == end == 'all':
        start = cube.objects.aggregate(Min('date'))['date__min']
        #todo could change to current date later
        end = cube.objects.aggregate(Max('date'))['date__max']
    elif start == 'all' and end != 'all':
        start = cube.objects.aggregate(Min('date'))['date__min']
    elif end == 'all' and start != 'all':
        #todo could change to current date later
        end = cube.objects.aggregate(Max('date'))['date__max']

    #cal the date range based on interval
    dt_range = []
    if interval == 'daily':
        try:
            dt_range = [(start + datetime.timedelta(days=i),
                         start + datetime.timedelta(days=i))
                        for i in range((end - start).days + 1)]
        except Exception:
            trace.exception('\n\n')
    elif interval == 'total':
        dt_range = []
    elif interval == 'weekly':
        if (end - start).days <= 6:
            dt_range.append((start, end))
        else:
            seven = datetime.timedelta(days=7)
            wk_start = start
            wk_end = start + datetime.timedelta(days=6)
            dt_range.append((wk_start, wk_end))
            while wk_end < end:
                wk_start = wk_start + seven
                wk_end = wk_end + seven
                if wk_start <= end < wk_end:
                    wk_end = end
                dt_range.append((wk_start, wk_end))
    elif interval == 'monthly':
        mth_end = month_end(start)
        dt_range.append((start, mth_end))
        while mth_end < end:
            mth_start = mth_end + datetime.timedelta(days=1)
            mth_end = month_end(mth_start)
            dt_range.append((mth_start, mth_end))
    # trace.info('s: {} e: {} range: {}'.format(start, end, dt_range))
    return start, end, dt_range

    ALPHANUMERICS = character_checker(digits, ascii_letters)
    ALPHANUMERICS_PLUS = character_checker(digits, ascii_letters, "-_")
    DATE_TIME = character_checker(digits, "-")
Example #3
0
    def filter_by_dates(start, end, obj):
        try:
            if start != 'all':
                start = datetime.datetime.strptime(start, '%Y-%m-%d').date()
                if end is None or end == 'all':
                    end = obj.aggregate(Max('date'))['date__max']
                else:
                    end = datetime.datetime.strptime(end, '%Y-%m-%d').date()
                obj = obj.filter(date__gte=start).filter(date__lte=end)

        except ValueError:
            raise RipeApiException(*RipeApiException.MODEL_BAD_DATE_FORMAT,
                                   debug_message=u'Invalid date format')
        return obj
Example #4
0
    def filter_cube(self, filters):
        sub = self.model.objects.all()

        sub.exclude(subscription_state__in=self.states)
        if isinstance(filters, dict):
            if len(sub) and len(filters):

                print '\n FILTERING START subs len: {}   filters len: {}, filters: {}'.format(
                    len(sub), len(filters), filters)

                keys = filters.keys()
                if 'user_state' in keys:
                    if 'package' in keys:

                        if filters['package'] == 'all':
                            package = [1, 0]
                        elif filters['package'] == 'ongoing':
                            package = [1]
                        elif filters['package'] == 'fixed':
                            package = [0]
                        else:
                            raise RipeApiException(
                                *RipeApiException.
                                MODEL_BAD_SUBSCRIPTION_PACKAGE,
                                debug_message=
                                u'Invalid Package Filter for subscriptions ({})'
                                .format(filters['package']))

                        sub = sub.filter(window_on_going__in=package)

                        if package[0] == 1:
                            self.pack = user_sub_ongoing

                        if package[0] == 0:
                            self.pack = user_sub_not_ongoing

                        if len(package) == 2:
                            self.pack = user_sub_not_ongoing + user_sub_ongoing

                        state = False
                        self.event_states = []
                        for _, n, event in self.pack:
                            if event == filters['user_state']:
                                state = True
                                self.event_states.append(n)

                        sub = sub.filter(
                            reduce(lambda q, f: q | Q(subscription_state=f),
                                   self.event_states, Q()))

                        if not state:
                            raise RipeApiException(
                                *RipeApiException.MODEL_BAD_SUBSCRIPTION_STATE,
                                debug_message=
                                u'Invalid State Filter for subscriptions ({})'.
                                format(filters['user_state']))

                    if 'territory' in keys:
                        sub = sub.filter(territory_code=filters['territory'])
                    if 'client' in keys:
                        sub = sub.filter(client_id=filters['client'])
                    if 'platform' in keys:
                        sub = sub.filter(platform_name=filters['platform'])
        return sub
Example #5
0
def registration_report(
        request,
        client,
        group,
        territory,
        platform,
        start_date,
        end_date, **kwargs):

    # use this for asserting whether we respond in json, or use CSV only allow either...
    response_format = str(request.GET.get('format', 'json')).lower()
    try:
        start_date = str(start_date) if start_date else 'all'
        end_date = str(end_date) if end_date else 'all'
    except Exception as e:
        return ApiSuccessHttpResponse(body={'status': '{}'.format(e.message)}, http_code=500)
    try:
        #if start_date != 'all':
        #    dates.character_checker(start_date)
        #if end_date != 'all':
        #    dates.character_checker(end_date)
        #dt = [start_date, end_date]
        #dt_dates = [datetime.datetime.strptime(d, DT_FORMAT).date() if d != 'all' else d for d in dt]
        # trace.info('dates: {}'.format(dt_dates))
        try:
            if start_date != 'all':
                start = datetime.datetime.strptime(start_date, DT_FORMAT).date()
            else:
                start = 'all'
            if end_date != 'all':
                end = datetime.datetime.strptime(end_date, DT_FORMAT).date()
            else:
                end = 'all'
        # trace.info('s: {} e: {}'.format(start, end))
        #if isinstance(start, datetime.date) and isinstance(end, datetime.date):
        #    if end < start:
        #        raise RipeApiException(
        #            *RipeApiException.BAD_DATE_RANGE,
        #            debug_message=u"End date '{}' is before '{}'".format(end, start)
        #        )
        except Exception as e:
            return ApiSuccessHttpResponse(body={'status': '{}'.format(e.message)}, http_code=200)

        group_key = group
        group = str(group) if group else 'all'

        params = {'client': client or 'all',
                  'platform': platform or 'all',
                  'territory': territory or 'all'}
        filters = {}
        # print 'ALL reg PARAMS: {}'.format(params)
        for k in params:
            if params[k] != 'all':
                filters[k] = str(params[k])

        trace.info('FILTERS: {}'.format(filters))
        trace.info('GROUP: {}'.format(group))

        #creates the structure.
        try:
            # trace.info('getting DATE RANGE BETWEEN: \ns:{} e: {}\n'.format(start, end))
            start, end, date_range = get_date_range(
                start,
                end,
                cube=registration,
                interval=kwargs.get('interval', None)
            )
            # trace.info('----------s:{} e: {}, range: {} range LEN: {}'.format(start, end, date_range, len(date_range)))

            if group == 'all':
                gen = ResponseGenerator('registrations',
                                        request.path,
                                        REG_ATTRIBUTES,
                                        REG_STRUCT,
                                        len(date_range) if len(date_range) else 1)
            else:
                trace.info('-----GROUPS BY {}------'.format(group))
                struct_with_groups = dict()
                struct_with_groups.update({'start': '', 'end': '', 'name': ''})
                grp_struct = {}
                # trace.info('----before struct {}'.format(grp_struct))

                #finds number of groups.
                groups, group_name = registration.get_groups(group)

                #FILTERED-GROUPS
                if group in filters:
                    # trace.info('----filtering groups: {} by {}'.format(group, filters[group]))
                    grp_struct.update({filters[group]: GROUP_SUMMARY_STRUCT})
                    # trace.info('----after struct FILTERED GRP {}'.format(grp_struct))
                else:
                    # trace.info('----all groups: {}'.format())
                    if groups:
                        trace.info('--creating struct for ALL groups: ---, group-name: {}, groups: {}'.format(group_name, groups))
                        for i in groups:
                            grp_struct.update({i[group_name]: GROUP_SUMMARY_STRUCT})
                        # trace.info('----after struct ALL GRP {}'.format(grp_struct))
                    else:
                        trace.info('{}')



                # trace.info('---- grp struct {}'.format(grp_struct))
                struct_with_groups.update(grp_struct)
                # trace.info('----curr rep struct {}'.format(struct_with_groups))
                gen = ResponseGenerator('registrations',
                                        request.path,
                                        REG_ATTRIBUTES,
                                        struct_with_groups,
                                        len(date_range) if len(date_range) else 1)
            gen.set_report_template()
            struct = gen.get_dict_response()
            trace.info('STRUCT created: '.format(struct))
        except Exception as e:
            return ApiSuccessHttpResponse(body={'status': '{}'.format(e.message)}, http_code=200)

        try:
            m = Mapper(struct)
            if not date_range:
                date_range = [(start, end)]
            i = 0
            for s, e in date_range:
                #if group == 'all':
                #    data = registration.get_total(filters, s, e, group)
                #    name = {'daily': s.strftime("%A"),
                #            'weekly': s.strftime("%W"),
                #            'monthly': s.strftime("%b"),
                #            'total': 'total'}
                #    data.update({'start': s.strftime(DT_FORMAT),
                #                 'end': e.strftime(DT_FORMAT),
                #                 'name': name[kwargs.get('interval', '')]})
                #    m.map_to_struct(Counter(data), 'current_report', i)
                #else:

                    #GROUPS
                ds = dict()
                ds.update({'start': s.strftime(DT_FORMAT),
                           'end': e.strftime(DT_FORMAT),
                           'name': 'test'})

                data = registration.get_total(filters, s, e, group)

                ds.update(data[0])
                #return ApiSuccessHttpResponse(body={'status': '{}'.format(ds)}, http_code=200)

                # trace.info('UPDATING LOC {} OF STRUCT {}'.format(i, struct))
                #from collections import Counter
                ##return ApiSuccessHttpResponse(body={'status': '{}'.format(Counter(ds))}, http_code=200)
                #ds = Counter(ds)
                #m.map_to_struct(ds, 'current_report', i)
                #i += 1
                struct = {'current_report': ds}

            # trace.info(' POPULATED STRUCT : {}'.format(struct))
        except Exception as e:
            return ApiSuccessHttpResponse(body={'status': '{}'.format(e)}, http_code=200)

        # previous_report = False
        # if start_date != 'all':
        #     previous_start_date, previous_end_date = dates.previous_report_date(start_date, end_date)
        #
        #     previous_results = CubeRegistrationsDaily.cube.total(
        #         filters, group, previous_start_date, previous_end_date)
        #
        #     previous_report = True

        result = struct

        if response_format == 'json':

            # results['attributes'] = {
            #     "client":       client,
            #     "groupby":      group_key,
            #     "territory":    territory,
            #     "platform":     platform,
            #     "start_date":   start_date,
            #     "end_date":     end_date,
            #     }

            # if previous_report and start != end:
            #     previous_results['attributes'] = {
            #         "client":       client,
            #         "groupby":      group_key,
            #         "territory":    territory,
            #         "platform":     platform,
            #         "start_date":  previous_start_date,
            #         "end_date":    previous_end_date
            #     }

            pass
            # if previous_report:
            #     result['user_subscription']['previous_report'] = previous_results
        elif response_format == 'csv':
            csv = BuildCsv('registrations')
            csv.input_json_structure(
                json=result
            )
            csv.build()
            return csv.response("registrations")
        else:
            raise RipeApiException(
                *RipeApiException.API_FORMAT,
                debug_message=u"format selection '{}' is not a valid format type".format(response_format)
            )

        return ApiSuccessHttpResponse(result)
    except Exception:
        trace.exception('\n---\n')
Example #6
0
def total_subscription_report(
        request,
        client,
        package,
        state,
        group,
        territory,
        platform,
        start_date,
        end_date, **kwargs):

    try:
        start_date = str(start_date) if start_date else 'all'
        # use this for asserting whether we respond in json, or use CSV only allow either...
        response_format = str(request.GET.get('format', 'json')).lower()
        if start_date != 'all':
            dates.character_checker(start_date)
            dates.character_checker(end_date)
            start = datetime.datetime.strptime(start_date, DT_FORMAT).date()
            end = datetime.datetime.strptime(end_date, DT_FORMAT).date()
            if end < start:
                raise RipeApiException(
                    *RipeApiException.BAD_DATE_RANGE,
                    debug_message=u"End date '{}' is before '{}'".format(end, start)
                )
        else:
            start = datetime.datetime.strptime('01-01-1977', DT_FORMAT).date()
            end = datetime.datetime.strptime('01-01-2999', DT_FORMAT).date()


        group_key = group
        group = str(group) if group else 'all'


        params = {'client': client or 'all',
                  'platform': platform or 'all',
                  'territory': territory or 'all',
                  'package': package or 'all',
                  'user_state': state or 'active'}

        filters = {}


        for k in params:
            if params[k] != 'all':
                filters[k] = str(params[k])



        # package is an exception
        filters['package'] = package

        fc = CubeSubscriptionsDaily.objects.filter(
            client_id=client,
            #package=
            subscription_state=state,
            #group=
            territory_code=territory,
            #platform=
            date__gte=start,
            date__lte=end
        ).latest('date')

        #if kwargs.get('interval', None) == 'monthly':
        #    try:
        #        results = SUBSCRIPTIONS.subscriptions.monthly(filters, group, start_date, end_date)
        #    except Exception:
        #        trace.exception('\n\n')
        #elif kwargs.get('interval', None) == 'weekly':
        #    try:
        #        results = SUBSCRIPTIONS.subscriptions.weekly(filters, group, start_date, end_date)
        #    except Exception:
        #        trace.exception('\nWEEKLY VIEW\n')
        #elif kwargs.get('interval', None) == 'daily':
        #    try:
        #        results = SUBSCRIPTIONS.subscriptions.daily_summary(filters, group, start_date, end_date)
        #    except Exception:
        #        trace.exception('\n---daily view---\n')
        #
        #else:
        #    try:
        #        results = SUBSCRIPTIONS.subscriptions.total(filters, group, start_date, end_date)
        #    except Exception:
        #        trace.exception('\n TOTAL IN RIPE API \n\n')
        #
        #previous_report = False
        #if start_date != 'all':
        #    previous_start_date, previous_end_date = dates.previous_report_date(start_date, end_date)
        #
        #    previous_results = SUBSCRIPTIONS.subscriptions.total(
        #        filters, group, previous_start_date, previous_end_date)
        #
        #    previous_report = True
        results = model_to_dict(fc)

        if response_format == 'json':
            results['attributes'] = {
                "client":       client,
                "package":      package,
                "state":        state,
                "groupby":      group_key,
                "territory":    territory,
                "platform":     platform,
                "start_date":   start_date,
                "end_date":     end_date,
                }

            # trace.info('DAILY SUMMARY RESULTS IN VIEW - 1 : {}\n'.format(results))
            # TODO
            #if previous_report and start != end:
            #    previous_results['attributes'] = {
            #        "client":       client,
            #        "package":      package,
            #        "state":        state,
            #        "groupby":      group_key,
            #        "territory":    territory,
            #        "platform":     platform,
            #        "start_date":  previous_start_date,
            #        "end_date":    previous_end_date
            #    }

            # trace.info('DAILY SUMMARY RESULTS IN VIEW - 2 : {}\n'.format(results))

            result = {
                "user_subscription": {
                    "current_report": results,
                    }
            }

            # TODO
            #if previous_report:
            #    result['user_subscription']['previous_report'] = previous_results


            # trace.info(' CURR REP : \n{}\n'.format(result['user_subscription']['current_report'].keys()))
            # trace.info(' FINAL REP : \n{}\n'.format(result['user_subscription']['current_report']))

            return ApiSuccessHttpResponse(result)
        elif response_format == 'csv':
            csv = BuildCsv()
            csv.input_json_structure(
                json=results
            )
            csv.build()
            return csv.response("subscriptions")
        else:
            raise RipeApiException(
                *RipeApiException.API_FORMAT,
                debug_message=u"format selection '{}' is not a valid format type".format(response_format)
            )
    except Exception:
        trace.exception('\n--??--\n')
Example #7
0
def fail(name, legal_chars):
    message = u"The field '{}' may only contain characters from the set '{}'.".format(
        name, legal_chars)
    raise RipeApiException(*RipeApiException.CHARACTER_CHECK_FAILURE,
                           debug_message=message)