def handle(self, *args, **options): processed_external_usernames = [] for yandex_domain in YandexDomain.objects.all(): for user in MailUser.objects.active().filter(external_username__contains="@"+yandex_domain.name): if user.external_username in processed_external_usernames: continue processed_external_usernames.append(user.external_username) username, domain = user.external_username.split('@') new_password = generate_password() try: yandex_domain.api.edit_user_details(login=username, password=new_password) except YandexApiException, e: self.stderr.write("Failed to set new password for %s, the error was: %s" % (user.external_username, e.message)) else: MailUser.objects.filter(external_username=user.external_username).\ update(external_password=new_password)
def create_user_at_yandex(sender, **kwargs): if not kwargs["created"]: return instance = kwargs["instance"] try: username, domain = instance.external_username.split("@") except ValueError: return try: yd = YandexDomain.objects.get(name=domain) except YandexDomain.DoesNotExist: return password = generate_password() try: yd.api.create_user(username, password) except YandexApiException: pass else: instance.external_password = password instance.save()