From 51ff990cbbd33f1285188dfd87b5e7055e204ea1 Mon Sep 17 00:00:00 2001 From: Lukas Winkler Date: Tue, 28 Jul 2020 18:25:56 +0200 Subject: [PATCH] use clean instead of save --- acros/models/Acronym.py | 26 ++++++++++++-------------- acros/models/PaperReference.py | 3 +-- acros/models/Tag.py | 3 +-- acros/models/Weblink.py | 3 +-- acros/models/WikipediaLink.py | 11 +++++++---- acros/utils/apis.py | 5 ++++- 6 files changed, 26 insertions(+), 25 deletions(-) diff --git a/acros/models/Acronym.py b/acros/models/Acronym.py index 9e0e97b..13e534d 100644 --- a/acros/models/Acronym.py +++ b/acros/models/Acronym.py @@ -31,16 +31,24 @@ class Acronym(models.Model): modified_date = models.DateTimeField(auto_now=True) pageviews = models.IntegerField(default=0, editable=False) - def save(self, *args, **kwargs): + def clean(self): + if not self.slug: + self.slug = slugify(self.name) + + if not self.id: + try: + found = Acronym.objects.get(slug=self.slug) + except Acronym.DoesNotExist: + found = False + if found: + raise ValidationError(f"slug '{self.slug}' already exists") + self.description_html = md_to_html(self.description_md) if not self.acro_letters: self.acro_letters = [0] for pos, char in enumerate(self.full_name): if char == " " and pos <= len(self.full_name): self.acro_letters.append(pos + 1) - if not self.slug: - self.slug = slugify(self.name) - super(Acronym, self).save(*args, **kwargs) def __str__(self): return self.name @@ -53,13 +61,3 @@ class Acronym(models.Model): class Meta: ordering = ["name"] - - def clean(self): - if not self.id: - new_slug = slugify(self.name) - try: - found = Acronym.objects.get(slug=new_slug) - except Acronym.DoesNotExist: - found = False - if found: - raise ValidationError(f"slug '{new_slug}' already exists") diff --git a/acros/models/PaperReference.py b/acros/models/PaperReference.py index e482f92..f5bca1e 100644 --- a/acros/models/PaperReference.py +++ b/acros/models/PaperReference.py @@ -22,7 +22,7 @@ class PaperReference(models.Model): def __str__(self): return self.title - def save(self, *args, **kwargs): + def clean(self, *args, **kwargs): if not self.fetched: ads.config.token = ADS_AUTH_TOKEN cols = ["title", "author", "year", "pubdate", "doi", "identifier"] @@ -45,7 +45,6 @@ class PaperReference(models.Model): else: self.arxiv_id = None self.fetched = True - super(PaperReference, self).save(*args, **kwargs) @property def ads_url(self): diff --git a/acros/models/Tag.py b/acros/models/Tag.py index 97b96b1..81a2748 100644 --- a/acros/models/Tag.py +++ b/acros/models/Tag.py @@ -12,9 +12,8 @@ class Tag(models.Model): def __str__(self): return self.name - def save(self, *args, **kwargs): + def clean(self): self.slug = slugify(self.name) - super(Tag, self).save() def get_absolute_url(self): return reverse('tag', args=[str(self.slug)]) diff --git a/acros/models/Weblink.py b/acros/models/Weblink.py index fb8a5bf..279b0f6 100644 --- a/acros/models/Weblink.py +++ b/acros/models/Weblink.py @@ -17,8 +17,7 @@ class Weblink(models.Model): def __str__(self): return self.url - def save(self, *args, **kwargs): + def clean(self): uri = urlparse(self.url) self.host, created = Host.objects.get_or_create(host=uri.hostname) self.title = get_website_title(self.url) - super(Weblink, self).save(*args, **kwargs) diff --git a/acros/models/WikipediaLink.py b/acros/models/WikipediaLink.py index 6ca100f..7305f0f 100644 --- a/acros/models/WikipediaLink.py +++ b/acros/models/WikipediaLink.py @@ -1,10 +1,11 @@ from urllib.parse import unquote +from django.core.exceptions import ValidationError from django.db import models from simple_history.models import HistoricalRecords from acros.models import Acronym, WikipediaImage -from acros.utils.apis import WikipediaAPISummary +from acros.utils.apis import WikipediaAPISummary, NotFoundError class WikipediaLink(models.Model): @@ -19,9 +20,12 @@ class WikipediaLink(models.Model): fetched = models.BooleanField(default=False) history = HistoricalRecords() - def save(self, *args, **kwargs): + def clean(self): if not self.fetched: - summary = WikipediaAPISummary(self.title) + try: + summary = WikipediaAPISummary(self.title) + except NotFoundError as e: + raise ValidationError(str(e)) self.extract = summary.extract self.extract_html = summary.extract_html self.description = summary.description @@ -38,7 +42,6 @@ class WikipediaLink(models.Model): self.thumbnail = thumbnail self.fetched = True - super(WikipediaLink, self).save(*args, **kwargs) @property def url(self): diff --git a/acros/utils/apis.py b/acros/utils/apis.py index 2e9b377..608aade 100644 --- a/acros/utils/apis.py +++ b/acros/utils/apis.py @@ -16,7 +16,10 @@ class WikipediaAPISummary: def __init__(self, title: str): r = requests.get(self.urlbase + title) - r.raise_for_status() + try: + r.raise_for_status() + except requests.HTTPError: + raise NotFoundError("Wikipedia API returns error") self.data = r.json() @property