def send_invite(group, email, invite, resend=False): invite = invite.replace('.', '(dot)') if invite not in group.invites or resend: field = 'invites.%s' % (invite,) Group.atomic_update(group.name, {'$set': {field: ''}}, clause={field: None}) group.invites[invite] = '' group_hash = hash_group_invite(group, invite) link = 'predictionbazaar.com/#invite+%s+%s+%s' % ( encodeURIComponent(group.name), encodeURIComponent(invite), encodeURIComponent(group_hash), ) text = ( '%s has invited you to join "%s" at the ' 'reputation-based prediction market, predictionbazaar.com.' '\n\n' 'View or accept the invite here:\n%s' % ( group.owner, group.name, link ) ) html = ( '%s has invited you to join "%s" at the ' 'reputation-based prediction market, predictionbazaar.com.' '<br><br>' 'Click to <a href="%s">view or accept the invite</a>.' % ( group.owner, group.name, link ) ) # This is a hack to send plaintext emails. Gmail might be filtering out HTML emails. html = None send_mail_async(email, 'predictionbazaar.com - group invitation', text, html)
def forgot_password(self, retrieval=None): users = User.find({'$or': [{'name_': retrieval}, {'email': retrieval}]}) if not users: return 'No users with that username or email were found.' for user in users: password_reset = PasswordReset({ 'token': str(randint(0, MAX_UID)) + str(randint(0, MAX_UID)), 'name': user.name, 'state': 'created', 'time': now(), }) if not password_reset.save(): return 'There was an error sending the email. Try again.' link = 'predictionbazaar.com/#password_reset+%s+%s' % ( encodeURIComponent(password_reset.name), encodeURIComponent(password_reset.token), ) text = ( 'Someone filled out a password reset form for you at predictionbazaar.com.\n\n' 'Use this link to reset it:\n%s' % (link,) ) html = ( 'Someone filled out a password reset form for you at predictionbazaar.com.<br><br>' 'Click to <a href="%s">reset your password.</a>' % (link,) ) send_mail_async(user.email, 'predictionbazaar.com - password reset', text, html) return 'success'
def post(self, request): serializer_class = self.serializer_class(data=request.data) IGNORE_CAPTCHA = config.DISABLE_SIGNUP_CAPTCHA if IGNORE_CAPTCHA: credentials_valid = serializer_class.is_valid( raise_exception=False) if not credentials_valid: if len(serializer_class.errors ) == 1 and 'captcha' in serializer_class.errors: # ignoring captcha pass else: serializer_class.is_valid(raise_exception=True) else: serializer_class.is_valid(raise_exception=True) email = serializer_class.data.get("email") user, _ = User.objects.get_or_create(email=email, is_active=True) OneTimePassword.objects.filter(user=user, is_active=True).update(is_active=False) password = OneTimePassword.objects.create(user=user) Token.objects.get_or_create(user=user) subject = '%s - One-Time password' % settings.EMAIL_SUBJECT_PREFIX body = password.one_time_password send_mail_async( subject, body, settings.DEFAULT_FROM_EMAIL, [email], [settings.DEFAULT_FROM_EMAIL], ) return Response(serializer_class.data)
def comment_post_save(sender, instance, created, *args, **kwargs): # Utils and constants signer = Signer() protocol = 'https' server = next(iter(settings.ALLOWED_HOSTS or []), None) if server == '*': protocol = 'http' server = '0.0.0.0:8000' # client_server = server[4:] if server.startswith('.inf') else server # Broadcast over web-sockets ws_send_comment_changed(instance, created) # Send e-mail notification subscribers = {instance.topic.owner.pk}.union( set( Comment.objects.filter(topic=instance.topic).values_list( 'owner_id', flat=True).distinct())) unsubscribed = {instance.owner.pk}.union( set(instance.topic.unsubscribed.all().values_list('pk', flat=True))) recipients = User.objects.filter(pk__in=subscribers - unsubscribed) subject = '{} - {}'.format(settings.EMAIL_SUBJECT_PREFIX, instance.topic.title[5:]) for recipient in recipients: body = """Comment by {author}:<br> <br> {body}<br> <br> To reply, visit: {protocol}://{client}/#/{lang}/@/topic/{topic_id}/comment/{comment_id}<br> <br> --<br> To unsubscribe from this topic, visit:<br> https://{server}/unsubscribe/{topic_id}?sign={signed_email}<br>""".format( protocol=protocol, body=instance.text[5:], server=server, client=settings.CLIENT_DOMAIN, lang=instance.topic.title[2:4], topic_id=instance.topic.pk, comment_id=instance.pk, author=instance.owner.username, signed_email=signer.sign(recipient.email)) send_mail_async( subject, body, settings.DEFAULT_FROM_EMAIL, [recipient.email], [settings.DEFAULT_FROM_EMAIL], ) # Subscribe the commenter (instance.owner) to the (instance.topic): instance.topic.unsubscribed.remove(instance.owner) # (if previously was unsubscribed) # update comment count if created: instance.topic.update_comment_count() # Save or update its copy to MongoDB, if it's defined data = make_data(instance) update_syncdb_async('comments', data)