from django.conf import settings
from django.dispatch import receiver
from django.utils import translation
from django.utils.translation import ugettext as _

from meinberlin.apps.dashboard2 import signals as a4dashboard_signals

from . import models


@receiver(a4dashboard_signals.module_component_updated)
def log_module_component_changes(sender, module, component, user, **kwargs):
    with translation.override(settings.DEFAULT_LANGUAGE):
        message = _(
            '"{username}" modified the component "{component_label}"'
            ' in the module "{module_name}"'
            ' of the project "{project_name}".').format(
                username=user.username,
                project_name=module.project.name,
                module_name=module.name,
                component_label=component.label)

    models.LogEntry.objects.create(
        message=message,
        action=models.MODULE_COMPONENT_UPDATED,
        actor=user,
        component_identifier=component.identifier,
        project=module.project,
        module=module,
    )


@receiver(a4dashboard_signals.project_component_updated)
def log_project_component_changes(sender, project, component, user, **kwargs):
    with translation.override(settings.DEFAULT_LANGUAGE):
        message = _(
            '"{username}" modified the component "{component_label}"'
            ' of the project "{project_name}".').format(
                username=user.username,
                project_name=project.name,
                component_label=component.label)

    models.LogEntry.objects.create(
        message=message,
        action=models.PROJECT_COMPONENT_UPDATED,
        actor=user,
        component_identifier=component.identifier,
        project=project,
        module=None
    )


@receiver(a4dashboard_signals.project_created)
def log_project_created(sender, project, user, **kwargs):
    with translation.override(settings.DEFAULT_LANGUAGE):
        message = _(
            '"{username}" created the project "{project_name}".').format(
                username=user.username,
                project_name=project.name)

    models.LogEntry.objects.create(
        message=message,
        action=models.PROJECT_CREATED,
        actor=user,
        project=project,
        module=None,
        component_identifier=''
    )


@receiver(a4dashboard_signals.project_published)
def log_project_published(sender, project, user, **kwargs):
    with translation.override(settings.DEFAULT_LANGUAGE):
        message = _(
            '"{username}" published the project "{project_name}".') \
            .format(username=user.username,
                    project_name=project.name)

    models.LogEntry.objects.create(
        message=message,
        action=models.PROJECT_PUBLISHED,
        actor=user,
        project=project,
        module=None,
        component_identifier=''
    )


@receiver(a4dashboard_signals.project_unpublished)
def log_project_unpublished(sender, project, user, **kwargs):
    with translation.override(settings.DEFAULT_LANGUAGE):
        message = _(
            '"{username}" depublished the project "{project_name}".') \
            .format(username=user.username,
                    project_name=project.name)

    models.LogEntry.objects.create(
        message=message,
        action=models.PROJECT_UNPUBLISHED,
        actor=user,
        project=project,
        module=None,
        component_identifier=''
    )

@@ -471,39 +471,17 @@
        '
-%22%7Busername%7D%22 modified t
+T
 he compo
@@ -600,59 +600,37 @@
 me%7D%22
-.').format(%0A                username=user.username,
+ has been modified.').format(
 %0A   
@@ -1242,31 +1242,9 @@
    '
-%22%7Busername%7D%22 modified t
+T
 he c
@@ -1322,59 +1322,37 @@
 me%7D%22
-.').format(%0A                username=user.username,
+ has been modified.').format(
 %0A   
@@ -1876,30 +1876,9 @@
    '
-%22%7Busername%7D%22 created t
+T
 he p
@@ -1904,59 +1904,36 @@
 me%7D%22
-.').format(%0A                username=user.username,
+ has been created.').format(
 %0A   
@@ -2384,32 +2384,9 @@
    '
-%22%7Busername%7D%22 published t
+T
 he p
@@ -2412,62 +2412,39 @@
 me%7D%22
-.') %5C%0A            .format(username=user.username,%0A    
+ has been published.').format(%0A
     
@@ -2900,34 +2900,9 @@
    '
-%22%7Busername%7D%22 depublished t
+T
 he p
@@ -2928,62 +2928,41 @@
 me%7D%22
-.') %5C%0A            .format(username=user.username,%0A    
+ has been unpublished.').format(%0A
     

remove username from adminlog message