Tuesday, January 8, 2019

Re: default groups and permissions

hey man, sorry I was very busy.
just now got the time, so I wrote this in like 15 minutes, there is probably better solution but is working and I will check for something better tomorrow.

first of all I didn't have time to create abstract user so I worked with standard django user + model korisnik (which is basically user on serbian language :D)

this is models.py


forms.py just have 2 forms, one for user, another for 'korisnik'


and in views.py I did it with function based view like this:


so there are 2 forms, and it checks if they are both valid.
it takes request.POST from choice (I created all groups already) and based on choice it will put user into that group.

I hope this can help you but if have any further question, I am available tomorrow.

уто, 8. јан 2019. у 16:43 Robin Riis <riis.somfan@gmail.com> је написао/ла:
 12 class SignUp(PermissionRequiredMixin, generic.CreateView):
 13     form_class = WiggenUserCreationForm
 14     success_url = reverse_lazy('check_users')
 15     template_name = 'registration/signup.html'
 16     login_url = '/konton/login'
 17     permission_required = 'users.add_user'
 18     
 19     def post(self, request, *args, **kwargs):
 20         form = self.form_class(request.POST)
 21         if form.is_valid():
 22             form.save()
 23             grupp = ''
 24             if request.POST.get('title') == 'Försäljningschef':
 25                 grupp = Group.objects.get_or_create(name = 'Försäljningschef')
 26             if request.POST.get('title') == 'Affärsområdeschef':
 27                 grupp = Group.objects.get_or_create(name = 'Affärsområdeschef')
 28             if request.POST.get('title') == 'Ekonomi':
 29                 grupp = Group.objects.get_or_create(name = 'Ekonomi')
 30             if request.POST.get('title') == 'Verkstad':
 31                 grupp = Group.objects.get_or_create(name = 'Verkstad')
 32             if request.POST.get('title') == 'IT utvecklare':
 33                 grupp = Group.objects.get_or_create(name = 'IT utvecklare')
 34 
 35             if grupp != '':
 36                 user = WiggenUser.objects.get(username = form.cleaned_data.get('username'))
 37                 user.groups.add(grupp)
 38                 print('did it work?')


so does my view look right now, and that did not work :P
am kinda busy right now but will try again tomorrow when i have more time.
thnx Nebojsa! but could you please show me a full example?

Den måndag 7 januari 2019 kl. 13:18:37 UTC+1 skrev Nebojsa:
you can do it from views.py inside signup function
if Försäljningschef
then if exist() sign there, if not create that group.

I will be at home in few hours so can write one full example if you can't make it until then.

нед, 6. јан 2019. у 18:55 Robin Riis <riis....@gmail.com> је написао/ла:
i have a custom user model that i want to have 4 default groups depending on job title.

this is my model.py
  1 from django.contrib.auth.models import AbstractUser, UserManager, PermissionsMixin
  2 from django.db import models
  3 
  4 class WiggenUserManager(UserManager):
  5     pass
  6 
  7 class WiggenUser(AbstractUser):
  8     title_choices = (
  9         ('Försäljningschef', 'Försäljningschef'),
 10         ('Affärsområdeschef', 'Affärsområdeschef'),
 11         ('Ekonomi', 'Ekonomi'),
 12         ('Verkstad', 'Verkstad'),
 13         ('IT utvecklare', 'IT utvecklare')
 14     )
 15     objects = WiggenUserManager()
 16     dob = models.DateField(blank=True, null=True)
 17     employee_number = models.IntegerField(blank=True, null=True)
 18     title = models.CharField(max_length=30, choices=title_choices, blank=True, null=True)
 19     mobile = models.CharField(max_length=15, blank=True, null=True)
 20     phone = models.CharField(max_length=15, blank=True, null=True)
 21     salary = models.IntegerField(blank=True, null=True)

so when i add a new user with for example (title Verkstad) i want that user to belong to the group verkstad..

where do i add that new group and set its permissions? i read somewhere about signal but it didnt say what to import or where to add it

anyone got an idea? :)

thnx in advance :)

--
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 post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/17b1e7b9-61e1-4643-9679-653d138052c9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/de9ac45a-4952-44e6-aab7-7c41dc2b684b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJ60hW0aY1ECrgP_RBQAqF%2BF6dgiJqmHEE8g0VU4QKKeR0W18g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment