Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions osf/management/commands/add_notification_subscription.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# -*- coding: utf-8 -*-
# This is a management command, rather than a migration script, for two primary reasons:
# 1. It makes no changes to database structure (e.g. AlterField), only database content.
# 2. It takes a long time to run and the site doesn't need to be down that long.

from __future__ import unicode_literals
import logging

import django
django.setup()

from django.core.management.base import BaseCommand
from django.db import transaction

from osf.models import OSFUser, NotificationSubscription

from website.notifications.utils import to_subscription_key

from scripts import utils as script_utils

logger = logging.getLogger(__name__)


def add_reviews_notification_setting(notification_type):
active_users = OSFUser.objects.filter(date_confirmed__isnull=False).exclude(date_disabled__isnull=False).exclude(is_active=False).order_by('id')
total_active_users = active_users.count()

logger.info('About to add a global_reviews setting for {} users.'.format(total_active_users))

total_created = 0
for user in active_users.iterator():
user_subscription_id = to_subscription_key(user._id, notification_type)

subscription = NotificationSubscription.load(user_subscription_id)
if not subscription:
logger.info('No {} subscription found for user {}. Subscribing...'.format(notification_type, user._id))
subscription = NotificationSubscription(_id=user_subscription_id, owner=user, event_name=notification_type)
subscription.save() # Need to save in order to access m2m fields
subscription.add_user_to_subscription(user, 'email_transactional')
else:
logger.info('User {} already has a {} subscription'.format(user._id, notification_type))
total_created += 1

logger.info('Added subscriptions for {}/{} users'.format(total_created, total_active_users))


class Command(BaseCommand):
"""
Add subscription to all active users for given notification type.
"""
def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
'--dry',
action='store_true',
dest='dry_run',
help='Run migration and roll back changes to db',
)

parser.add_argument(
'--notification',
type=str,
required=True,
help='Notification type to subscribe users to',
)

def handle(self, *args, **options):
dry_run = options.get('dry_run', False)
if not dry_run:
script_utils.add_file_logger(logger, __file__)
with transaction.atomic():
add_reviews_notification_setting(notification_type=options['notification'])
if dry_run:
raise RuntimeError('Dry run, transaction rolled back.')
20 changes: 20 additions & 0 deletions osf/migrations/0061_add_reviews_notification_subscription.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-10-02 19:38
from __future__ import unicode_literals

from django.db import migrations
from django.core.management import call_command


def add_reviews_notification_subscription(apps, schema_editor):
call_command('add_notification_subscription', '--notification=global_reviews')

class Migration(migrations.Migration):

dependencies = [
('osf', '0060_reviews'),
]

operations = [
migrations.RunPython(add_reviews_notification_subscription),
]
49 changes: 0 additions & 49 deletions osf/migrations/0061_auto_20171002_1438.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def accept_all_published_preprints(apps, schema_editor):
class Migration(migrations.Migration):

dependencies = [
('osf', '0062_auto_20171004_1506'),
('osf', '0061_add_reviews_notification_subscription'),
]

operations = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Migration(migrations.Migration):

dependencies = [
('osf', '0060_add_nodelog_should_hide_nid_index'),
('osf', '0063_accept_preprints'),
('osf', '0062_accept_preprints'),
]

operations = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-10-04 20:06
# Generated by Django 1.11.4 on 2017-10-19 14:18
from __future__ import unicode_literals

from django.db import migrations, models
Expand All @@ -8,13 +8,13 @@
class Migration(migrations.Migration):

dependencies = [
('osf', '0061_auto_20171002_1438'),
('osf', '0063_merge_20171012_1215'),
]

operations = [
migrations.AlterField(
model_name='notificationdigest',
name='message',
field=models.CharField(max_length=10000),
field=models.TextField(),
),
]
31 changes: 0 additions & 31 deletions osf/migrations/0065_auto_20171016_1201.py

This file was deleted.

2 changes: 1 addition & 1 deletion osf/models/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ class NotificationDigest(ObjectIDMixin, BaseModel):
timestamp = NonNaiveDateTimeField()
send_type = models.CharField(max_length=50, db_index=True, validators=[validate_subscription_type, ])
event = models.CharField(max_length=50)
message = models.CharField(max_length=10000)
message = models.TextField()
# TODO: Could this be a m2m with or without an order field?
node_lineage = ArrayField(models.CharField(max_length=5))
19 changes: 12 additions & 7 deletions reviews/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,10 @@ def update_last_transitioned(self, ev):
self.reviewable.date_last_transitioned = now

def save_changes(self, ev):
node = self.reviewable.node
node._has_abandoned_preprint = False
now = self.action.date_created if self.action is not None else timezone.now()
should_publish = self.reviewable.in_public_reviews_state
self.reviewable.node._has_abandoned_preprint = False
if should_publish and not self.reviewable.is_published:
if not (self.reviewable.node.preprint_file and self.reviewable.node.preprint_file.node == self.reviewable.node):
raise ValueError('Preprint node is not a valid preprint; cannot publish.')
Expand All @@ -191,15 +192,14 @@ def save_changes(self, ev):
elif not should_publish and self.reviewable.is_published:
self.reviewable.is_published = False
self.reviewable.save()
self.reviewable.node.save()
node.save()

def resubmission_allowed(self, ev):
return self.reviewable.provider.reviews_workflow == workflow.Workflows.PRE_MODERATION.value

def notify_submit(self, ev):
context = self.get_context()
context['referrer'] = ev.kwargs.get('user')
context['template'] = 'reviews_submission_confirmation'
user = ev.kwargs.get('user')
auth = Auth(user)
self.reviewable.node.add_log(
Expand Down Expand Up @@ -266,12 +266,17 @@ def reviews_notification(self, context):
# Handle email notifications for a new submission.
@reviews_signals.reviews_email_submit.connect
def reviews_submit_notification(self, context):
template = context['template']
event_type = utils.find_subscription_type('global_reviews')
for user_id in context['email_recipients']:
user = OSFUser.load(user_id)
user_subscriptions = get_user_subscriptions(user, event_type)
context['no_future_emails'] = user_subscriptions['none']
context['is_creator'] = user == context.get('reviewable').node.creator
email = mails.Mail(template, subject='Confirmation of your submission to {provider}'.format(provider=context.get('reviewable').provider.name))
mails.send_mail(user.username, email, mimetype='html', user=user, **context)
context['is_creator'] = user == context['reviewable'].node.creator
context['provider_name'] = context['reviewable'].provider.name
mails.send_mail(
user.username,
mails.REVIEWS_SUBMISSION_CONFIRMATION,
mimetype='html',
user=user,
**context
)
5 changes: 5 additions & 0 deletions website/mails/mails.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,8 @@ def get_english_article(word):
'send_data_share_preprint_error_desk',
subject='Share Error'
)

REVIEWS_SUBMISSION_CONFIRMATION = Mail(
'reviews_submission_confirmation',
subject='Confirmation of your submission to ${provider_name}'
)
2 changes: 1 addition & 1 deletion website/notifications/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
'global_comments': 'Comments added',
'global_file_updated': 'Files updated',
'global_mentions': 'Mentions added',
'global_reviews': 'Preprints submission updated'
'global_reviews': 'Preprint submissions updated'
}

# Note: the python value None mean inherit from parent
Expand Down