djeuscan: enhance email template

Signed-off-by: Corentin Chary <corentin.chary@gmail.com>
This commit is contained in:
Corentin Chary 2012-08-14 11:45:52 +02:00
parent 2b823fb905
commit 88b15c18d2
6 changed files with 111 additions and 70 deletions

View File

@ -126,3 +126,31 @@ def get_account_versionlogs(profile):
return VersionLog.objects.filter( return VersionLog.objects.filter(
Q(package__in=packages) | Q(overlay__in=overlays) Q(package__in=packages) | Q(overlay__in=overlays)
) )
def get_user_fav_infos(user):
upstream_k = lambda c: c["n_versions"] - c["n_packaged"] - c["n_overlay"]
categories = sorted(get_account_categories(user),
key=upstream_k, reverse=True)
c_upstream = sum([upstream_k(c) for c in categories])
herds = sorted(get_account_herds(user),
key=upstream_k, reverse=True)
h_upstream = sum([upstream_k(c) for c in herds])
maintainers = sorted(get_account_maintainers(user),
key=upstream_k, reverse=True)
m_upstream = sum([upstream_k(c) for c in maintainers])
packages = sorted(
get_profile(user).packages.all(),
key=lambda p: p.n_versions - p.n_packaged - p.n_overlay,
reverse=True
)
p_upstream = sum(
[c.n_versions - c.n_packaged - c.n_overlay for c in packages]
)
return {
"categories": categories, "categories_upstream": c_upstream,
"herds": herds, "herds_upstream": h_upstream,
"maintainers": maintainers, "maintainers_upstream": m_upstream,
"packages": packages, "packages_upstream": p_upstream,
}

View File

@ -19,7 +19,7 @@ from euscan.version import gentoo_unstable
from djeuscan.models import Package, RefreshPackageQuery, UserProfile, \ from djeuscan.models import Package, RefreshPackageQuery, UserProfile, \
VersionLog VersionLog
from djeuscan.processing import scan, misc from djeuscan.processing import scan, misc
from djeuscan.helpers import get_account_versionlogs from djeuscan.helpers import get_account_versionlogs, get_user_fav_infos
class TaskFailedException(Exception): class TaskFailedException(Exception):
@ -272,18 +272,15 @@ def consume_refresh_queue(locked=False):
def send_user_email(address, subject, text): def send_user_email(address, subject, text):
try: try:
send_mail( send_mail(
subject, text, settings.EMAIL_FROM, [address], fail_silently=False subject, text, settings.DEFAULT_FROM_EMAIL, [address], fail_silently=False
) )
except Exception, exc: except Exception, exc:
raise send_user_email.retry(exc=exc) raise send_user_email.retry(exc=exc)
@task @task
def process_emails(profiles): def process_emails(profiles, only_if_vlogs=False):
for profile in profiles: for profile in profiles:
if not profile.email_activated:
continue
now = datetime.now() now = datetime.now()
user = profile.user user = profile.user
@ -301,12 +298,16 @@ def process_emails(profiles):
vtype__in=gentoo_unstable vtype__in=gentoo_unstable
) )
if not vlogs.count(): if only_if_vlogs and not vlogs.count():
continue continue
infos = get_user_fav_infos(user)
infos["user"] = user
infos["vlogs"] = vlogs
mail_text = render_to_string( mail_text = render_to_string(
"euscan/accounts/euscan_email.txt", "euscan/accounts/euscan_email.txt",
{"user": user, "vlogs": vlogs} infos
) )
send_user_email.delay( send_user_email.delay(
@ -319,20 +320,32 @@ def process_emails(profiles):
@task @task
def send_update_email(): def send_update_email():
profiles = UserProfile.objects.filter(email_every=UserProfile.EMAIL_SCAN) profiles = UserProfile.objects.filter(
group_chunks(process_emails, profiles, settings.TASKS_EMAIL_GROUPS)() email_every=UserProfile.EMAIL_SCAN,
email_activated=True
)
group_chunks(
process_emails,
profiles,
settings.TASKS_EMAIL_GROUPS,
only_if_vlogs=True
)()
@task @task
def send_weekly_email(): def send_weekly_email():
profiles = UserProfile.objects.filter(email_every=UserProfile.EMAIL_WEEKLY) profiles = UserProfile.objects.filter(
email_every=UserProfile.EMAIL_WEEKLY,
email_activated=True
)
group_chunks(process_emails, profiles, settings.TASKS_EMAIL_GROUPS)() group_chunks(process_emails, profiles, settings.TASKS_EMAIL_GROUPS)()
@task @task
def send_monthly_email(): def send_monthly_email():
profiles = UserProfile.objects.filter( profiles = UserProfile.objects.filter(
email_every=UserProfile.EMAIL_MONTHLY email_every=UserProfile.EMAIL_MONTHLY,
email_activated=True
) )
group_chunks(process_emails, profiles, settings.TASKS_EMAIL_GROUPS)() group_chunks(process_emails, profiles, settings.TASKS_EMAIL_GROUPS)()

View File

@ -83,35 +83,7 @@
</ul> </ul>
</dd> </dd>
{% endif %} {% endif %}
{% if vlog %} {% include "euscan/_package_vlogs.html" %}
<dt>Version history</dt>
<dd>
<ul class="log">
{% for version in vlog %}
{% if version.action == version.VERSION_ADDED %}
<li class="added">
{% else %}
<li class="removed">
{% endif %}
{% if version.overlay == "gentoo" %}
<img src="{{ STATIC_URL }}img/gentoo-icon.png" alt="gentoo" title="In Gentoo" />
{% elif version.overlay %}
<img src="{{ STATIC_URL }}img/overlay-icon.png" alt="overlays" title="In Overlays" />
{% else %}
<img src="{{ STATIC_URL }}img/upstream-icon.png" alt="upstream" title="Upstream" />
{% endif %}
{% if version.vtype and not version.vtype|is_stable %}
<img src="{{ STATIC_URL }}img/unstable-icon.png" alt="unstable" title="{{ version.vtype }}" />
{% endif %}
{{ version }} - {{ version.datetime }}
</li>
{% endfor %}
</ul>
</dd>
{% endif %}
{% if log %} {% if log %}
<dt>euscan log</dt> <dt>euscan log</dt>
<dd> <dd>

View File

@ -1,6 +1,50 @@
{% load djeuscan_helpers %}
{% load sub %}
Hello {{ user }}, Hello {{ user }},
euscan news: {% if vlogs %}euscan news:
{% for vlog in vlogs %} {% for vlog in vlogs %}
* {{ vlog.package.category }}/{{ vlog.package.name }}-{{ vlog.version }}-{{ vlog.revision }}:{{ vlog.slot }} [{{ vlog.vtype }}] * {{ vlog }} {% if vlog.vtype and not vlog.vtype|is_stable %}({{ vlog.vtype }}){% endif %} - {{ vlog.datetime }} {% endfor %}
{% endfor %} {% endif %}
Summary:
{% with categories|length as num_categories %}{% if num_categories %}
Watching {{ num_categories }} categor{{ num_categories|pluralize:"y,ies"}}
{{ categories_upstream }} new upstream version{{ categories_upstream|pluralize:"s"}}
{% if categories_upstream %}
Hot categories:
{% for c in categories|slice:":3" %}
* {{ c.category }} ({{ c.n_versions|sub:c.n_packaged|sub:c.n_overlay }}){% endfor %}
{% endif %}{% endif %}{% endwith %}
{% with herds|length as num_herds %}{% if num_herds %}
Watching {{ num_herds }} herd{{ num_herds|pluralize:"s"}}
{{ herds_upstream }} new upstream version{{ herds_upstream|pluralize:"s"}}
{% if herds_upstream %}
Hot herds:
{% for c in herds|slice:":3" %}
* {{ c.herds__herd }} ({{ c.n_versions|sub:c.n_packaged|sub:c.n_overlay }}){% endfor %}
{% endif %}{% endif %}{% endwith %}
{% with maintainers|length as num_maintainers %}{% if num_maintainers %}
Watching {{ num_maintainers }} maintainer{{ num_maintainers|pluralize:"s"}}
{{ maintainers_upstream }} new upstream version{{ maintainers_upstream|pluralize:"s"}}
{% if maintainers_upstream %}
Hot maintainers:
{% for c in maintainers|slice:":3" %}
* {{ c.maintainers__name }} ({{ c.n_versions|sub:c.n_packaged|sub:c.n_overlay }}){% endfor %}
{% endif %}{% endif %}{% endwith %}
{% with packages|length as num_packages %}{% if num_packages %}
Watching {{ num_packages }} package{{ num_packages|pluralize:"s"}}
{{ packages_upstream }} new upstream version{{ packages_upstream|pluralize:"s"}}
{% if packages_upstream %}
Hot packages:
{% for c in packages|slice:":3" %}
* {{ c }} ({{ c.n_versions|sub:c.n_packaged|sub:c.n_overlay }}){% endfor %}
{% endif %}{% endif %}{% endwith %}
--
This email was sent by euscan

View File

@ -113,4 +113,8 @@
{% endwith %} {% endwith %}
</div> </div>
</div> </div>
<dl>
{% include "euscan/_package_vlogs.html" %}
</dl>
{% endblock %} {% endblock %}

View File

@ -9,7 +9,9 @@ from django.contrib.auth.decorators import login_required
from django.views.decorators.http import require_POST from django.views.decorators.http import require_POST
from djeuscan.helpers import version_key, packages_from_names, get_profile, \ from djeuscan.helpers import version_key, packages_from_names, get_profile, \
get_account_categories, get_account_herds, get_account_maintainers get_account_categories, get_account_herds, get_account_maintainers, \
get_user_fav_infos
from djeuscan.feeds import UserFeed
from djeuscan.models import Version, Package, Herd, Maintainer, EuscanResult, \ from djeuscan.models import Version, Package, Herd, Maintainer, EuscanResult, \
VersionLog, RefreshPackageQuery, ProblemReport, Category, Overlay VersionLog, RefreshPackageQuery, ProblemReport, Category, Overlay
from djeuscan.forms import WorldForm, PackagesForm, ProblemReportForm, \ from djeuscan.forms import WorldForm, PackagesForm, ProblemReportForm, \
@ -397,34 +399,12 @@ def refresh_package(request, category, package):
@login_required @login_required
@render_to('euscan/accounts/index.html') @render_to('euscan/accounts/index.html')
def accounts_index(request): def accounts_index(request):
user = request.user user = request.user
upstream_k = lambda c: c["n_versions"] - c["n_packaged"] - c["n_overlay"]
categories = sorted(get_account_categories(user), infos = get_user_fav_infos(user)
key=upstream_k, reverse=True) infos['vlog'] = UserFeed().items({'user':user, 'options' : {}})
c_upstream = sum([upstream_k(c) for c in categories])
herds = sorted(get_account_herds(request.user),
key=upstream_k, reverse=True)
h_upstream = sum([upstream_k(c) for c in herds])
maintainers = sorted(get_account_maintainers(request.user),
key=upstream_k, reverse=True)
m_upstream = sum([upstream_k(c) for c in maintainers])
packages = sorted(
get_profile(user).packages.all(),
key=lambda p: p.n_versions - p.n_packaged - p.n_overlay,
reverse=True
)
p_upstream = sum(
[c.n_versions - c.n_packaged - c.n_overlay for c in packages]
)
return {
"categories": categories, "categories_upstream": c_upstream,
"herds": herds, "herds_upstream": h_upstream,
"maintainers": maintainers, "maintainers_upstream": m_upstream,
"packages": packages, "packages_upstream": p_upstream,
}
return infos
@login_required @login_required
@render_to('euscan/accounts/preferences.html') @render_to('euscan/accounts/preferences.html')