Group Admin

2013-12-20 Thread vinayak11118
New here. Just wanted to know if there are any group admins present who 
review a post? Because I posted a question sometime back and it still 
hasn't showed up on the news feed.

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/4a103155-00eb-4bde-86b2-b079f5da3503%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Django Custom User Admin Login

2013-12-20 Thread vinayak11118
I'm creating a custom user by extending the AbstractBaseUser class. I have 
read the Django documentation and several tutorials online and I think that 
what I've done till now is very basic and should work.

The problem I'm facing is that even if I create a new DB from scratch and 
do a syncdb, the manage.py console **does not ask me for a admin username 
and password** (as it usually does). Hence, I cannot access /admin. 

The problem is partially (and wrongly) resolved my using 
AUTH_PROFILE_MODULE instead of AUTH_USER_MODEL. This however leads to 
'Users' and 'MyUser' being showed as different objects on the admin page 
i.e. MyUser is not set as the default user. (obviously)

**models.py**

class UserManager(BaseUserManager):
def create_user(self, username, email, corp, password=None):
if not (username or email or corp):
raise ValueError('Users must have an username, email and 
corporation')
user = self.model(username=username, 
email=UserManager.normalize_email(email), corp=corp, )
user.set_password(password)
user.is_active = True
user.save(using=self._db)
return user

def create_superuser(self, username, email, corp, password):
user = self.create_user(self, username, email, corp,
password=password, )
user.is_active = True
user.is_admin = True
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user


class MyUser(AbstractBaseUser, PermissionsMixin):
username = models.CharField(max_length=254, unique=True, 
blank=False)
first_name = models.CharField(max_length=30, blank=True)
last_name = models.CharField(max_length=30, blank=True)
email = models.EmailField(blank=True)
corp = models.ForeignKey(Client, related_name="is_employee_of")

is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)

def get_full_name(self):
return self.first_name + " " + self.last_name

def get_short_name(self):
return self.first_name

def __unicode__(self):
return self.username + "@" + self.corp.shortName

objects = UserManager()

USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email', 'corp']


**admin.py**

admin.site.register(Client)
admin.site.register(Address)

class UserCreationForm(forms.ModelForm):
password1 = forms.CharField(label='Password', 
widget=forms.PasswordInput)
password2 = forms.CharField(label='Password Confirmation', 
widget=forms.PasswordInput)

class Meta:
model = MyUser
fields = ('username', 'email', 'corp', 'first_name', 
'last_name')

def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")

if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords don't match")

return password2

def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])

if commit:
user.save()
return user

class UserChangeForm(forms.ModelForm):
password = ReadOnlyPasswordHashField()

class Meta:
model = MyUser

def clean_password(self):
return self.initial["password"]

class MyUserAdmin(UserAdmin):
form = UserChangeForm
add_form = UserCreationForm

list_display = ('username', 'email', 'corp', 'first_name', 
'last_name', 'is_admin', 'is_staff')
list_filter = ('is_admin',)
fieldsets = (
(None, {'fields': ('username', 'email', 'password')}),
('Personal Info', {'fields': ('first_name', 'last_name', 
'corp')}),
('Permissions', {'fields': ('is_admin', 'is_staff')}),
('Important Dates', {'fields': ('last_login',)}),
)

add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'email', 'password1', 'password2')}
),
)

search_fields = ('username',)
ordering = ('username',)
filter_horizontal = ()

admin.site.register(MyUser, MyUserAdmin)

**settings.py**


INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'Profiles

Re: Django Custom User Admin Login

2013-12-21 Thread vinayak11118
So I solved the problem. Even I don't know how. :/
I used 
thisas
 a template and added my own models and requirements on top of it. 

Slightly off topic but I needed this setup because I want to grant users 
object level permissions. 
django-guardianlooked like a match 
but I'm having trouble making it work with custom user 
models. The developer of guardian has a 
warningfor
 custom users. 

Since the template I used to create my custom user had 
admin.site.unregister(Group) included in the admin.py file, guardian throws:

'MyUser' has no attribute 'groups' error. Allowing groups to register shows the 
same error. Do we need to implement custom groups when we use custom users? As 
of now, I don't need the group functionality - so it'd be great if there is a 
work around.

Here is my architecture:

|--- *customuser*
   |--- *customauth*
   |--- management
   |--- migrations
   |--- admin.py// same as used in the initial post, slight 
additions
   |--- models.py  // same as used in the initial post, slight additions
   |--- views.py
   |--- *customuser*
   |--- setting.py
   |--- urls.py
   |--- *client001*
   |--- admin.py// posted below
   |--- models.py  // posted below
   |--- views.py  


*So, I have a separate app for each client (customer) that registers for my 
app. Each client can have multiple users with each user having permission 
to 'view' a 'stream' or not. The main app stores a list of all users and 
all clients.*


*customauth/models.py*

class Address(models.Model):
> country = models.CharField(_('Country'), max_length=100, blank=True)
> *** ommited ***
> street_line3 = models.CharField(_('Address Line 3'), max_length=100, 
> blank=True)
>
> class Client(models.Model):
> cID = models.IntegerField(primary_key=True, blank=False)
> *** ommited ***
> address = models.ForeignKey(Address, related_name='located_at')
>
> class MyUserManager(BaseUserManager):
> def create_user(self, email, username, password=None):
> *** ommited ***
> user.set_password(password)
> user.save(using=self._db)
> return user
>
> def create_superuser(self, email, username, password):
> *** ommited ***
> user.is_admin = True
> user.is_superuser = True
> user.save(using=self._db)
> return user
>
>
> class MyUser(AbstractBaseUser):
> email = models.EmailField(
> verbose_name='email address',
> max_length=255,
> unique=True,
> db_index=True,
> )
> username = models.CharField(max_length=254, unique=True, blank=False, 
> db_index=True)
> *** ommited ***
> corp = models.ForeignKey(Client, related_name="employee_of", null=True)
>

> objects = MyUserManager()
>
> USERNAME_FIELD = 'username'
> REQUIRED_FIELDS = ['email']
>
>
>
*customauth/admin.py* 

admin.site.register(Address)
admin.site.register(Client)


class UserCreationForm(forms.ModelForm):
password1 = forms.CharField(label='Password', 
widget=forms.PasswordInput)
password2 = forms.CharField(label='Password confirmation', 
widget=forms.PasswordInput)

class Meta:
model = ZenatixUser
fields = ('email', 'username')

def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords don't match")
return password2

def save(self, commit=True):
user = super(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user


class UserChangeForm(forms.ModelForm):
password = ReadOnlyPasswordHashField()

class Meta:
model = ZenatixUser
fields = ['username', 'corp', 'first_name', 'last_name', 'email', 
'password', 'date_of_birth', 'is_active',
  'is_admin']

def clean_password(self):
return self.initial["password"]


class MyUserAdmin(UserAdmin):
form = UserChangeForm
add_form = UserCreationForm
list_display = ('username', 'corp', 'email', 'is_admin')
list_filter = ('corp',)
fieldsets = (
(None, {'fields': ('username', 'email', 'corp', 'password')}),
('Personal info', {'fields': ('date_of_birth', 'first_name', 
'last_name')}),
('Permissions', {'fields': ('is_admin', 'is_active')}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'email', 'password1', 'password2', 
'corp')}
),
)
search_fields = ('username',)
ordering = ('username',)
filter_horizontal = ()

Re: Django Custom User Admin Login

2013-12-21 Thread vinayak11118
Also, from initial tests it appears that django-guardian is not setting 
permissions with custom user. I wrote a small manage.py script to test that:

from django.core.management.base import BaseCommand, CommandError
from iiitd.models import *
from guardian.shortcuts import assign_perm, remove_perm

class Command(BaseCommand):
def handle(self, *args, **options):
user = MyUser.objects.filter(username='abc')[0]
stream = Stream.objects.filter(uuid='001')[0]
assign_perm('read_stream', user, stream)
print user.has_perm('read_stream', stream)
remove_perm('read_stream', user, stream)
print user.has_perm('read_stream', stream)

It prints True in both cases.
However, the guardian_userobjectpermission table is updating correctly when 
I add or delete permissions. Confused. o.O

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/dc7c1d92-5a1c-4eb7-812f-6a1bcd09ccdf%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


referencing models before syncdb

2013-12-25 Thread vinayak11118
I have an actions.py file which defines custom actions for the admin page 
for one of my models. It uses an intermediary page (like the default delete 
action) and hence has a corresponding form which is also declared in the 
same file.

For some reason, I had drop by database (development) and now when I try to 
run syncdb, it gives me the following error:


Traceback (most recent call last):
  File "/home/vinayak/pyCharm/helpers/pycharm/django_manage.py", line 23, 
in 
run_module(manage_file, None, '__main__', True)
  File "/usr/lib/python2.7/runpy.py", line 176, in run_module
fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
  File "/home/vinayak/zenatix/customuser/manage.py", line 10, in 
execute_from_command_line(sys.argv)
  File 
"/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", 
line 399, in execute_from_command_line
utility.execute()
  File 
"/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", 
line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
  File 
"/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", 
line 242, in run_from_argv
self.execute(*args, **options.__dict__)
  File 
"/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", 
line 284, in execute
self.validate()
  File 
"/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", 
line 310, in validate
num_errors = get_validation_errors(s, app)
  File 
"/usr/local/lib/python2.7/dist-packages/django/core/management/validation.py", 
line 34, in get_validation_errors
for (app_name, error) in get_app_errors().items():
  File 
"/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 
196, in get_app_errors
self._populate()
  File 
"/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 
75, in _populate
self.load_app(app_name, True)
  File 
"/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 
99, in load_app
models = import_module('%s.models' % app_name)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", 
line 40, in import_module
__import__(name)
  File "/usr/local/lib/python2.7/dist-packages/debug_toolbar/models.py", 
line 63, in 
patch_root_urlconf()
  File "/usr/local/lib/python2.7/dist-packages/debug_toolbar/models.py", 
line 51, in patch_root_urlconf
reverse('djdt:render_panel')
  File 
"/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 
480, in reverse
app_list = resolver.app_dict[ns]
  File 
"/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 
310, in app_dict
self._populate()
  File 
"/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 
262, in _populate
for pattern in reversed(self.url_patterns):
  File 
"/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 
346, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", 
self.urlconf_module)
  File 
"/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 
341, in urlconf_module
self._urlconf_module = import_module(self.urlconf_name)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", 
line 40, in import_module
__import__(name)
  File "/home/vinayak/zenatix/customuser/customuser/urls.py", line 6, in 

admin.autodiscover()
  File 
"/usr/local/lib/python2.7/dist-packages/django/contrib/admin/__init__.py", 
line 29, in autodiscover
import_module('%s.admin' % app)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", 
line 40, in import_module
__import__(name)
  File "/home/vinayak/zenatix/customuser/iiitd/admin.py", line 3, in 

from actions import grant_read_permission
  File "/home/vinayak/zenatix/customuser/iiitd/actions.py", line 13, in 

class SelectUserForm(forms.Form):
  File "/home/vinayak/zenatix/customuser/iiitd/actions.py", line 16, in 
SelectUserForm
clientObj = ClientInfo.objects.all()[:1].get()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", 
line 301, in get
num = len(clone)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", 
line 77, in __len__
self._fetch_all()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", 
line 854, in _fetch_all
self._result_cache = list(self.iterator())
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", 
line 220, in iterator
for row in compiler.results_iter():
  File 
"/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", 
line 710, in results_iter
for rows in self.execute_sql(MULTI):
  File 
"/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler