# Path: backend/flindt/organization/models.py
# class Organization(FlindtBaseModel):
#     """
#     An organization couples roles and users to a specific organization.
#     This is useful to archive all roles for an organization.
#     An organization can also have API_KEYS for different integrations.
#     """
# 
#     name = models.CharField(unique=True, max_length=255)
#     glassfrog_api_key = models.CharField(max_length=255, blank=True)
#     slack_bot_api_key = models.CharField(max_length=255, blank=True)
#     glassfrog_anchor_circle_id = models.IntegerField(blank=True, null=True)
#     users = models.ManyToManyField(User, blank=True)
#     roles = models.ManyToManyField(
#         Role,
#         blank=True,
#         # Don't show archived roles in the admin.
#         limit_choices_to={'archived': False},
#     )
# 
#     def __str__(self):
#         return self.name
# 
#     def message_for_reminder(self):
#         """
#         Send a message to all users that have unfinished feedbacks that is
#         older than 7 days.
#         """
#         now = timezone.now()
#         week_ago = now - datetime.timedelta(days=7)
# 
#         message = _(
#             _('Hey, we noticed that people are waiting for your feedback, please help your colleagues by giving them '
#               'some feedback at {}/give-feedback.').
#             format(settings.FRONTEND_HOSTNAME)
#         )
#         for user in self.users.all():
#             if user.feedback_sent_feedback.filter(status=Feedback.INCOMPLETE, round__start_date__lt=week_ago).exists():
#                 messenger = Messenger(user=user)
#                 messenger.send_message(message)


from django.contrib import admin
from django.utils.translation import ugettext as _
from django_object_actions import DjangoObjectActions
from flindt.integrations.importer import GlassFrogImporter
from flindt.round.manager import IntegrationError
from .models import Organization
    def import_users(self, request, obj):
        g = GlassFrogImporter(api_key=obj.glassfrog_api_key, organization=obj)
        g.import_users()
        self.message_user(request, 'Users imported.')
    import_users.label = _('Import users for this organization')

    def import_roles(self, request, obj):
        g = GlassFrogImporter(api_key=obj.glassfrog_api_key, organization=obj)
        g.import_circles(anchor_circle_id=obj.glassfrog_anchor_circle_id)
        self.message_user(request, 'Roles imported.')
    import_roles.label = _('Import roles for this organization')

    def archive_roles(self, request, obj):
        obj.roles.update(archived=True)
        self.message_user(request, 'Roles archived.')
    archive_roles.label = _('Archive existing roles for this organization')

    def send_reminder(self, request, obj):
        try:
            obj.message_for_reminder()
            self.message_user(request, 'Reminder sent.')
        except IntegrationError as e:
            self.message_user(request, 'There was an integration error: %s' % e)

    send_reminder.label = _('Send reminder')
    send_reminder.short_description = _('Send a reminder to users that have unfinished feedback older than a week.')

    change_actions = ('import_users', 'import_roles', 'archive_roles', 'send_reminder')



# Path: backend/flindt/organization/models.py
# class Organization(FlindtBaseModel):
#     """
#     An organization couples roles and users to a specific organization.
#     This is useful to archive all roles for an organization.
#     An organization can also have API_KEYS for different integrations.
#     """
# 
#     name = models.CharField(unique=True, max_length=255)
#     glassfrog_api_key = models.CharField(max_length=255, blank=True)
#     slack_bot_api_key = models.CharField(max_length=255, blank=True)
#     glassfrog_anchor_circle_id = models.IntegerField(blank=True, null=True)
#     users = models.ManyToManyField(User, blank=True)
#     roles = models.ManyToManyField(
#         Role,
#         blank=True,
#         # Don't show archived roles in the admin.
#         limit_choices_to={'archived': False},
#     )
# 
#     def __str__(self):
#         return self.name
# 
#     def message_for_reminder(self):
#         """
#         Send a message to all users that have unfinished feedbacks that is
#         older than 7 days.
#         """
#         now = timezone.now()
#         week_ago = now - datetime.timedelta(days=7)
# 
#         message = _(
#             _('Hey, we noticed that people are waiting for your feedback, please help your colleagues by giving them '
#               'some feedback at {}/give-feedback.').
#             format(settings.FRONTEND_HOSTNAME)
#         )
#         for user in self.users.all():
#             if user.feedback_sent_feedback.filter(status=Feedback.INCOMPLETE, round__start_date__lt=week_ago).exists():
#                 messenger = Messenger(user=user)
#                 messenger.send_message(message)


# Path: backend/flindt/organization/admin.py
from django.contrib import admin
from django.utils.translation import ugettext as _
from django_object_actions import DjangoObjectActions
from flindt.integrations.importer import GlassFrogImporter
from flindt.round.manager import IntegrationError
from .models import Organization

    def import_users(self, request, obj):
        g = GlassFrogImporter(api_key=obj.glassfrog_api_key, organization=obj)
        g.import_users()
        self.message_user(request, 'Users imported.')
    import_users.label = _('Import users for this organization')

    def import_roles(self, request, obj):
        g = GlassFrogImporter(api_key=obj.glassfrog_api_key, organization=obj)
        g.import_circles(anchor_circle_id=obj.glassfrog_anchor_circle_id)
        self.message_user(request, 'Roles imported.')
    import_roles.label = _('Import roles for this organization')

    def archive_roles(self, request, obj):
        obj.roles.update(archived=True)
        self.message_user(request, 'Roles archived.')
    archive_roles.label = _('Archive existing roles for this organization')

    def send_reminder(self, request, obj):
        try:
            obj.message_for_reminder()
            self.message_user(request, 'Reminder sent.')
        except IntegrationError as e:
            self.message_user(request, 'There was an integration error: %s' % e)

    send_reminder.label = _('Send reminder')
    send_reminder.short_description = _('Send a reminder to users that have unfinished feedback older than a week.')

    change_actions = ('import_users', 'import_roles', 'archive_roles', 'send_reminder')



admin.site.register(Organization, OrganizationAdmin)