Skip to content

Commit

Permalink
fix in issue ialbert#870
Browse files Browse the repository at this point in the history
  • Loading branch information
Natay committed Jul 25, 2021
1 parent 9c8575a commit 2261297
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 15 deletions.
7 changes: 7 additions & 0 deletions biostar/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ def edit_url(self):

return reverse('edit_profile')

@property
def no_messages(self):
"""
User has turned all notifications off
"""
return self.message_prefs == self.NO_MESSAGES

@property
def mailing_list(self):
"""
Expand Down
7 changes: 1 addition & 6 deletions biostar/forum/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ def create_post(author, title, content, request=None, root=None, parent=None, pt


def diff_ratio(text1, text2):

# Do not match on spaces
s = SequenceMatcher(lambda char: re.match(r'\w+', char), text1, text2)
return round(s.ratio(), 5)
Expand Down Expand Up @@ -313,12 +312,8 @@ def create_subscription(post, user, sub_type=None, update=False):
subs = Subscription.objects.filter(post=post.root, user=user)
sub = subs.first()

default = Subscription.TYPE_MAP.get(user.profile.message_prefs,
Subscription.LOCAL_MESSAGE)
default = Subscription.TYPE_MAP.get(user.profile.message_prefs, Subscription.LOCAL_MESSAGE)

empty = sub_type is None
# Get the current sub type from what's given or the existing sub
sub_type = None if empty else sub_type
# No type has been given so default
sub_type = sub_type or default

Expand Down
26 changes: 17 additions & 9 deletions biostar/forum/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from mistune import Renderer, InlineLexer, InlineGrammar
from mistune import escape as escape_text
from bleach.sanitizer import Cleaner
from biostar.forum import auth
from biostar.forum import auth, tasks
from biostar.forum.models import Post, Subscription
from biostar.accounts.models import Profile, User
from bleach.callbacks import nofollow
Expand Down Expand Up @@ -189,15 +189,24 @@ def rewrite_static(link):
return link


def resolve_subtype(user):
"""
Resolve a users subscription type to be LOCAL_MESSAGE or EMAIL_MESSAGE
"""
return Profile.LOCAL_MESSAGE if user.no_messages else user.profile.message_prefs


class BiostarInlineLexer(MonkeyPatch):
grammar_class = BiostarInlineGrammer

def __init__(self, root=None, allow_rewrite=False, *args, **kwargs):
def __init__(self, post=None, allow_rewrite=False, *args, **kwargs):
"""
:param root: Root post that is being pared
:param static_imgs:
"""
self.root = root
self.post = post
# Resolve the root if exists.
self.root = post.parent.root if (post and post.parent) else None
self.allow_rewrite = allow_rewrite

super(BiostarInlineLexer, self).__init__(*args, **kwargs)
Expand Down Expand Up @@ -247,14 +256,16 @@ def output_mention_link(self, m):
profile = reverse("user_profile", kwargs=dict(uid=user.profile.uid))
link = f'<a href="{profile}">{user.profile.name}</a>'
# Subscribe mentioned users to post.
if self.root:
if self.post:
# Create user subscription if it does not already exist.
auth.create_subscription(post=self.root, user=user, update=True)
sub_type = resolve_subtype(user)
auth.create_subscription(post=self.root, user=user, sub_type=sub_type, update=True)
else:
link = m.group(0)

return link


def output_post_link(self, m):
uid = m.group("uid")
link = m.group(0)
Expand Down Expand Up @@ -416,14 +427,11 @@ def parse(text, post=None, clean=True, escape=True, allow_rewrite=False):
eg. images/foo.png -> /static/images/foo.png
"""

# Resolve the root if exists.
root = post.parent.root if (post and post.parent) else None

# Initialize the renderer
renderer = BiostarRenderer(escape=escape)

# Initialize the lexer
inline = BiostarInlineLexer(renderer=renderer, root=root, allow_rewrite=allow_rewrite)
inline = BiostarInlineLexer(renderer=renderer, post=post, allow_rewrite=allow_rewrite)

markdown = mistune.Markdown(hard_wrap=True, renderer=renderer, inline=inline)

Expand Down
2 changes: 2 additions & 0 deletions biostar/forum/templates/messages/ping.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

[{{ post.title}}]({{ post.get_absolute_url }}): {{post.content|truncatewords:180}}
43 changes: 43 additions & 0 deletions biostar/forum/templates/messages/ping_email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{% load accounts_tags %}
{% block subject %}

[Biostar] {{ post.title|truncatechars:60 }}

{% endblock %}

{% block html %}

A user has tagged you on <a href="{{ protocol }}://{{ domain }}{{ http_port }}">Biostar</a>
<p>
User <a href="{{ protocol }}://{{ domain }}{{ http_port }}{{ user.profile.get_absolute_url }}">
{{ post.author.profile.name }}</a> wrote

<a href="{{ protocol }}://{{ domain }}{{ post.get_absolute_url }}">{{ post.title }}</a>:
</p>
<p>
{{ post.html|safe }}
</p>

<hr>
<p class="muted" >
You may visit {{ protocol }}://{{ domain }}{{ post.get_absolute_url }}
</p>

<p>The Biostar Team</p>

{% endblock %}

{% block text %}

A user has tagged you on {{ domain }}.

User {{ user.profile.name }} wrote {{ post.title }}:

{{ post.content }}

You may visit {{ protocol }}://{{ domain }}{{ post.get_absolute_url }}

The Biostar Team


{% endblock %}

0 comments on commit 2261297

Please sign in to comment.