Hello!
I want to build review articles before publish. But that would happen if an author chose a journal, if he did not choose a journal when creating his article the article would simply be published on the site.
In this regard, I have two questions:
- How to build it in view? If I understand correctly, it can be built by BooleanField in models. But I don’t know how to build it in my views. I tried to build this, but It doesn’t work.
- Also it will be good opportunity if this article is accepted by publisher if they have a representative on the site, if not, the article would be sent to the database for inspection by the administration of the site (I have “editor” in journal models).
I will be happy if you help me.
articles/models.py
class Article(models.Model, HitCountMixin):
slug = models.SlugField(unique=True)
title = models.CharField(max_length=255)
author = models.ForeignKey(
"users.CustomUser", on_delete=models.SET_NULL, null=True
)
abstract = models.TextField(blank=True, null=True)
content = models.TextField(blank=True, null=True)
journal = models.ForeignKey(
"journals.Journal", on_delete=models.SET_NULL, null=True, blank=True
)
journalyear = models.ForeignKey(
"JournalYear", on_delete=models.SET_NULL, null=True, blank=True
)
journalvolume = models.ForeignKey(
"JournalVolume", on_delete=models.SET_NULL, null=True, blank=True
)
journal_pages = models.CharField(max_length=10, null=True, blank=True)
article_type = models.ForeignKey(
"Article_type", on_delete=models.SET_NULL, null=True, blank=True
)
created_date = models.DateTimeField(
default=timezone.now, null=True, blank=True)
published_date = models.DateTimeField(
default=timezone.now, blank=True, null=True)
submitted = models.BooleanField(default=False)
def __str__(self):
return self.title
class Meta:
ordering = ["-created_date"]
def get_absolute_url(self):
return reverse("article_detail", args=[self.slug])
journal/models.py
class Journal(models.Model):
editor = models.ForeignKey(
get_user_model(), on_delete=models.SET_NULL, null=True, blank=True
)
slug = models.SlugField(unique=True)
name = models.CharField(max_length=100, blank=True, null=True)
description = models.TextField(null=True, blank=True)
def __str__(self):
return self.name
class Meta:
ordering = ["-name"]
def get_absolute_url(self):
return reverse("journal_detail", args=[self.slug])
articles/views.py
class ArticleCreateView(LoginRequiredMixin, CreateView, SuccessMessageMixin):
model = Article
form_class = ArticleForm
template_name = "articles/example.html"
success_message = "%(title)s was created successfully"
def form_valid(self, form):
form.instance.author = self.request.user
return super(ArticleCreateView, self).form_valid(form)
def post(self, request, *args, **kwargs):
article = Article.objects.all()
form = self.get_form()
if article.journal:
article.submitted = False
messages.add_message(self.request, messages.INFO,
'Your Article pending for publisher approval')
return self.form_valid(form)
else:
article.submitted = True
return self.form_valid(form)
articles/forms.py
class ArticleForm(forms.ModelForm):
title = forms.CharField(required=False)
abstract = forms.CharField(required=False, widget=PagedownWidget())
content = forms.CharField(required=False, widget=PagedownWidget())
article_type = forms.ModelChoiceField(
required=False, queryset=Article_type.objects.all()
)
language = forms.ModelChoiceField(required=False, queryset=Language.objects.all())
class Meta:
model = Article
exclude = ["author", "created_date", "published_date", "id"]
help_texts = {
"title": _("Article should contain words, number and etc..."),
"tags": _("Keywords should contain words, number and etc..."),
"abstract": _("Abstract should contain words, number and etc..."),
"text": _("Text should contain words, number and etc..."),
"article_type": _("Text should contain words, number and etc..."),
"subject": _("Text should contain words, number and etc..."),
"language": _("Text should contain words, number and etc..."),
}
widgets = {
"journal": autocomplete.ModelSelect2(url="journalautocomplete"),
"journalvolume": autocomplete.ModelSelect2(
url="journalvolumeautocomplete", forward=["journalyear"]
),
"journalyear": autocomplete.ModelSelect2(
url="journalyearautocomplete", forward=["journal"]
),
}
articles/example.html
{% extends 'base.html' %}
{% block content %}
<form method="post">{% csrf_token %}
{{ form.media }}
{{form.as_p}}
<button type='submit' name='save_article'>Save</button>
</form>
{% endblock content %}