def create_profile(sender, instance, created, raw, using, **kwargs): if created: # Set the username a simpler username. username = f"{instance.pk}" # Fix uid clashes. if Profile.objects.filter(uid=username): username = util.get_uuid(8) logger.info( f"username clash for pk={instance.pk} new uid={username}") # Make sure staff users are also moderators. role = (Profile.MANAGER if (instance.is_staff or instance.is_superuser) else Profile.READER) # Create a user profile associate with the user. Profile.objects.create(user=instance, uid=username, name=instance.first_name, role=role) try: # Minimize failures on signup. # Send the welcome message. user_ids = [instance.pk] tasks.create_messages(user_ids=user_ids, template="messages/welcome.md") except Exception as exc: logger.error(f"{exc}") instance.profile.add_watched()
def notify_followers(subs, author, extra_context={}): """ Generate notification to users subscribed to a post, excluding author, a message/email. """ from biostar.forum.models import Subscription # Template used to send local messages local_template = "messages/subscription_message.md" # Template used to send emails with email_template = "messages/subscription_email.html" # Does not have subscriptions. if not subs: return # Select users that should be notified. users = [sub.user for sub in subs] # Every subscribed user gets local messages with any subscription type. create_messages(template=local_template, extra_context=extra_context, rec_list=users, sender=author) # Select users with email subscriptions. email_subs = subs.filter(type=Subscription.EMAIL_MESSAGE) # No email subscriptions if not email_subs: return recipient_list = [sub.user.email for sub in email_subs] send_email(template_name=email_template, extra_context=extra_context, recipient_list=recipient_list)
def create_profile(sender, instance, created, raw, using, **kwargs): if created: # Set the username a simpler username. username = f"{instance.pk}" # Update the user with a simpler username. User.objects.filter(pk=instance.pk).update(username=username) # Make sure staff users are also moderators. role = (Profile.MANAGER if (instance.is_staff or instance.is_superuser) else Profile.READER) # Create a user profile associate with the user. Profile.objects.using(using).create(user=instance, uid=username, name=instance.first_name, role=role) # Send the welcome message. user_ids = [instance.pk] tasks.create_messages(user_ids=user_ids, template="messages/welcome.md") # Recompute watched tags instance.profile.add_watched()
def spam_check(uid): from biostar.forum.models import Post, Log from biostar.accounts.models import User, Profile post = Post.objects.filter(uid=uid).first() author = post.author # Automated spam disabled in for trusted user if author.profile.trusted or author.profile.score > 50: return # Classify spam only if we have not done it yet. if post.spam != Post.DEFAULT: return try: from biostar.utils import spamlib if not os.path.isfile(settings.SPAM_MODEL): spamlib.build_model(fname=settings.SPAM_DATA, model=settings.SPAM_MODEL) # Classify the content. flag = spamlib.classify_content(post.content, model=settings.SPAM_MODEL) # Another process may have already classified it as spam. check = Post.objects.filter(uid=post.uid).first() if check and check.spam == Post.SPAM: return ## Links in title usually mean spam. spam_words = ["http://", "https://"] for word in spam_words: flag = flag or (word in post.title) if flag: Post.objects.filter(uid=post.uid).update(spam=Post.SPAM, status=Post.CLOSED) # Get the first admin. user = User.objects.filter(is_superuser=True).order_by("pk").first() create_messages(template="messages/spam-detected.md", extra_context=dict(post=post), user_ids=[post.author.id]) spam_count = Post.objects.filter(spam=Post.SPAM, author=author).count() db_logger(user=user, action=Log.CLASSIFY, text=f"classified the post as spam", post=post) if spam_count > 1 and low_trust(post.author): # Suspend the user Profile.objects.filter(user=author).update(state=Profile.SUSPENDED) db_logger(user=user, action=Log.MODERATE, text=f"suspended the author of", post=post) except Exception as exc: print(exc) logger.error(exc) return False
def notify_followers(sub_ids, author_id, uid, extra_context={}): """ Generate notification to users subscribed to a post, excluding author, a message/email. """ from biostar.forum.models import Subscription from biostar.accounts.models import Profile, User from biostar.forum.models import Post from django.conf import settings # Template used to send local messages local_template = "messages/subscription_message.md" # Template used to send emails with email_template = "messages/subscription_email.html" # Does not have subscriptions. if not sub_ids: return post = Post.objects.filter(uid=uid).first() author = User.objects.filter(id=author_id).first() subs = Subscription.objects.filter(uid__in=sub_ids) users = [sub.user for sub in subs] user_ids = [u.pk for u in users] # Update template context with post extra_context.update(dict(post=post)) # Every subscribed user gets local messages with any subscription type. create_messages(template=local_template, extra_context=extra_context, user_ids=user_ids, sender=author) # Select users with email subscriptions. # Exclude mailing list users to avoid duplicate emails. email_subs = subs.filter(type=Subscription.EMAIL_MESSAGE) email_subs = email_subs.exclude( user__profile__digest_prefs=Profile.ALL_MESSAGES) # No email subscriptions if not email_subs: return recipient_list = [sub.user.email for sub in email_subs] from_email = settings.DEFAULT_NOREPLY_EMAIL send_email(template_name=email_template, extra_context=extra_context, name=author.profile.name, from_email=from_email, recipient_list=recipient_list, mass=True)
def create_profile(sender, instance, created, raw, using, **kwargs): if created: # Set the username to a simpler form. username = f"user-{instance.pk}" User.objects.filter(pk=instance.pk).update(username=username) # Make sure staff users are also moderators. role = Profile.MANAGER if instance.is_staff else Profile.READER Profile.objects.using(using).create(user=instance, uid=username, name=instance.first_name, role=role) tasks.create_messages(rec_list=[instance], template="messages/welcome.md")
def notify_followers(subs, author, extra_context={}): """ Generate notification to users subscribed to a post, excluding author, a message/email. """ from biostar.forum.models import Subscription from biostar.accounts.models import Profile from django.conf import settings message('Notifying users') # Template used to send local messages local_template = "messages/subscription_message.md" # Template used to send emails with email_template = "messages/subscription_email.html" # Does not have subscriptions. if not subs: return users = [sub.user for sub in subs] # Every subscribed user gets local messages with any subscription type. create_messages(template=local_template, extra_context=extra_context, rec_list=users, sender=author) # Select users with email subscriptions. # Exclude mailing list users to avoid duplicate emails. email_subs = subs.filter(type=Subscription.EMAIL_MESSAGE) email_subs = email_subs.exclude( user__profile__digest_prefs=Profile.ALL_MESSAGES) # No email subscriptions if not email_subs: return recipient_list = [sub.user.email for sub in email_subs] from_email = settings.DEFAULT_NOREPLY_EMAIL send_email(template_name=email_template, extra_context=extra_context, name=author.profile.name, from_email=from_email, recipient_list=recipient_list, mass=True)
def create_profile(sender, instance, created, raw, using, **kwargs): if created: # Set the username to a simpler form. username = f"{instance.first_name}-{instance.pk}" if instance.first_name else f'user-{instance.pk}' username = slugify(username) if User.objects.filter(username=username).exclude(id=instance.pk).exists(): username = util.get_uuid(6) User.objects.filter(pk=instance.pk).update(username=username) # Make sure staff users are also moderators. role = Profile.MANAGER if (instance.is_staff or instance.is_superuser) else Profile.READER Profile.objects.using(using).create(user=instance, uid=username, name=instance.first_name, role=role) user_ids = [instance.pk] tasks.create_messages(user_ids=user_ids, template="messages/welcome.md") # Recompute watched tags instance.profile.add_watched()
def notify_followers(post, author): """ Send subscribed users, excluding author, a message/email. """ # Template used to send local messages local_template = "messages/subscription_message.html" # Template used to send emails with email_template = "messages/subscription_email.html" context = dict(post=post) # Everyone subscribed gets a local message. subs = Subscription.objects.filter(post=post.root).exclude( type=models.Profile.NO_MESSAGES) # Send local messages users = set(sub.user for sub in subs if sub.user != author) create_messages(template=local_template, extra_context=context, rec_list=users, sender=author) # Send emails to users that specified so subs = subs.filter(type=models.Profile.EMAIL_MESSAGE) emails = [ sub.user.email for sub in subs if (sub.user != author and sub.type == models.Profile.EMAIL_MESSAGE) ] from_email = settings.ADMIN_EMAIL send_email( template_name=email_template, extra_context=context, subject="Subscription", email_list=emails, from_email=from_email, send=True, )