Problem with class and "self"

I have a Django app that is taking input from a form, one field of which may, or may not, have a URL for an image to be uploaded. At the moment I am attempting to run some tests on the given URL before I retrieve it. To address the entire set of issues with the image I’ve created a new class imageurl.

Here is how imageurl is called:

views.py

from utilskit import imageurl

class FormsetMixin(object):
    object = None

    …

    def post(self, request, *args, **kwargs):        
        url = request.POST.get('imgurl')
        imageurl.testimage(url)
        …

Then the imageurl class:

utilskit.py

class imageurl():

	VALID_IMAGE_EXTENSIONS = [
		'.jpg',
		'.jpeg',
		'.png',
		'.gif',
	]

	VALID_IMAGE_MIMETYPES = [
		"image"
		]
		
		
	########################
	# Main entry tests
	########################

	def testimage(url):
		#from django.contrib import messages
        #"else" use here is just for debugging
		if url == '' or url == None:
			print('url is empty')
			return True
		if not self.check_extension(url):
			#messages.success('Extension on URL submitted is not in allowed set')  # set UI message
			print('Extension on URL submitted is not in allowed set')
			return False
		else:
			#messages.success('Extension OK')  # set UI message
			print('Extension OK')

		if not imageurl.check_mimetype(url):

               …

		print('all tests passed')
		return True

	########################
	# usually sub tests
	########################
	
	def check_extension(url, extension_list=VALID_IMAGE_EXTENSIONS):
		return any([url.endswith(e) for e in extension_list])
		

	def check_mimetype(url, mimetype_list=VALID_IMAGE_MIMETYPES):
		import mimetypes
		mimetype, encoding = mimetypes.guess_type(url)
		if mimetype:
			return any([mimetype.startswith(m) for m in mimetype_list])
		else:
			return False
	
	def check_exists(url):
		import requests
		…

When I use self.check_extension() like this I get an error that “self” is not defined. I’ve tried running the class in Idle too:

url = 'http://my.com/logo.png'
imageurl.testimage(url)

Which results in the same error. If I put “self” like this: def testimage(self, url): I get the error testimage() takes exactly 2 arguments (1 given).

If I change the tests to directly name the class if not imageurl.check_extension(url):

What is the correct way to use “self” in a class like this?

Two related questions, is it correct that I have to do import inside each defined function in the class:

	def check_mimetype(url, mimetype_list=VALID_IMAGE_MIMETYPES):
		import mimetypes
    …

Rather than in the view?

And, int he class, do I address messages like this:

request.user.messages.debug()

Thanks

I think you may need to work a little on your understanding of what a Python class is, and how they work - particularly with respect to methods within a class and how they’re used.

Some resources you might find helpful:

1 Like

Sorry, managed to post half a message there!? I’ll check your URLs.