def create(cls, *, email, report, inviter, message, request): """ Create and send an invitation to the person with the given email address Returns True if the invite was sent, otherwise False (meaning the user has already been invited before) """ try: user = User.objects.get(email__iexact=email) except User.DoesNotExist: user = User(email=email, is_active=False) user.save() if Invite.objects.filter(user=user, report=report).exists(): return False invite = Invite(user=user, created_by=inviter, report=report) send_mail( "Invasive Species Hotline Submission Review Request", render_to_string("reports/_invite_expert.txt", { "inviter": inviter, "message": message, "url": user.get_authentication_url(request, next=reverse("reports-detail", args=[report.pk])) }), "*****@*****.**", [email] ) invite.save() return True
def create(cls, *, email, report, inviter, message, request): """ Create and send an invitation to the person with the given email address Returns True if the invite was sent, otherwise False (meaning the user has already been invited before) """ try: user = User.objects.get(email__iexact=email) except User.DoesNotExist: user = User(email=email, is_active=False) user.save() if Invite.objects.filter(user=user, report=report).exists(): return False invite = Invite(user=user, created_by=inviter, report=report) send_mail( "Invasive Species Hotline Submission Review Request", render_to_string( "reports/_invite_expert.txt", { "inviter": inviter, "message": message, "url": user.get_authentication_url(request, next=reverse("reports-detail", args=[report.pk])) }), "*****@*****.**", [email]) invite.save() return True
def setUp(self): super(SpeciesFormsTest, self).setUp() make(Species, name="stuff") self.user = User(first_name="foo", last_name="bar", email="*****@*****.**", is_staff=True) self.user.set_password("foobar") self.user.save() self.client.login(username="******", password="******")
def save(self, *args, request, **kwargs): # first thing we need to do is create or find the right User object try: user = User.objects.get(email__iexact=self.cleaned_data['email']) except User.DoesNotExist: user = User(email=self.cleaned_data['email'], first_name=self.cleaned_data['first_name'], last_name=self.cleaned_data['last_name'], prefix=self.cleaned_data.get('prefix', ""), suffix=self.cleaned_data.get('suffix', ""), is_active=False) user.save() self.instance.created_by = user self.instance.county = County.objects.filter( the_geom__intersects=self.instance.point).first() super().save(*args, **kwargs) # if the submitter left a question, add it as a comment if self.cleaned_data.get("questions"): c = Comment(report=self.instance, created_by=user, body=self.cleaned_data['questions'], visibility=Comment.PROTECTED) c.save() send_mail( "OregonInvasivesHotline.org - Thank you for your submission", render_to_string( "reports/_submission.txt", { "user": user, "url": user.get_authentication_url(request, next=reverse( "reports-detail", args=[self.instance.pk])) }), "*****@*****.**", [user.email]) UserNotificationQuery.notify(self.instance, request) return self.instance
class SpeciesFormsTest(TestCase, elasticmodels.ESTestCase): """ Some unit tests to ensure forms in the species app work as they should. """ def setUp(self): super(SpeciesFormsTest, self).setUp() make(Species, name="stuff") self.user = User(first_name="foo", last_name="bar", email="*****@*****.**", is_staff=True) self.user.set_password("foobar") self.user.save() self.client.login(username="******", password="******") def test_get_initial_queryset(self): form = SpeciesSearchForm(user=self.user, data={}) queryset = form.get_queryset() self.assertEqual(len(queryset), Species.objects.count()) def test_valid_form(self): form = SpeciesSearchForm({'querystring': 'other'}, user=self.user) self.assertTrue(form.is_valid()) def test_form_with_empty_querystring_returns_everything(self): form = SpeciesSearchForm({'querystring': ''}, user=self.user) self.assertTrue(form.is_valid()) results = list(form.search().execute()) self.assertEqual(len(results), Species.objects.count()) def test_search_returns_correct_object(self): # test object name = "other" make(Species, name=name) # set it all up form = SpeciesSearchForm({'querystring': 'other'}, user=self.user) # make sure the form is valid self.assertTrue(form.is_valid()) results = form.search().execute()[0] # Check to see that the id of the test object "other" # is in the list of ids returned by the search function self.assertEqual(results['name'], name)
def save(self, *args, request, **kwargs): # first thing we need to do is create or find the right User object try: user = User.objects.get(email__iexact=self.cleaned_data['email']) except User.DoesNotExist: user = User( email=self.cleaned_data['email'], first_name=self.cleaned_data['first_name'], last_name=self.cleaned_data['last_name'], prefix=self.cleaned_data.get('prefix', ""), suffix=self.cleaned_data.get('suffix', ""), phone=self.cleaned_data.get('phone', ""), has_completed_ofpd=self.cleaned_data.get("has_completed_ofpd"), is_active=False ) user.save() self.instance.created_by = user self.instance.county = County.objects.filter(the_geom__intersects=self.instance.point).first() super().save(*args, **kwargs) # if the submitter left a question, add it as a comment if self.cleaned_data.get("questions"): c = Comment(report=self.instance, created_by=user, body=self.cleaned_data['questions'], visibility=Comment.PROTECTED) c.save() send_mail( "OregonInvasivesHotline.org - Thank you for your submission", render_to_string("reports/_submission.txt", { "user": user, "url": user.get_authentication_url(request, next=reverse("reports-detail", args=[self.instance.pk])) }), "*****@*****.**", [user.email] ) UserNotificationQuery.notify(self.instance, request) return self.instance
def save(self, *args, request, **kwargs): is_new = self.instance.pk is None to_return = super().save(*args, **kwargs) if is_new: # when a new comment is made, send out an email to the relevant # people # any managers or staff who commented on this should get notified to_notify = set( Comment.objects.filter(report=self.instance.report, created_by__is_active=True).values_list( "created_by__email", flat=True)) # whoever claimed the report should get notified if self.instance.report.claimed_by: to_notify.add(self.instance.report.claimed_by.email) # all invited experts should be notified to_notify |= set( Invite.objects.filter(report=self.instance.report).values_list( "user__email", flat=True)) # notify the person who submitted the report if it is PUBLIC or PROTECTED if self.instance.visibility in [Comment.PROTECTED, Comment.PUBLIC]: to_notify.add(self.instance.report.created_by.email) # don't notify yourself to_notify.discard(self.instance.created_by.email) letters = [] for email in to_notify: letters.append( ("OregonInvasivesHotline.org - New Comment on Report", render_to_string( "reports/_new_comment.txt", { "user": self.instance.created_by, "body": self.instance.body, "url": User(email=email).get_authentication_url( request, next=self.instance.get_absolute_url()) }), "*****@*****.**", [email])) send_mass_mail(letters) return to_return
species.save() with suspended_updates(): # create users user_id_to_user_id = {} report_submitter_user_id = {} key_to_user_id = {} old.execute(""" SELECT cardable_id, users.id AS user_id, affiliations, enabled, vcards.id, n_family, n_given, n_prefix, n_suffix, cardable_type, email FROM vcards LEFT JOIN users ON vcards.cardable_id = users.id AND cardable_type = 'User' ORDER BY cardable_type """) for row in dictfetchall(old): user = User.objects.filter(email=row['email']).first() if not user: user = User(email=row['email'], is_active=False) user.email = row['email'] user.first_name = row['n_given'] or "" user.last_name = row['n_family'] or "" user.prefix = row['n_prefix'] or "" user.suffix = row['n_suffix'] or "" user.is_active = user.is_active or bool(row['enabled']) user.affiliations = user.affiliations or (row['affiliations'] or "") user.save() user_id_to_user_id[row['user_id']] = user.pk if row['cardable_type'] == "Submitter": report_submitter_user_id[row['cardable_id']] = user.pk key_to_user_id[(row['cardable_type'], row['cardable_id'])] = user.pk