python - Automatically Update Django Profile when creating user -


i new django , hoping easy think should be.

i have written custom django app uses saltstack end spinning aws instances. when instance comes up, adds user created using ssh key.

i need give each user uid , gid can create user accounts on linux hosts them. right now, requires admin go in each new user created , manually update these fields in admin interface before user can use tool.

what hoping when user created, there way django search through profiles uid/gids have been used, increment one, , assign newly created user automatically.

here profile model:

class profile(models.model):     user = models.onetoonefield(user, on_delete=models.cascade)     linux_username = models.charfield(max_length=50, blank=true)     linux_uid = models.charfield(max_length=10, blank=true)     linux_gid = models.charfield(max_length=10, blank=true)     linux_ssh_authkey = models.textfield(max_length=1000, blank=true)  @receiver(post_save, sender=user) def update_user_profile(sender, instance, created, **kwargs):     if created:         profile.objects.created(user=instance)     instance.profile.save() 

i hope makes sense--when user signs up, choose linux username , upload ssh public key. but, still have go in admins populate uid , gid fields. example, 4001, if user signed up, 4002.

is there way add simple bit of code update_user_profile function auto-populate these fields?

i have tried messing around little cannot figure out how poll database next available uid/gid , set new user.

any appreciated!

thanks!

if in model gid , uid real charfield need use cast before aggregation , can try create it:

from django.db.models import max django.db.models import integerfield django.db.models.functions import cast  @receiver(post_save, sender=user) def update_user_profile(sender, instance, created, **kwargs):     if created:         luid = profile.objects.annotate(int_uid=cast('linux_uid', integerfield())).aggregate(luid=max('int_uid')).get('luid', 4000)         lgid = profile.objects.annotate(int_gid=cast('linux_gid', integerfield())).aggregate(lgid=max('int_gid')).get('lgid', 4000)               profile.objects.created(             user=instance,             linux_uid=int(luid) + 1,             linux_gid=int(lgid) + 1         ) 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -