<commit_before>import simplejson as json

from django.http import Http404, HttpResponse
from django.template.loader import render_to_string
from django.core.mail import mail_admins
from django.contrib.sites.models import Site
from django.conf import settings
from django.core.urlresolvers import reverse

from components.ask_admin.forms import FeedbackForm

def send_feedback(request):
  if request.method == "POST" and request.is_ajax():
    form = FeedbackForm(request.POST)
    if form.is_valid():
      html_message = render_to_string("email/ask_admin.html", {
          "user": request.user,
          "url": form.cleaned_data["url"],
          "question": form.cleaned_data["question"],
      })
      message = render_to_string("email/ask_admin.txt", {
          "user": request.user,
          "url": form.cleaned_data["url"],
          "question": form.cleaned_data["url"],
      })
      
      current_site = Site.objects.get(id=settings.SITE_ID)
      mail_admins("[%s] Message for admins" % current_site.domain,
          message, html_message=html_message)
          
      return HttpResponse(json.dumps({"success": True}), mimetype="application/json")
  
  raise Http404<commit_msg>Making the email function a little more flexible if the user is not sending the email via ajax.<commit_after>import simplejson as json

from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.template.loader import render_to_string
from django.core.mail import mail_admins
from django.contrib.sites.models import Site
from django.conf import settings
from django.core.urlresolvers import reverse

from components.ask_admin.forms import FeedbackForm

def send_feedback(request):
  if request.method == "POST":
    form = FeedbackForm(request.POST)
    if form.is_valid():
      html_message = render_to_string("email/ask_admin.html", {
          "user": request.user,
          "url": form.cleaned_data["url"],
          "question": form.cleaned_data["question"],
      })
      message = render_to_string("email/ask_admin.txt", {
          "user": request.user,
          "url": form.cleaned_data["url"],
          "question": form.cleaned_data["url"],
      })
      
      current_site = Site.objects.get(id=settings.SITE_ID)
      mail_admins("[%s] Message for admins" % current_site.domain,
          message, html_message=html_message)
          
      if request.is_ajax():
        return HttpResponse(json.dumps({"success": True}), mimetype="application/json")
      
      return HttpResponseRedirect(form.cleaned_data["url"])
  
  raise Http404
{'fraction_non_alphanumeric': 0.11029126213592233, 'fraction_numerical': 0.004660194174757282, 'mean_word_length': 3.4878048780487805, 'pattern_counts': {'":': 15, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5375926', 'n_tokens_mistral': 766, 'n_tokens_neox': 743, 'n_words': 169}