Tuesday, October 25, 2022

Re: Django call_command from Admin

Hi Paul,

Many thanks for this, much appreciated,

I will have a look and try to implement it,
will let you know.

Kind regards
Aziz Meknassi

On Monday, October 24, 2022 at 2:03:08 PM UTC+1 pa...@scom.ca wrote:

you need to basically add a hook (save class) to your save model and
process stuff along the way.

you will need to modify this example but it's how i update info when
saving an scom email user for dovecot.

you can also override the admin class as well with something simaliar

Note :
super(EmailUsers, self).save(*args, **kwargs) #Save the email changes

is extemely important as stuff will process but any field updates will
not save without the above line......

example :


def save(self, *args, **kwargs): #This will create an email account if
nessesary.
emailaddress = str(self.username) #Set the email address to see if we
need to create the mailbox?
emailpassword = str(self.password) #Set the password (used for check
alias)

#prefill username/domain is blank
if self.domain == None or str(self.domain) == '' :
self.domain = self.username.split('@')[1]

if self.source == None or str(self.source) == '' :
self.source = self.username

if self.destination == None or str(self.destination) == '' :
self.destination = self.username




domain = str(self.domain)
send_settings = str(self.send_settings)
if self.send_settings == None :
send_settings = ''
self.send_settings = ''
dontupdate = self.dontupdate
self.dontupdate = False

#set dovecot directory & split user / domain
if self.username.split('@')[1] == 'preload.scom.ca' :
self.home = '/data/dovecot/users/%s/%s' %(
self.domain,self.destination.split('@')[0] )
else:
self.home = '/data/dovecot/users/%s/%s' %( self.domain,self.destination )

self.user = self.username.split('@')[0]

#Set alias Flag
if self.password == 'alias' or (self.source != self.destination) :
self.alias_flag = True
#self.sieve_forwards = ''
#self.vacation_active = False
else :
self.alias_flag = False

self.username_search = self.username
self.username_search = self.username_search.replace ('.','')
self.username_search = self.username_search.replace ('@','')


super(EmailUsers, self).save(*args, **kwargs) #Save the email changes

i email with something like :

#Try to get info for this account
if dontupdate == False :

imap_test = Dovecot_Command ('INFO',self.username) #do i have this
account ?

if 'BAD' in imap_test.answer :
try : #Try to Create the account, note that the db must be updated
properly before it will work
imap_create = Dovecot_Command ('CM',self.username)
if 'OK' in imap_create.answer :
send_subject = 'Email Account Created : %s' %(str(self.username) )

except :
send_subject = 'Error Account : %s' %(str(self.username) )
pass

else :
send_subject = 'Email Account Updated : %s' %(self.username)

#Send update email

send_from = 'mon...@scom.ca'
send_files = []
send_to = ['mon...@scom.ca']
send_text = '\n\n'+ send_subject + '\n'
sendmail(send_from,send_to,send_subject,send_text,send_files) #Send
the warning email




Happy Monday !!!
Thanks - paul

Paul Kudla


Scom.ca Internet Services <http://www.scom.ca>
004-1009 Byron Street South
Whitby, Ontario - Canada
L1N 4S3

Toronto 416.642.7266
Main 1.866.411.7266
Fax 1.888.892.7266
Email pa...@scom.ca

On 10/21/2022 2:30 PM, Mohammad Ehsan Ansari wrote:
> Please refer the signal concept in django you will get how to implement it
>
> On Thu, 20 Oct, 2022, 9:36 pm Aziz Mek, <azi...@gmail.com
> <mailto:azi...@gmail.com>> wrote:
>
> Hi All,
>
> I was wondering  if you have come across the following:
>
> I have a field in the model that's empty, when the user fills it up
> and clicks Save, it
> should trigger/call a management Command (This command is already
> build that sends emails ), Django docs say i can use call_command
> but not sure how to implement it with the save
>
> I am just after the trigger really when the save takes place
>
> Many thanks in advance
>
> Kind regards
> Aziz
>
>
> --
> 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
> <mailto:django-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/a0ca69f0-6065-4b86-a977-cfb6dcab8fd7n%40googlegroups.com <https://groups.google.com/d/msgid/django-users/a0ca69f0-6065-4b86-a977-cfb6dcab8fd7n%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
> 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
> <mailto:django-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAF8ux6Li0BjKS2zvQuPWL6zq-R6Buo9wa1fBx68m4ESpTptHug%40mail.gmail.com <https://groups.google.com/d/msgid/django-users/CAF8ux6Li0BjKS2zvQuPWL6zq-R6Buo9wa1fBx68m4ESpTptHug%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
> --
> This message has been scanned for viruses and
> dangerous content by *MailScanner* <http://www.mailscanner.info/>, and is
> believed to be clean.

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b9780a9a-bff1-4305-abb4-6d12a9ce9920n%40googlegroups.com.

No comments:

Post a Comment