Skip to content

Commit

Permalink
added cascade to a few fields, reworked logging a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
ialbert committed Mar 25, 2021
1 parent e9ff0fe commit 783a99b
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 23 deletions.
51 changes: 38 additions & 13 deletions biostar/forum/auth.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import hashlib
import hashlib
import logging
import urllib.parse as urlparse

from django.contrib import messages
from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.db import transaction
from django.db.models import F, Q
from django.template import loader
from django.utils.safestring import mark_safe
from django.core.cache import cache

from biostar.accounts.const import MESSAGE_COUNT
from biostar.accounts.models import Message
Expand Down Expand Up @@ -470,6 +469,34 @@ def bump(request, post, **kwargs):
return url


def change_user_state(mod, target, state):
"""
Changes user state.
"""

# Only moderators may change user states.
if not mod.profile.is_moderator:
logger.error(f"{mod} is not a moderator")
return

# Cannot moderate self.
if mod == target:
logger.error(f"{mod} cannot moderate themselves")
return

# The target may not be a moderator.
if target.profile.is_moderator:
logger.info(f"{mod} cannot alter state on a moderator {target}")
return

# Set the new state.
target.profile.state = state
target.save()

# Generate the logging message.
msg = f"changed user state to {target.profile.get_state_display()}"
db_logger(user=mod, action=Log.MODERATE, target=target, text=msg, post=None)

def toggle_spam(request, post, **kwargs):
"""
Toggles spam status on post based on a status
Expand Down Expand Up @@ -497,10 +524,11 @@ def toggle_spam(request, post, **kwargs):
# Refetch up to date state of the post.
post = Post.objects.filter(id=post.id).get()

# Set the state for the user.
if post.is_spam and not post.author.profile.is_moderator:
post.author.profile.state = Profile.SUSPENDED if post.is_spam else Profile.NEW
post.author.profile.save()
# Set the state for the user (only non moderators are affected)
state = Profile.SUSPENDED if post.is_spam else Profile.NEW

# Apply the user change.
change_user_state(mod=user, target=post.author, state=state)

# Generate logging messages.
if post.is_spam:
Expand All @@ -515,8 +543,7 @@ def toggle_spam(request, post, **kwargs):
messages.success(request, text)

# Submit the log into the database.
db_logger(user=user, action=Log.MODERATE, text=text, post=post)

db_logger(user=user, action=Log.MODERATE, target=post.author, text=text, post=post)

url = post.get_absolute_url()

Expand Down Expand Up @@ -613,11 +640,9 @@ def moderate(request, post, action, comment=""):
return url


def db_logger(user=None, action=Log.MODERATE, text='', ipaddr=None, post=None):
def db_logger(user=None, action=Log.MODERATE, text='', target=None, ipaddr=None, post=None):
"""
Creates a database log.
"""
Log.objects.create(user=user, action=action, text=text, ipaddr=ipaddr, post=post)

logger.info(f"post={user.email}, post={post}, {text}")
return
Log.objects.create(user=user, action=action, text=text, target=target, ipaddr=ipaddr, post=post)
logger.info(f"user={user.email} {text} ")
47 changes: 47 additions & 0 deletions biostar/forum/migrations/0017_expanded_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Generated by Django 3.1 on 2021-03-25 14:18

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('sites', '0002_alter_domain_unique'),
('forum', '0016_cascading'),
]

operations = [
migrations.AddField(
model_name='log',
name='target',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='target', to=settings.AUTH_USER_MODEL),
),
migrations.AlterField(
model_name='award',
name='post',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='forum.post'),
),
migrations.AlterField(
model_name='log',
name='post',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='forum.post'),
),
migrations.AlterField(
model_name='log',
name='text',
field=models.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name='log',
name='user',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
migrations.AlterField(
model_name='post',
name='site',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='sites.site'),
),
]
17 changes: 10 additions & 7 deletions biostar/forum/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class Post(models.Model):
tags = TaggableManager()

# What site does the post belong to.
site = models.ForeignKey(Site, null=True, on_delete=models.SET_NULL)
site = models.ForeignKey(Site, null=True, on_delete=models.CASCADE)

# Unique id for the post.
uid = models.CharField(max_length=32, unique=True, db_index=True)
Expand Down Expand Up @@ -531,7 +531,7 @@ class Award(models.Model):
'''
badge = models.ForeignKey(Badge, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, null=True, on_delete=models.SET_NULL)
post = models.ForeignKey(Post, null=True, on_delete=models.CASCADE)
date = models.DateTimeField()

# context = models.CharField(max_length=1000, default='')
Expand Down Expand Up @@ -561,11 +561,14 @@ class Log(models.Model):
(DEFAULT, "Default")
]

# User that is logged.
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
# User that performed the action.
user = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)

# Post related information goes here.
post = models.ForeignKey(Post, on_delete=models.SET_NULL, null=True, blank=True)
# A potential target user (it may be null)
target = models.ForeignKey(User, related_name="target", null=True, blank=True, on_delete=models.CASCADE)

# Post related information goes here (it may be null).
post = models.ForeignKey(Post, null=True, blank=True, on_delete=models.CASCADE)

# The IP address associated with the log.
ipaddr = models.GenericIPAddressField(null=True, blank=True)
Expand All @@ -574,7 +577,7 @@ class Log(models.Model):
action = models.IntegerField(choices=ACTIONS_CHOICES, default=DEFAULT, db_index=True)

# The logging information.
text = models.TextField()
text = models.TextField(null=True, blank=True)

# Date this log was created.
date = models.DateTimeField()
Expand Down
12 changes: 9 additions & 3 deletions biostar/forum/templates/view_logs.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@
<p>
<a href="/info/mods/"><i class="info circle icon"></i> Moderator resources</a>
</p>

<div class="ui middle aligned list">
{% for log in logs %}
<div class="item">
<img class="ui avatar image" src="{% gravatar user=log.user size=80 %}" />
<img class="ui avatar image" src="{% gravatar user=log.user size=80 %}"/>
<div class="content">
<em>{{ log.date|naturaltime }}</em>
<em>{{ log.date|naturaltime }}</em> &bull;
<a href="{{ log.user.profile.get_absolute_url }}">{{ log.user.profile.name }}</a>

{{ log.text }}

{% if log.target %}
&bull; <a href="{{ log.target.profile.get_absolute_url }}">{{ log.target.profile.name }}</a>
{% endif %}

{% if log.post %}
<a href="{{ log.post.get_absolute_url }}">{{ log.post.title }}</a>
&bull; <em><a href="{{ log.post.get_absolute_url }}">{{ log.post.title }}</a></em>
{% endif %}

</div>
</div>
{% endfor %}
Expand Down

0 comments on commit 783a99b

Please sign in to comment.