from parallels.common.actions.base.subscription_backup_action import SubscriptionBackupAction
from parallels.common.actions.utils.logging_properties \
	import LoggingProperties
from parallels.common.connections_config import MailContent
from parallels.common.checking import Problem

class EmailEmptyPassword(SubscriptionBackupAction):
	def get_description(self):
		return "Check empty passwords of mailboxes"

	def get_failure_message(self, global_context, subscription):
		return "Failed to check for empty passwords of mailboxes for subscription '%s'" % (subscription.name)

	def get_logging_properties(self):
		return LoggingProperties(info_log=False)

	def _run_subscription_backup(
		self, global_context, subscription, subscription_backup
	):
		if (
			subscription.mail_source_server is None or 
			subscription.mail_source_server.mail_settings.mode == MailContent.NONE
		):
			return
		
		for mailbox in subscription_backup.iter_mailboxes():
			if mailbox.password is None or mailbox.password == '':
				subscription.add_report_issue(
					global_context.pre_check_report,
					Problem(
						'mailuser-with-empty-password', Problem.WARNING, 
						u"Content can not be migrated for '%s' mailuser because of empty password of mailuser." % mailbox.full_name 
					),
					"Set password for '%s' mailuser on source panel." % mailbox.full_name
				)
