<commit_before>from datetime import datetime

from celery.decorators import periodic_task
from celery.schedules import crontab
from celery.task import task
from django.conf import settings
from django.contrib.sites.models import Site
from django.db.models import F
from django.utils.translation import ugettext_lazy as _
from haystack import site

from utils import send_templated_email
from widget.video_cache import (
    invalidate_cache as invalidate_video_cache,
    invalidate_video_moderation
)


@task()
def invalidate_video_caches(team_id):
    """Invalidate all TeamVideo caches for all the given team's videos."""
    from apps.teams.models import Team
    team = Team.objects.get(pk=team_id)
    for video_id in team.teamvideo_set.values_list('video__video_id', flat=True):
        invalidate_video_cache(video_id)

@task()
def invalidate_video_moderation_caches(team):
    """Invalidate the moderation status caches for all the given team's videos."""
    for video_id in team.teamvideo_set.values_list('video__video_id', flat=True):
        invalidate_video_moderation(video_id)

@task()
def update_video_moderation(team):
    """Set the moderated_by field for all the given team's videos."""
    from apps.videos.models import Video

    moderated_by = team if team.moderates_videos() else None
    Video.objects.filter(teamvideo__team=team).update(moderated_by=moderated_by)


@periodic_task(run_every=crontab(minute=0, hour=7))
def expire_tasks():
    """Find any tasks that are past their expiration date and unassign them.

    We currently run this once per day (at 7 AM server time).

    """
    from teams.models import Task

    expired_tasks = Task.objects.incomplete().filter(
        expiration_date__isnull=False,
        expiration_date__lt=datetime.now(),
    )
    for task in expired_tasks:
        task.assignee = task.expiration_date = None
        task.save()


@periodic_task(run_every=crontab(minute=0, hour=12))
def add_videos_notification(*args, **kwargs):
    from teams.models import TeamVideo, Team
    domain = Site.objects.get_current().domain

    qs = Team.objects.filter(teamvideo__created__gt=F('last_notification_time')).distinct()

    for team in qs:
        team_videos = TeamVideo.objects.filter(team=team, created__gt=team.last_notification_time)

        team.last_notification_time = datetime.now()
        team.save()
        members = team.users.filter( notify_by_email=True, is_active=True) \
            .distinct()

        subject = _(u'New %(team)s videos ready for subtitling!') % dict(team=team)

        for user in members:
            if not user.email:
                continue

            context = {
                'domain': domain,
                'user': user,
                'team': team,
                'team_videos': team_videos,
                "STATIC_URL": settings.STATIC_URL,
            }

            send_templated_email(user, subject,
                                 'teams/email_new_videos.html',<commit_msg>Changed the time for running video notifications tasks, this should reduce server load during peak hours<commit_after>from datetime import datetime

from celery.decorators import periodic_task
from celery.schedules import crontab
from celery.task import task
from django.conf import settings
from django.contrib.sites.models import Site
from django.db.models import F
from django.utils.translation import ugettext_lazy as _
from haystack import site

from utils import send_templated_email
from widget.video_cache import (
    invalidate_cache as invalidate_video_cache,
    invalidate_video_moderation
)


@task()
def invalidate_video_caches(team_id):
    """Invalidate all TeamVideo caches for all the given team's videos."""
    from apps.teams.models import Team
    team = Team.objects.get(pk=team_id)
    for video_id in team.teamvideo_set.values_list('video__video_id', flat=True):
        invalidate_video_cache(video_id)

@task()
def invalidate_video_moderation_caches(team):
    """Invalidate the moderation status caches for all the given team's videos."""
    for video_id in team.teamvideo_set.values_list('video__video_id', flat=True):
        invalidate_video_moderation(video_id)

@task()
def update_video_moderation(team):
    """Set the moderated_by field for all the given team's videos."""
    from apps.videos.models import Video

    moderated_by = team if team.moderates_videos() else None
    Video.objects.filter(teamvideo__team=team).update(moderated_by=moderated_by)


@periodic_task(run_every=crontab(minute=0, hour=7))
def expire_tasks():
    """Find any tasks that are past their expiration date and unassign them.

    We currently run this once per day (at 7 AM server time).

    """
    from teams.models import Task

    expired_tasks = Task.objects.incomplete().filter(
        expiration_date__isnull=False,
        expiration_date__lt=datetime.now(),
    )
    for task in expired_tasks:
        task.assignee = task.expiration_date = None
        task.save()


@periodic_task(run_every=crontab(minute=0, hour=23))
def add_videos_notification(*args, **kwargs):
    from teams.models import TeamVideo, Team
    domain = Site.objects.get_current().domain

    qs = Team.objects.filter(teamvideo__created__gt=F('last_notification_time')).distinct()

    for team in qs:
        team_videos = TeamVideo.objects.filter(team=team, created__gt=team.last_notification_time)

        team.last_notification_time = datetime.now()
        team.save()
        members = team.users.filter( notify_by_email=True, is_active=True) \
            .distinct()

        subject = _(u'New %(team)s videos ready for subtitling!') % dict(team=team)

        for user in members:
            if not user.email:
                continue

            context = {
                'domain': domain,
                'user': user,
                'team': team,
                'team_videos': team_videos,
                "STATIC_URL": settings.STATIC_URL,
            }

            send_templated_email(user, subject,
                                 'teams/email_new_videos.html',
{'fraction_non_alphanumeric': 0.08753489899819346, 'fraction_numerical': 0.0019707669568073574, 'mean_word_length': 3.313031161473088, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '765080', 'n_tokens_mistral': 1858, 'n_tokens_neox': 1762, 'n_words': 454}