Posts Tagged ‘Python’

Django admin: how to hide fields for certain users (that are not superusers)

Posted on the April 8th, 2010 under Django, Programming, Python by Mauro

I’m working on a project and I’m using the incredible django admin. So, yesterday I needed a way to hide some fields in a model for user that didn’t have superuser permissions. After some googling, I found a method in the ModelAdmin class that was perfect (well, I think that it’s perfect) for my needs: get_form.
The method is not really mentioned in the official django documentation except in the comment framework, but you can use it in your ModelAdmin subclass as well. It’s called before the “change form” is created, so we can dynamically change it before it’s displayed.
The principle is very simple: I dynamically populate the exclude attribute so that if a user is not a superuser I can exclude a field (or more that one field). Let’s see an example:

class MyModelAdmin(admin.ModelAdmin):
	def get_form(self, request, obj=None, **kwargs):
		self.exclude = []	
		if not request.user.is_superuser:
			self.exclude.append('field_to_hide')
		return super(MyModelAdmin, self).get_form(request, obj, **kwargs)

What it does is simply add the field_to_hide to the exclude list of MyModelAdmin. In this case, the field will be visible only to superusers, checking the request.user.is_superuser attribute. Pretty simple!

Donate 1 euro, buy me a coffee, I need it to write more posts! Thanks ;)

How to develop a Plug-in for Panic Coda with Python

Posted on the March 31st, 2009 under Programming, Python by Mauro

If you are (like me) a mac fan and you use the excellent Panic Coda to develop, you should know that is now possible to write plugins with every languages you want (if you have the interpreter installed on your system).
Python is bundled on every mac, and is also a beautiful and simple language, so i want to show how to develop a simple plugin for Coda with Python!
First of all you need to download the Coda Plug-in Creator that is the application that will convert your python script into a brand new plugin.

Donate 1 euro, buy me a coffee, I need it to write more posts! Thanks ;)

Contact me

Posted on the March 17th, 2009 under Art by Mauro

Your Name (required)

Your Email (required)

Subject

Your Message

Donate 1 euro, buy me a coffee, I need it to write more posts! Thanks ;)