Beispiel #1
0
def notify(request):
    data = {}
    data['page_title'] = "Notify about new PTO"
    if request.method == 'POST':
        form = forms.AddForm(request.user, data=request.POST)
        if form.is_valid():
            start = form.cleaned_data['start']
            end = form.cleaned_data['end']
            details = form.cleaned_data['details'].strip()
            notify = form.cleaned_data['notify']
            entry = Entry.objects.create(
                user=request.user,
                start=start,
                end=end,
                details=details,
            )
            clean_unfinished_entries(entry)

            messages.info(request, 'Entry added, now specify hours')
            url = reverse('dates.hours', args=[entry.pk])
            request.session['notify_extra'] = notify
            return redirect(url)
    else:
        initial = {}
        if request.GET.get('start'):
            try:
                initial['start'] = parse_datetime(request.GET['start'])
            except DatetimeParseError:
                pass
        if request.GET.get('end'):
            try:
                initial['end'] = parse_datetime(request.GET['end'])
            except DatetimeParseError:
                pass
        form = forms.AddForm(request.user, initial=initial)

    profile = request.user.get_profile()
    manager = None
    if profile and profile.manager:
        manager = ldap_lookup.fetch_user_details(profile.manager)
    data['hr_managers'] = [
        x.user for x in (UserProfile.objects.filter(
            hr_manager=True).select_related('user'))
    ]

    data['manager'] = manager
    data['all_managers'] = [x for x in data['hr_managers'] if x]
    if manager:
        data['all_managers'].append(manager)
    data['form'] = form
    return render(request, 'dates/notify.html', data)
Beispiel #2
0
def notify(request):
    data = {}
    data['page_title'] = "Notify about new PTO"
    if request.method == 'POST':
        form = forms.AddForm(request.user, data=request.POST)
        if form.is_valid():
            start = form.cleaned_data['start']
            end = form.cleaned_data['end']
            details = form.cleaned_data['details'].strip()
            notify = form.cleaned_data['notify']
            entry = Entry.objects.create(
              user=request.user,
              start=start,
              end=end,
              details=details,
            )
            clean_unfinished_entries(entry)

            messages.info(request, 'Entry added, now specify hours')
            url = reverse('dates.hours', args=[entry.pk])
            request.session['notify_extra'] = notify
            return redirect(url)
    else:
        initial = {}
        if request.GET.get('start'):
            try:
                initial['start'] = parse_datetime(request.GET['start'])
            except DatetimeParseError:
                pass
        if request.GET.get('end'):
            try:
                initial['end'] = parse_datetime(request.GET['end'])
            except DatetimeParseError:
                pass
        form = forms.AddForm(request.user, initial=initial)

    profile = request.user.get_profile()
    manager = None
    if profile and profile.manager:
        manager = ldap_lookup.fetch_user_details(profile.manager)
    data['hr_managers'] = [x.user for x in
                           (UserProfile.objects
                            .filter(hr_manager=True)
                            .select_related('user'))]

    data['manager'] = manager
    data['all_managers'] = [x for x in data['hr_managers'] if x]
    if manager:
        data['all_managers'].append(manager)
    data['form'] = form
    return render(request, 'dates/notify.html', data)
Beispiel #3
0
def send_email_notification(entry, extra_users, is_edit=False):
    email_addresses = []
    for profile in (UserProfile.objects
                     .filter(hr_manager=True,
                             user__email__isnull=False)):
        email_addresses.append(profile.user.email)

    profile = entry.user.get_profile()
    if profile and profile.manager:
        manager = ldap_lookup.fetch_user_details(profile.manager)
        if manager.get('mail'):
            email_addresses.append(manager['mail'])

    if extra_users:
        email_addresses.extend(extra_users)
    email_addresses = list(set(email_addresses))  # get rid of dupes
    if not email_addresses:
        email_addresses = [settings.FALLBACK_TO_ADDRESS]
    if is_edit:
        subject = settings.EMAIL_SUBJECT_EDIT
    else:
        subject = settings.EMAIL_SUBJECT
    subject = subject % dict(
      first_name=entry.user.first_name,
      last_name=entry.user.last_name,
      username=entry.user.username,
      email=entry.user.email,
    )

    message = template = loader.get_template('dates/notification.txt')
    context = {
      'entry': entry,
      'user': entry.user,
      'is_edit': is_edit,
      'settings': settings,
      'start_date': entry.start.strftime(settings.DEFAULT_DATE_FORMAT),
    }
    body = template.render(Context(context)).strip()
    connection = get_connection()
    message = EmailMessage(
      subject=subject,
      body=body,
      from_email=entry.user.email,
      to=email_addresses,
      cc=entry.user.email and [entry.user.email] or None,
      connection=connection
    )

    success = message.send()
    return success, email_addresses
Beispiel #4
0
def send_email_notification(entry, extra_users, is_edit=False):
    email_addresses = []
    for profile in (UserProfile.objects.filter(hr_manager=True,
                                               user__email__isnull=False)):
        email_addresses.append(profile.user.email)

    profile = entry.user.get_profile()
    if profile and profile.manager:
        manager = ldap_lookup.fetch_user_details(profile.manager)
        if manager.get('mail'):
            email_addresses.append(manager['mail'])

    if extra_users:
        email_addresses.extend(extra_users)
    email_addresses = list(set(email_addresses))  # get rid of dupes
    if not email_addresses:
        email_addresses = [settings.FALLBACK_TO_ADDRESS]
    if is_edit:
        subject = settings.EMAIL_SUBJECT_EDIT
    else:
        subject = settings.EMAIL_SUBJECT
    subject = subject % dict(
        first_name=entry.user.first_name,
        last_name=entry.user.last_name,
        username=entry.user.username,
        email=entry.user.email,
    )

    message = template = loader.get_template('dates/notification.txt')
    context = {
        'entry': entry,
        'user': entry.user,
        'is_edit': is_edit,
        'settings': settings,
        'start_date': entry.start.strftime(settings.DEFAULT_DATE_FORMAT),
    }
    body = template.render(Context(context)).strip()
    connection = get_connection()
    message = EmailMessage(subject=subject,
                           body=body,
                           from_email=entry.user.email,
                           to=email_addresses,
                           cc=entry.user.email and [entry.user.email] or None,
                           connection=connection)

    success = message.send()
    return success, email_addresses
Beispiel #5
0
def emails_sent(request, pk):
    data = {}
    entry = get_object_or_404(Entry, pk=pk)
    if entry.user != request.user:
        if not (request.user.is_staff or request.user.is_superuser):
            return http.HttpResponseForbidden('insufficient access')

    emails = request.REQUEST.getlist('e')
    if isinstance(emails, basestring):
        emails = [emails]
    data['emails'] = emails
    data['emailed_users'] = []
    for email in emails:
        record = ldap_lookup.fetch_user_details(email)
        if record:
            data['emailed_users'].append(record)
        else:
            data['emailed_users'].append(email)
    show_fireworks = not request.COOKIES.get('no_fw', False)
    data['show_fireworks'] = show_fireworks
    return render(request, 'dates/emails_sent.html', data)
Beispiel #6
0
def emails_sent(request, pk):
    data = {}
    entry = get_object_or_404(Entry, pk=pk)
    if entry.user != request.user:
        if not (request.user.is_staff or request.user.is_superuser):
            return http.HttpResponseForbidden('insufficient access')

    emails = request.REQUEST.getlist('e')
    if isinstance(emails, basestring):
        emails = [emails]
    data['emails'] = emails
    data['emailed_users'] = []
    for email in emails:
        record = ldap_lookup.fetch_user_details(email)
        if record:
            data['emailed_users'].append(record)
        else:
            data['emailed_users'].append(email)
    show_fireworks = not request.COOKIES.get('no_fw', False)
    data['show_fireworks'] = show_fireworks
    return render(request, 'dates/emails_sent.html', data)