# Path: user/forms.py
# class ContactForm(forms.Form):
#     subject = forms.CharField(help_text=_(
#         'This will become the subject field of the resulting email.'))
#     content = forms.CharField(
#         widget=forms.Textarea,
#         help_text=_('This will be the content of the email. HTML is not allowed '
#                     'and any html tags will be removed.')
#     )
# 
#     def clean(self):
#         super().clean()
#         self.subject = html_clean.clean_all(self.data['subject'])
#         self.content = html_clean.clean_all(self.data['content'])
# 
# Path: util/error/reporting.py
# def db_error(message):
#     """
#     Report a database error in a view.
#     """
#     return render_to_response('error/db.html', {'error': message})


from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.http import HttpRequest
from django.shortcuts import render, redirect
from django.views.decorators.csrf import csrf_protect
from django.utils.translation import ugettext as _
from user.forms import ContactForm
from util.error.reporting import db_error


CONTACT_FOOTER = """
-------------------
This message has been sent via the Course Mangement System.
Sent by: """

@login_required
@csrf_protect
def contact_form(request: HttpRequest, user_id: int):
    try:
        user = User.objects.get(id=user_id)
    except User.DoesNotExist:
        return db_error(_('Requested user does not exist.'))
    assert isinstance(user, User)

    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            content = form.content + CONTACT_FOOTER + request.user.email
            user.email_user(
                subject="[CM contact form] " + form.subject,
                message=content
            )
            return redirect('index')
    else:

# Path: user/forms.py
# class ContactForm(forms.Form):
#     subject = forms.CharField(help_text=_(
#         'This will become the subject field of the resulting email.'))
#     content = forms.CharField(
#         widget=forms.Textarea,
#         help_text=_('This will be the content of the email. HTML is not allowed '
#                     'and any html tags will be removed.')
#     )
# 
#     def clean(self):
#         super().clean()
#         self.subject = html_clean.clean_all(self.data['subject'])
#         self.content = html_clean.clean_all(self.data['content'])
# 
# Path: util/error/reporting.py
# def db_error(message):
#     """
#     Report a database error in a view.
#     """
#     return render_to_response('error/db.html', {'error': message})


# Path: user/views/contact.py
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.http import HttpRequest
from django.shortcuts import render, redirect
from django.views.decorators.csrf import csrf_protect
from django.utils.translation import ugettext as _
from user.forms import ContactForm
from util.error.reporting import db_error



CONTACT_FOOTER = """
-------------------
This message has been sent via the Course Mangement System.
Sent by: """

@login_required
@csrf_protect
def contact_form(request: HttpRequest, user_id: int):
    try:
        user = User.objects.get(id=user_id)
    except User.DoesNotExist:
        return db_error(_('Requested user does not exist.'))
    assert isinstance(user, User)

    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            content = form.content + CONTACT_FOOTER + request.user.email
            user.email_user(
                subject="[CM contact form] " + form.subject,
                message=content
            )
            return redirect('index')
    else:

        form = ContactForm()