def report_abuse(request, object_id): app = get_app_or_error(request.user, object_id) if request.method == 'POST': form = ReportAbuseForm(request.POST) if form.is_valid(): abuse_description = form.cleaned_data['abuse_description'] email_from = settings.DEFAULT_FROM_EMAIL email_to = get_contact_admin_email() current_site = Site.objects.get_current() message = """ User %s reported an abuse in application %s (http://%s%s) described as: %s """ % (request.user.email.strip(), app.name, current_site.domain, app.get_absolute_url(), abuse_description) try: send_mail( '[WallManager] Application ' + app.name + ' received an abuse report.', message, email_from, [email_to]) request.user.message_set.create( message="Application %s reported successfully." % app.name) except: request.user.message_set.create( message= "Failure while sending e-mail message containing the report. Please try again later." ) else: return application_detail(request, object_id, form) return HttpResponseRedirect(reverse('application-list'))
def test_contact(self): """ Sample test for a message to the designated contact administrator """ # Clean email inbox mail.outbox = [] login = self.do_login(user="******") response = self.client.get('/contact/') self.assertEqual(response.status_code, 200) self.assertContains(response, "Contact Admin") self.assertContains(response, "<form", 1) self.assertContains(response, "message") self.assertContains(response, "submit") sample_message = 'I have an application that needs the library xyz. Could you please install it on the system?' post_data = { 'message': sample_message, } response = self.client.post('/contact/', post_data) self.assertEqual(response.status_code, 200) self.assertEqual(len(mail.outbox), 1) self.assertEqual(len(mail.outbox[0].to), 1) self.assertEqual(mail.outbox[0].to[0], get_contact_admin_email()) self.assertEqual(mail.outbox[0].subject, '[WallManager] Message from user %s' % self.zacarias.email) self.assertTrue( sample_message in mail.outbox[0].body)
def check_if_contact_admin(sender, instance, signal, *args, **kwargs): """ Checks if the removed user is the contact admin (and if so, sets the contact admin to null) """ contact_admin_email = get_contact_admin_email() if instance.email == contact_admin_email: try: wallmanager_instance = WallManager.objects.all()[0] wallmanager_instance.contact = "" wallmanager_instance.save() except IndexError: pass
def contact(request): if request.method == 'POST': form = MessageToAdminForm(request.POST) if form.is_valid(): subject = '[WallManager] Message from user %s' % request.user.email message = 'Dear WallManager administrator,\n\n\ The user %s has sent you a message using the website\'s administrator contact form. The message is as follows:\n\n\ %s' % (request.user.email, form.cleaned_data['message']) email_from = settings.DEFAULT_FROM_EMAIL email_to = get_contact_admin_email() try: send_mail(subject, message, email_from, [email_to]) request.user.message_set.create( message= "Your message was sent successfully to the designated contact administrator." ) return application_list(request) except SMTPException, e: request.user.message_set.create( message= "Unable to send your message. Please try again later.")
def check_if_no_longer_staff(sender, instance, signal, *args, **kwargs): """ Checks if modified user is contact admin and removes him/her from contact if no longer marked as staff """ if get_contact_admin_email() == instance.email and instance.is_staff == False: WallManager.objects.all().delete()