Exemplo n.º 1
0
def delete_ng_report(sender, instance, **kwargs):
    """Automatically update user's streak counters."""
    today = get_date()
    current_start = instance.user.userprofile.current_streak_start or None
    longest_start = instance.user.userprofile.longest_streak_start or None
    longest_end = instance.user.userprofile.longest_streak_end or None

    # If instance is in the future or there is another
    # report that date, don't do anything
    if (instance.is_future_report or
            (NGReport.objects.filter(user=instance.user, report_date=instance.report_date)
                             .exclude(pk=instance.id).exists())):
        return

    try:
        next_report = instance.get_next_by_report_date(user=instance.user, report_date__lte=today)
    except NGReport.DoesNotExist:
        next_report = None

    try:
        previous_report = instance.get_previous_by_report_date(user=instance.user)
    except NGReport.DoesNotExist:
        previous_report = None

    # There aren't any reports
    if not next_report and not previous_report:
        current_start = None
        longest_start = None
        longest_end = None

    # If the deleted report is between the range of the longest
    # streak counters, we need to update them.
    elif (longest_start and longest_end and
            instance.report_date in daterange(longest_start, longest_end)):

        if longest_start == instance.report_date and next_report:
            longest_start = next_report.report_date
        elif longest_end == instance.report_date and previous_report:
            longest_end = previous_report.report_date
        elif (previous_report and next_report and
              (next_report.report_date -
               previous_report.report_date).days > 7):
            # Compare the number of reports registered from the starting point
            # of the longest streak up until the date of the deleted report,
            # with the number of reports registered from the date of the
            # deleted report until the end of the longest streak.
            lower_half_report_count = NGReport.objects.filter(
                report_date__range=(longest_start, instance.report_date),
                user=instance.user).count()
            upper_half_report_count = NGReport.objects.filter(
                report_date__range=(instance.report_date, longest_end),
                user=instance.user).count()

            # If the first time slice contains more reports, then we need
            # to move the end of the longest streak, just before
            # the deletion point. If the opposite is true, move the starting
            # point of the longest streak just after the deletion point.
            if (lower_half_report_count >= upper_half_report_count and
                    previous_report.report_date >= longest_start):
                longest_end = previous_report.report_date
            elif (upper_half_report_count > lower_half_report_count and
                    next_report.report_date <= longest_end):
                longest_start = next_report.report_date

    # If the deleted report is between the range of the current
    # streak counter and today, then we need to update the counter.
    if current_start and instance.report_date in daterange(current_start, today):
        if current_start == instance.report_date and next_report:
            current_start = next_report.report_date
        elif (previous_report and next_report and
                (next_report.report_date -
                 previous_report.report_date).days > 7):
            current_start = next_report.report_date

    instance.user.userprofile.current_streak_start = current_start
    instance.user.userprofile.longest_streak_start = longest_start
    instance.user.userprofile.longest_streak_end = longest_end
    instance.user.userprofile.save()
Exemplo n.º 2
0
    def save(self, *args, **kwargs):
        """Override save for custom functionality."""

        # Make last report notificaton date NULL on new report
        today = get_date()
        if self.pk is None and self.user.userprofile.first_report_notification:
            start = today - datetime.timedelta(weeks=4)
            end = today + datetime.timedelta(weeks=4)
            if self.report_date in daterange(start, end):
                self.user.userprofile.first_report_notification = None
                self.user.userprofile.second_report_notification = None
                self.user.userprofile.save()

        # User streaks functionality
        one_week = datetime.timedelta(7)
        current_start = self.user.userprofile.current_streak_start or None
        longest_start = self.user.userprofile.longest_streak_start or None
        longest_end = self.user.userprofile.longest_streak_end or None

        # Save the mentor of the user if no mentor is defined.
        if not self.mentor:
            self.mentor = self.user.userprofile.mentor
        # Save the country if possible
        saved_report = get_object_or_none(NGReport, id=self.id)
        if (saved_report and (saved_report.latitude != self.latitude or
                              saved_report.longitude != self.longitude)):
            country = None
            try:
                country = self.location.split(',')[-1].strip()
            except IndexError:
                pass
            if country in COUNTRIES_LIST:
                self.country = country
        super(NGReport, self).save()

        # Resolve the verified action items.
        action_item_name = u'{0} {1}'.format(VERIFY_ACTION, self.user.get_full_name())
        if self.verified_activity:
            ActionItem.resolve(instance=self,
                               user=self.mentor,
                               name=action_item_name)
        else:
            # Create all the action items, if any
            ActionItem.create(self)

        if self.is_future_report:
            return

        # If there is already a running streak and the report date
        # is within this streak, update the current streak counter.
        if (current_start and self.report_date < current_start and
                self.report_date in daterange((current_start - one_week), current_start)):
            current_start = self.report_date
        # If there isn't any current streak, and the report date
        # is within the current week, let's start the counting.
        elif not current_start and self.report_date in daterange(get_date(-7), today):
            current_start = self.report_date

        # Longest streak section
        # If longest streak already exists, let's update it.
        if longest_start and longest_end:

            # Compare the number of reports registered during
            # the current streak and the number of reports
            # during the longest streak. If current streak is bigger
            # than the previous longest streak, update the longest streak.
            longest_streak_count = NGReport.objects.filter(
                report_date__range=(longest_start, longest_end),
                user=self.user).count()
            current_streak_count = NGReport.objects.filter(
                report_date__range=(current_start, today),
                user=self.user).count()
            if current_start and current_streak_count > longest_streak_count:
                longest_start = current_start
                longest_end = today

            # This happens only when a user appends a report, dated in the
            # range of longest streak counters and it's out of the range
            # of current streak counter.
            elif self.report_date in daterange(longest_start - one_week, longest_end + one_week):
                if self.report_date < longest_start:
                    longest_start = self.report_date
                elif self.report_date > longest_end:
                    longest_end = self.report_date
        else:
            # Longest streak counters are empty, let's setup their value
            longest_start = self.report_date
            longest_end = self.report_date
        # Assign the calculated values, to user's profile.
        self.user.userprofile.current_streak_start = current_start
        self.user.userprofile.longest_streak_start = longest_start
        self.user.userprofile.longest_streak_end = longest_end
        self.user.userprofile.save()
Exemplo n.º 3
0
    def save(self, *args, **kwargs):
        """Override save for custom functionality."""

        # Make last report notificaton date NULL on new report
        today = get_date()
        if self.pk is None and self.user.userprofile.first_report_notification:
            start = today - datetime.timedelta(weeks=4)
            end = today + datetime.timedelta(weeks=4)
            if self.report_date in daterange(start, end):
                self.user.userprofile.first_report_notification = None
                self.user.userprofile.second_report_notification = None
                self.user.userprofile.save()

        # User streaks functionality
        one_week = datetime.timedelta(7)
        current_start = self.user.userprofile.current_streak_start or None
        longest_start = self.user.userprofile.longest_streak_start or None
        longest_end = self.user.userprofile.longest_streak_end or None

        # Save the mentor of the user if no mentor is defined.
        if not self.mentor:
            self.mentor = self.user.userprofile.mentor
        super(NGReport, self).save()

        if self.is_future_report:
            return

        # If there is already a running streak and the report date
        # is within this streak, update the current streak counter.
        if (current_start and self.report_date < current_start and
            self.report_date in daterange((current_start - one_week),
                                          current_start)):
            current_start = self.report_date
        # If there isn't any current streak, and the report date
        # is within the current week, let's start the counting.
        elif (not current_start and
                self.report_date in daterange(get_date(-7), today)):
            current_start = self.report_date

        # Longest streak section
        # If longest streak already exists, let's update it.
        if longest_start and longest_end:

            # Compare the number of reports registered during
            # the current streak and the number of reports
            # during the longest streak. If current streak is bigger
            # than the previous longest streak, update the longest streak.
            longest_streak_count = NGReport.objects.filter(
                report_date__range=(longest_start, longest_end),
                user=self.user).count()
            current_streak_count = NGReport.objects.filter(
                report_date__range=(current_start, today),
                user=self.user).count()
            if current_start and current_streak_count > longest_streak_count:
                longest_start = current_start
                longest_end = today

            # This happens only when a user appends a report, dated in the
            # range of longest streak counters and it's out of the range
            # of current streak counter.
            elif self.report_date in daterange(longest_start - one_week,
                                               longest_end + one_week):
                if self.report_date < longest_start:
                    longest_start = self.report_date
                elif self.report_date > longest_end:
                    longest_end = self.report_date
        else:
            # Longest streak counters are empty, let's setup their value
            longest_start = self.report_date
            longest_end = self.report_date
        # Assign the calculated values, to user's profile.
        self.user.userprofile.current_streak_start = current_start
        self.user.userprofile.longest_streak_start = longest_start
        self.user.userprofile.longest_streak_end = longest_end
        self.user.userprofile.save()
Exemplo n.º 4
0
    def save(self, *args, **kwargs):
        """Override save method."""
        one_week = datetime.timedelta(7)
        today = get_date()
        current_start = self.user.userprofile.current_streak_start or None
        longest_start = self.user.userprofile.longest_streak_start or None
        longest_end = self.user.userprofile.longest_streak_end or None

        # Save the mentor of the user if no mentor is defined.
        if not self.mentor:
            self.mentor = self.user.userprofile.mentor
        super(NGReport, self).save()

        if self.is_future_report:
            return

        # If there is already a running streak and the report date
        # is within this streak, update the current streak counter.
        if (current_start and self.report_date < current_start and
            self.report_date in daterange((current_start - one_week),
                                          current_start)):
            current_start = self.report_date
        # If there isn't any current streak, and the report date
        # is within the current week, let's start the counting.
        elif (not current_start and
                self.report_date in daterange(get_date(-7), today)):
            current_start = self.report_date

        # Longest streak section
        # If longest streak already exists, let's update it.
        if longest_start and longest_end:

            # Compare the number of reports registered during
            # the current streak and the number of reports
            # during the longest streak. If current streak is bigger
            # than the previous longest streak, update the longest streak.
            longest_streak_count = NGReport.objects.filter(
                report_date__range=(longest_start, longest_end),
                user=self.user).count()
            current_streak_count = NGReport.objects.filter(
                report_date__range=(current_start, today),
                user=self.user).count()
            if current_start and current_streak_count > longest_streak_count:
                longest_start = current_start
                longest_end = today

            # This happens only when a user appends a report, dated in the
            # range of longest streak counters and it's out of the range
            # of current streak counter.
            elif self.report_date in daterange(longest_start - one_week,
                                               longest_end + one_week):
                if self.report_date < longest_start:
                    longest_start = self.report_date
                elif self.report_date > longest_end:
                    longest_end = self.report_date
        else:
            # Longest streak counters are empty, let's setup their value
            longest_start = self.report_date
            longest_end = self.report_date
        # Assign the calculated values, to user's profile.
        self.user.userprofile.current_streak_start = current_start
        self.user.userprofile.longest_streak_start = longest_start
        self.user.userprofile.longest_streak_end = longest_end
        self.user.userprofile.save()
Exemplo n.º 5
0
def delete_ng_report(sender, instance, **kwargs):
    """Automatically update user's streak counters."""
    today = get_date()
    current_start = instance.user.userprofile.current_streak_start or None
    longest_start = instance.user.userprofile.longest_streak_start or None
    longest_end = instance.user.userprofile.longest_streak_end or None

    # If instance is in the future or there is another
    # report that date, don't do anything
    if (instance.is_future_report or (NGReport.objects.filter(
            user=instance.user, report_date=instance.report_date).exclude(
                pk=instance.id).exists())):
        return

    try:
        next_report = instance.get_next_by_report_date(user=instance.user,
                                                       report_date__lte=today)
    except NGReport.DoesNotExist:
        next_report = None

    try:
        previous_report = instance.get_previous_by_report_date(
            user=instance.user)
    except NGReport.DoesNotExist:
        previous_report = None

    # There aren't any reports
    if not next_report and not previous_report:
        current_start = None
        longest_start = None
        longest_end = None

    # If the deleted report is between the range of the longest
    # streak counters, we need to update them.
    elif (longest_start and longest_end
          and instance.report_date in daterange(longest_start, longest_end)):

        if longest_start == instance.report_date and next_report:
            longest_start = next_report.report_date
        elif longest_end == instance.report_date and previous_report:
            longest_end = previous_report.report_date
        elif (
                previous_report and next_report and
            (next_report.report_date - previous_report.report_date).days > 7):
            # Compare the number of reports registered from the starting point
            # of the longest streak up until the date of the deleted report,
            # with the number of reports registered from the date of the
            # deleted report until the end of the longest streak.
            lower_half_report_count = NGReport.objects.filter(
                report_date__range=(longest_start, instance.report_date),
                user=instance.user).count()
            upper_half_report_count = NGReport.objects.filter(
                report_date__range=(instance.report_date, longest_end),
                user=instance.user).count()

            # If the first time slice contains more reports, then we need
            # to move the end of the longest streak, just before
            # the deletion point. If the opposite is true, move the starting
            # point of the longest streak just after the deletion point.
            if (lower_half_report_count >= upper_half_report_count
                    and previous_report.report_date >= longest_start):
                longest_end = previous_report.report_date
            elif (upper_half_report_count > lower_half_report_count
                  and next_report.report_date <= longest_end):
                longest_start = next_report.report_date

    # If the deleted report is between the range of the current
    # streak counter and today, then we need to update the counter.
    if (current_start
            and instance.report_date in daterange(current_start, today)):
        if current_start == instance.report_date and next_report:
            current_start = next_report.report_date
        elif (
                previous_report and next_report and
            (next_report.report_date - previous_report.report_date).days > 7):
            current_start = next_report.report_date

    instance.user.userprofile.current_streak_start = current_start
    instance.user.userprofile.longest_streak_start = longest_start
    instance.user.userprofile.longest_streak_end = longest_end
    instance.user.userprofile.save()
Exemplo n.º 6
0
    def save(self, *args, **kwargs):
        """Override save for custom functionality."""

        # Make last report notificaton date NULL on new report
        today = get_date()
        if self.pk is None and self.user.userprofile.first_report_notification:
            start = today - datetime.timedelta(weeks=4)
            end = today + datetime.timedelta(weeks=4)
            if self.report_date in daterange(start, end):
                self.user.userprofile.first_report_notification = None
                self.user.userprofile.second_report_notification = None
                self.user.userprofile.save()

        # User streaks functionality
        one_week = datetime.timedelta(7)
        current_start = self.user.userprofile.current_streak_start or None
        longest_start = self.user.userprofile.longest_streak_start or None
        longest_end = self.user.userprofile.longest_streak_end or None

        # Save the mentor of the user if no mentor is defined.
        if not self.mentor:
            self.mentor = self.user.userprofile.mentor
        # Save the country if possible
        saved_report = get_object_or_none(NGReport, id=self.id)
        if (saved_report and (saved_report.latitude != self.latitude
                              or saved_report.longitude != self.longitude)):
            country = None
            try:
                country = self.location.split(',')[-1].strip()
            except IndexError:
                pass
            if country in COUNTRIES_LIST:
                self.country = country
        super(NGReport, self).save()

        if self.is_future_report:
            return

        # If there is already a running streak and the report date
        # is within this streak, update the current streak counter.
        if (current_start and self.report_date < current_start
                and self.report_date in daterange(
                    (current_start - one_week), current_start)):
            current_start = self.report_date
        # If there isn't any current streak, and the report date
        # is within the current week, let's start the counting.
        elif (not current_start
              and self.report_date in daterange(get_date(-7), today)):
            current_start = self.report_date

        # Longest streak section
        # If longest streak already exists, let's update it.
        if longest_start and longest_end:

            # Compare the number of reports registered during
            # the current streak and the number of reports
            # during the longest streak. If current streak is bigger
            # than the previous longest streak, update the longest streak.
            longest_streak_count = NGReport.objects.filter(
                report_date__range=(longest_start, longest_end),
                user=self.user).count()
            current_streak_count = NGReport.objects.filter(
                report_date__range=(current_start, today),
                user=self.user).count()
            if current_start and current_streak_count > longest_streak_count:
                longest_start = current_start
                longest_end = today

            # This happens only when a user appends a report, dated in the
            # range of longest streak counters and it's out of the range
            # of current streak counter.
            elif self.report_date in daterange(longest_start - one_week,
                                               longest_end + one_week):
                if self.report_date < longest_start:
                    longest_start = self.report_date
                elif self.report_date > longest_end:
                    longest_end = self.report_date
        else:
            # Longest streak counters are empty, let's setup their value
            longest_start = self.report_date
            longest_end = self.report_date
        # Assign the calculated values, to user's profile.
        self.user.userprofile.current_streak_start = current_start
        self.user.userprofile.longest_streak_start = longest_start
        self.user.userprofile.longest_streak_end = longest_end
        self.user.userprofile.save()