<commit_before># created by <NAME>, <EMAIL>
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import datetime
from smtplib import SMTPException

from django.contrib.auth.models import User
from django import forms
from django.contrib.sites.shortcuts import get_current_site
from django.http import HttpResponse, request
from django.shortcuts import redirect, render
from django.urls import reverse, reverse_lazy
from django.views import View
from django.views.generic import ListView, DetailView, CreateView, UpdateView
from django.core.mail import send_mail
from django.core.mail import EmailMessage

from promotion.forms import JoinApplicationForm
from promotion.models import JoinApplication

from fosswebsite.settings import join_application_mail_list, join_application_reply_to

approve_mail_content = ',\\n\\nWe are exited to inform that you are selected for the interview.'
reject_mail_content = ',\\n\\nWe are sorry to inform that you are not selected for the interview. Please try again ' \
                      'next time.'


class JoinApplicationListView(ListView):
    model = JoinApplication
    queryset = JoinApplication.objects.filter(is_approved=False, is_rejected=False).order_by('-date')

    def get_context_data(self, **kwargs):
        context = super(JoinApplicationListView, self).get_context_data(**kwargs)
        context['count'] = len(context['object_list'])
        status = self.request.GET.get('list', None)
        if status == "approved":
            context['object_list'] = JoinApplication.objects.filter(is_approved=True).order_by('-date')
        if status == "rejected":
            context['object_list'] = JoinApplication.objects.filter(is_rejected=True).order_by('-date')
        return context


class JoinApplicationDetailView(DetailView):
    errors = None
    model = JoinApplication

    def get_context_data(self, **kwargs):
        context = super(JoinApplicationDetailView, self).get_context_data(**kwargs)
        context['approve_mail_subject'] = 'Congrats! FOSS@Amrita membership application'
        context['approve_mail_content'] = 'Hi ' + self.get_object().name + approve_mail_content
        context['reject_mail_subject'] = 'FOSS@Amrita membership application'
        context['reject_mail_content'] = 'Hi ' + self.get_object().name + reject_mail_content
        context['mail_error'] = self.request.GET.get('errors', None)
        return context


class JoinApplicationCreateView(CreateView):
    form_class = JoinApplicationForm
    template_name = 'base/form.html'
    success_url = reverse_lazy('join_success')

    def get_context_data(self, **kwargs):
        context = super(JoinApplicationCreateView, self).get_context_data(**kwargs)
        context['title'] = 'Applications'
        context['heading'] = 'Membership Application'
        return context

    def form_valid(self, form):
        valid_form = super(JoinApplicationCreateView, self).form_valid(form)

        # generate urls
        list_url = ''.join(['http://', get_current_site(self.request).domain, reverse('join_list')])

        # mail data
        subject = 'Application from ' + form.cleaned_data.get('name')
        content = 'A new application was submitted by ' + form.cleaned_data.get('name') + ' at ' + \
                  str(datetime.datetime.now()) + '. \n\nPlease visit ' + list_url + ' for more details.'

        to_address_list = list(User.objects.filter(is_superuser=True).values_list('email', flat=True))
        # sent mail when application is submitted
        send_mail(subject, content, '<EMAIL>', to_address_list, fail_silently=True)

        mail_content = "Hi " + form.cleaned_data.get('name')+ ", \n\n" + \
                       "Great to know that you are interested in being a part of the FOSS club at Amritpauri." + \
                       "We got your application, please complete the " + \
                       "tasks at [1] and complete at least 25 Hackerrank[2] problems or " + \
                       "if you are not familiar with programing complete cs50.tv[3] till week 3 " + \
                       "before 30th of this month.\n\n" + \
                       "Let us know when you are done with the Hackerrank problems, so that we can have a one on " + \
                       "one interview. We won't be testing your skills but would ask about the problems you have " + \
                       "solved. This is to test if you are really interested and we expect you to be honest. " + \
                       "If you have any queries feel free to reply to this mail." + \
                       "\n\n[1] http://foss.amrita.ac.in/foss/#sixth\n[2] https://www.hackerrank.com/" + \
                       "\n[3] http://cs50.tv/2016/fall/\n\nWith regards, \n\nFOSS@Amrita"

        email = EmailMessage(
            'Tasks to complete, FOSS@Amrita',
            mail_content,
            '<EMAIL>',
            [form.cleaned_data.get('name'), join_application_reply_to],
            join_application_mail_list,
            reply_to=join_application_reply_to,
            headers={'Message-ID': 'foss@amrita'},
        )
        email.send()

        return valid_form


class EmailForm(forms.Form):
    mail_id = forms.EmailField()
    mail_subject = forms.CharField()
    mail_content = forms.CharField()


class JoinApplicationUpdateView(UpdateView):
    """<commit_msg>email typo fixes<commit_after># created by <NAME>, <EMAIL>
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import datetime
from smtplib import SMTPException

from django.contrib.auth.models import User
from django import forms
from django.contrib.sites.shortcuts import get_current_site
from django.http import HttpResponse, request
from django.shortcuts import redirect, render
from django.urls import reverse, reverse_lazy
from django.views import View
from django.views.generic import ListView, DetailView, CreateView, UpdateView
from django.core.mail import send_mail
from django.core.mail import EmailMessage

from promotion.forms import JoinApplicationForm
from promotion.models import JoinApplication

from fosswebsite.settings import join_application_mail_list, join_application_reply_to

approve_mail_content = ',\\n\\nWe are excited to inform that you are selected for the interview. Be there at ' + \
                        'ground floor computer lab by 5PM. \n\nIf you have any queries contact : Chirath, 8547801861' \
                        '\n\nThank you, \n\nFOSS@Amrita'

reject_mail_content = ',\\n\\nWe are sorry to inform that you are not selected for the interview. Please try again ' \
                      'next time.'


class JoinApplicationListView(ListView):
    model = JoinApplication
    queryset = JoinApplication.objects.filter(is_approved=False, is_rejected=False).order_by('-date')

    def get_context_data(self, **kwargs):
        context = super(JoinApplicationListView, self).get_context_data(**kwargs)
        context['count'] = len(context['object_list'])
        status = self.request.GET.get('list', None)
        if status == "approved":
            context['object_list'] = JoinApplication.objects.filter(is_approved=True).order_by('-date')
        if status == "rejected":
            context['object_list'] = JoinApplication.objects.filter(is_rejected=True).order_by('-date')
        return context


class JoinApplicationDetailView(DetailView):
    errors = None
    model = JoinApplication

    def get_context_data(self, **kwargs):
        context = super(JoinApplicationDetailView, self).get_context_data(**kwargs)
        context['approve_mail_subject'] = 'Congrats! FOSS@Amrita membership application'
        context['approve_mail_content'] = 'Hi ' + self.get_object().name + approve_mail_content
        context['reject_mail_subject'] = 'FOSS@Amrita membership application'
        context['reject_mail_content'] = 'Hi ' + self.get_object().name + reject_mail_content
        context['mail_error'] = self.request.GET.get('errors', None)
        return context


class JoinApplicationCreateView(CreateView):
    form_class = JoinApplicationForm
    template_name = 'base/form.html'
    success_url = reverse_lazy('join_success')

    def get_context_data(self, **kwargs):
        context = super(JoinApplicationCreateView, self).get_context_data(**kwargs)
        context['title'] = 'Applications'
        context['heading'] = 'Membership Application'
        return context

    def form_valid(self, form):
        valid_form = super(JoinApplicationCreateView, self).form_valid(form)

        # generate urls
        list_url = ''.join(['http://', get_current_site(self.request).domain, reverse('join_list')])

        # mail data
        subject = 'Application from ' + form.cleaned_data.get('name')
        content = 'A new application was submitted by ' + form.cleaned_data.get('name') + ' at ' + \
                  str(datetime.datetime.now()) + '. \n\nPlease visit ' + list_url + ' for more details.'

        to_address_list = list(User.objects.filter(is_superuser=True).values_list('email', flat=True))
        # sent mail when application is submitted
        send_mail(subject, content, '<EMAIL>', to_address_list, fail_silently=True)

        mail_content = "Hi " + form.cleaned_data.get('name')+ ", \n\n" + \
                       "Great to know that you are interested in being a part of the FOSS club at Amritapuri. " + \
                       "We got your application, please complete the " + \
                       "tasks at [1] and complete at least 25 Hackerrank[2] problems or " + \
                       "if you are not familiar with programing complete cs50.tv[3] till week 3 " + \
                       "before " + str(datetime.date.today() + datetime.timedelta(days=20)) + ".\n\n" + \
                       "Let us know when you are done with the Hackerrank problems, so that we can have a one on " + \
                       "one interview. We won't be testing your skills but would ask about the problems you have " + \
                       "solved. This is to test if you are really interested and we expect you to be honest. " + \
                       "If you have any queries feel free to reply to this mail." + \
                       "\n\n[1] http://foss.amrita.ac.in/foss/#sixth\n[2] https://www.hackerrank.com/" + \
                       "\n[3] http://cs50.tv/2016/fall/\n\nWith regards, \n\nFOSS@Amrita"

        email = EmailMessage(
            'Tasks to complete, FOSS@Amrita',
            mail_content,
            '<EMAIL>',
            [form.cleaned_data.get('name'), join_application_reply_to],
            join_application_mail_list,
            reply_to=join_application_reply_to,
            headers={'Message-ID': 'foss@amrita'},
        )
        email.send()

        return valid_form


class EmailForm(forms.Form):
    mail_id = forms.EmailField()
    mail_subject = forms.CharField()
    mail_content = forms.CharField()


class JoinApplicationUpdateView(UpdateView):
    """
{'fraction_non_alphanumeric': 0.10581480131243165, 'fraction_numerical': 0.004648195406489245, 'mean_word_length': 2.8842477876106196, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5587118', 'n_tokens_mistral': 3129, 'n_tokens_neox': 3008, 'n_words': 811}