Question about models. test

19 views
Skip to first unread message

dupakoor kannan

unread,
Feb 25, 2021, 1:07:43 AM2/25/21
to django...@googlegroups.com
Hello everyone,

I have the following model

class Publication(models.Model):
    name = models.CharField(max_length=25, blank=True, null=False)
    publication_url = models.TextField(blank=True)

and observed there are some inactive URLs on the production server (due to data entry mistakes). Is there any way I can write a test for the inactive URLs from the database (local copy)?. 


Thanks
Kannan

Ryan Nowakowski

unread,
Feb 25, 2021, 5:51:45 PM2/25/21
to django...@googlegroups.com
On Thu, Feb 25, 2021 at 01:06:00AM -0500, dupakoor kannan wrote:
> I have the following model
>
> class Publication(models.Model):
> name = models.CharField(max_length=25, blank=True, null=False)
> publication_url = models.TextField(blank=True)

Once you clean out all the bad data, you might want to migrate from
TextField to URLField here. That would help prevent data entry
mistakes assuming some of the errors are invalid URLs.

One other suggestion, even though you didn't ask :) I'd rename
publication_url to just url since it's a part of the Publication model,
the "publication" part is redundant.

> and observed there are some inactive URLs on the production server (due to
> data entry mistakes). Is there any way I can write a test for the inactive
> URLs from the database (local copy)?.

I assume "inactive" here means that the URLs are returning HTTP 404.
For the test, you can run this in `python manage.py shell`:

import requests # you'll need to pip install requests

for publication in Publication.objects.all():
response = requests.get(publication.publication_url)
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
# do something here with your "inactive" URL
pass


If you want to make this easier to run in the future, I suggest making a
custom management command[1]. If you want this test to run anytime
Publication.save() is called, add a validator[2] to publication_url.

[1] https://docs.djangoproject.com/en/3.1/howto/custom-management-commands/
[2] https://docs.djangoproject.com/en/3.1/ref/validators/

Peter of the Norse

unread,
May 2, 2021, 2:43:33 PM5/2/21
to django...@googlegroups.com
This kind of script can be dangerous.  It’s possible for malicious websites to create a page that can use up too many resources or otherwise act strangely.  URLValidator used to do this kind of thing optionally, but that was removed because of all of the problems it caused.  If you do try visiting all of the websites, make sure you have a low timeout and don’t follow redirects.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20210225225044.GE19963%40fattuba.com.

Reply all
Reply to author
Forward
0 new messages