2020-06-01 11:03:21 +02:00
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
from simple_history.models import HistoricalRecords
|
|
|
|
|
|
|
|
from acros.models import Acronym, Host
|
2020-06-01 20:26:00 +02:00
|
|
|
from acros.utils.apis import get_website_title
|
2020-06-01 11:03:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Weblink(models.Model):
|
|
|
|
acronym = models.ForeignKey(Acronym, on_delete=models.CASCADE, related_name="links")
|
|
|
|
url = models.URLField()
|
|
|
|
host = models.ForeignKey(Host, on_delete=models.CASCADE, editable=False)
|
2020-06-01 20:26:00 +02:00
|
|
|
title = models.CharField(max_length=500, blank=True)
|
2020-06-01 11:03:21 +02:00
|
|
|
history = HistoricalRecords()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.url
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
uri = urlparse(self.url)
|
|
|
|
self.host, created = Host.objects.get_or_create(host=uri.hostname)
|
2020-06-01 20:26:00 +02:00
|
|
|
self.title = get_website_title(self.url)
|
2020-06-01 11:03:21 +02:00
|
|
|
super(Weblink, self).save(*args, **kwargs)
|