Commit e6c4ddd0 authored by Laurent Peuch's avatar Laurent Peuch
Browse files

[enh] start to display diff of modifieds members in a command

parent 23cc923e
import re
from django import template
from django.conf import settings
numeric_test = re.compile("^\d+$")
register = template.Library()
@register.filter
def get_field(entry, field):
"""Gets an attribute of an object dynamically from a string name"""
if field.name in entry.field_dict:
if field.choices:
return getattr(entry.object, "get_%s_display" % field.name)()
return entry.field_dict[field.name]
else:
return settings.TEMPLATE_STRING_IF_INVALID
@register.filter
def only_existing(fields_list, entry):
for i in fields_list:
if entry.field_dict[i.name]:
yield i
import reversion
from datetime import date, timedelta
from django.core.mail import send_mail
from django.core.management.base import BaseCommand
from django.template import Context, Template
from members.models import Member
email_template = """\
{% load getattribute %}
Hello everyone,
Here are the modified and created user entries from yesterday :
{% for entry, users in data.created %}
'{{ entry.object }}' created by {{ users }}:
{% for field in fields|only_existing:entry %}* {% if field.verbose_name %}{{ field.verbose_name }}{% else %}{{ field.name }}{% endif %}: {{ entry|get_field:field }}
{% endfor %}{% endfor %}
{% for new, old, users in data.modified %}
{% endfor %}
Thanks for your work <3
"""
class Command(BaseCommand):
args = '<poll_id poll_id ...>'
help = 'Closes the specified poll for voting'
def handle(self, *args, **options):
data = {
"created": [],
"modified": [],
}
today = date.today()
yesterday = today - timedelta(days=1)
# modified_yesterday_members = Member.objects.filter(last_modified__lt=date.today(), last_modified__gt=yesterday)
modified_yesterday_members = Member.objects.filter(last_modified__gt=date.today())
for member_revisions in map(reversion.get_for_object, modified_yesterday_members):
member_revisions = list(member_revisions.get_unique())
# yesterday_modifications = filter(lambda x: x.field_dict["last_modified"].date() == yesterday, member_revisions)
yesterday_modifications = filter(lambda x: x.field_dict["last_modified"].date() == today, member_revisions)
users_that_has_modified_the_documented = u", ".join(set(map(lambda x: unicode(x.revision.user), yesterday_modifications)))
# older_modifications = filter(lambda x: x.field_dict["last_modified"].date() < yesterday, member_revisions)
older_modifications = filter(lambda x: x.field_dict["last_modified"].date() < today, member_revisions)
if not older_modifications:
data["created"].append([yesterday_modifications[0], users_that_has_modified_the_documented])
else:
data["modified"].append([yesterday_modifications[0], older_modifications[0], users_that_has_modified_the_documented])
fields = filter(lambda x: x.name not in ('added_on', 'last_modified', 'id', 'pk'), Member._meta.fields)
email_content = Template(email_template).render(Context({
"data": data,
"fields": fields,
}))
send_mail('[NeutrinetMembersManangement] modifications of %s' % yesterday.strftime("%F"), email_content, 'noreply@neutrinet.be', ['administration@neutrinet.be'], fail_silently=False)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment