diff --git a/news/factories.py b/news/factories.py index 74dcad74481d0c0de0e9dfeaabba153673a29723..a17229133a154ccd27117e135fac715a522ab54d 100644 --- a/news/factories.py +++ b/news/factories.py @@ -7,8 +7,8 @@ class NewsItemFactory(factory.django.DjangoModelFactory): class Meta: model = NewsItem - date = factory.Faker('date_time') - headline = factory.Faker('sentence', nb_words=6) - blurb = factory.Faker('text', max_nb_chars=200) - followup_link = factory.Faker('url') + date = factory.Faker('date_this_year') + headline = factory.Faker('sentence') + blurb = factory.Faker('paragraph', nb_sentences=8) + followup_link = factory.Faker('uri') followup_link_text = factory.Faker('sentence', nb_words=4) diff --git a/scipost/management/commands/models.py b/scipost/management/commands/models.py deleted file mode 100644 index 0447660e2a189556e389b610e0c5d6bc797218aa..0000000000000000000000000000000000000000 --- a/scipost/management/commands/models.py +++ /dev/null @@ -1,188 +0,0 @@ -import datetime -import hashlib -import random -import string - -from django.db import models, IntegrityError -from django.conf import settings -from django.utils import timezone - -from . import constants -from .managers import RegistrationInvitationQuerySet, CitationNotificationQuerySet - -from scipost.constants import TITLE_CHOICES - - -class RegistrationInvitation(models.Model): - """ - Invitation to particular persons for registration - """ - title = models.CharField(max_length=4, choices=TITLE_CHOICES) - first_name = models.CharField(max_length=30) - last_name = models.CharField(max_length=150) - email = models.EmailField() - status = models.CharField(max_length=8, choices=constants.REGISTATION_INVITATION_STATUSES, - default=constants.STATUS_DRAFT) - - # Text content - message_style = models.CharField(max_length=1, choices=constants.INVITATION_STYLE, - default=constants.INVITATION_FORMAL) - personal_message = models.TextField(blank=True) - invited_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, - blank=True, null=True, related_name='invitations_sent') - created_by = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='invitations_created') - - # Related to objects - invitation_type = models.CharField(max_length=2, choices=constants.INVITATION_TYPE, - default=constants.INVITATION_CONTRIBUTOR) - - # Response keys - invitation_key = models.CharField(max_length=40, unique=True) - key_expires = models.DateTimeField(default=timezone.now) - - # Timestamps - date_sent_first = models.DateTimeField(null=True, blank=True) - date_sent_last = models.DateTimeField(null=True, blank=True) - times_sent = models.PositiveSmallIntegerField(default=0) - created = models.DateTimeField(auto_now_add=True) - modified = models.DateTimeField(auto_now=True) - - objects = RegistrationInvitationQuerySet.as_manager() - - class Meta: - ordering = ['last_name'] - - def __str__(self): - return '{} {} on {}'.format(self.first_name, self.last_name, - self.created.strftime("%Y-%m-%d")) - - def save(self, *args, **kwargs): - self.refresh_keys(commit=False) - return super().save(*args, **kwargs) - - def refresh_keys(self, force_new_key=False, commit=True): - # Generate email activation key and link - if not self.invitation_key or force_new_key: - # TODO: Replace this all by the `secrets` package available from python 3.6(!) - salt = '' - for i in range(5): - salt += random.choice(string.ascii_letters) - salt = salt.encode('utf8') - invitationsalt = self.last_name.encode('utf8') - self.invitation_key = hashlib.sha1(salt + invitationsalt).hexdigest() - self.key_expires = timezone.now() + datetime.timedelta(days=365) - if commit: - self.save() - - def mail_sent(self, user=None): - """ - Update instance fields as if a new invitation mail has been sent out. - """ - if self.status == constants.STATUS_DRAFT: - self.status = constants.STATUS_SENT - if not self.date_sent_first: - self.date_sent_first = timezone.now() - self.date_sent_last = timezone.now() - self.invited_by = user or self.created_by - self.times_sent += 1 - self.citation_notifications.update(processed=True) - self.save() - - @property - def has_responded(self): - return self.status in [constants.STATUS_DECLINED, constants.STATUS_REGISTERED] - - -class CitationNotification(models.Model): - invitation = models.ForeignKey('invitations.RegistrationInvitation', - on_delete=models.SET_NULL, - null=True, blank=True) - contributor = models.ForeignKey('scipost.Contributor', - on_delete=models.CASCADE, - null=True, blank=True, - related_name='+') - - # Content - submission = models.ForeignKey('submissions.Submission', null=True, blank=True, - related_name='+') - publication = models.ForeignKey('journals.Publication', null=True, blank=True, - related_name='+') - processed = models.BooleanField(default=False) - - # Meta info - created_by = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='notifications_created') - date_sent = models.DateTimeField(null=True, blank=True) - created = models.DateTimeField(auto_now_add=True) - modified = models.DateTimeField(auto_now=True) - - objects = CitationNotificationQuerySet.as_manager() - - class Meta: - default_related_name = 'citation_notifications' - unique_together = ( - ('invitation', 'submission'), - ('invitation', 'publication'), - ('contributor', 'submission'), - ('contributor', 'publication'), - ) - - def __str__(self): - _str = 'Citation for ' - if self.invitation: - _str += ' Invitation ({} {})'.format( - self.invitation.first_name, - self.invitation.last_name, - ) - elif self.contributor: - _str += ' Contributor ({})'.format(self.contributor) - - _str += ' on ' - if self.submission: - _str += 'Submission ({})'.format(self.submission.arxiv_identifier_w_vn_nr) - elif self.publication: - _str += 'Publication ({})'.format(self.publication.doi_label) - return _str - - def save(self, *args, **kwargs): - if not self.submission and not self.publication: - raise IntegrityError(('CitationNotification needs to be related to either a ' - 'Submission or Publication object.')) - return super().save(*args, **kwargs) - - def mail_sent(self): - """ - Update instance fields as if a new citation notification mail has been sent out. - """ - self.processed = True - if not self.date_sent: - # Don't overwrite by accident... - self.date_sent = timezone.now() - self.save() - - def related_notifications(self): - return CitationNotification.objects.unprocessed().filter( - models.Q(contributor=self.contributor) | models.Q(invitation=self.invitation)) - - def get_first_related_contributor(self): - return self.related_notifications().filter(contributor__isnull=False).first() - - @property - def email(self): - if self.invitation: - return self.invitation.email - elif self.contributor: - return self.contributor.user.email - - @property - def last_name(self): - if self.invitation: - return self.invitation.last_name - elif self.contributor: - return self.contributor.user.last_name - - @property - def get_title(self): - if self.invitation: - return self.invitation.get_title_display() - elif self.contributor: - return self.contributor.get_title_display() diff --git a/scipost/management/commands/populate_db.py b/scipost/management/commands/populate_db.py index fff744730218a7164e1eda03096cb7354b454f18..83cf6e9ed7a1f3a355448a5e30a8378b1f498e8d 100644 --- a/scipost/management/commands/populate_db.py +++ b/scipost/management/commands/populate_db.py @@ -168,8 +168,7 @@ class Command(BaseCommand): def create_issues(self): IssueFactory.create_batch(5) - self.stdout.write(self.style.SUCCESS( - 'Successfully created 5 Issue.')) + self.stdout.write(self.style.SUCCESS('Successfully created 5 Issue.')) def create_publications(self): PublicationFactory.create_batch(5)