def handle(self, request, data): try: if not os.path.exists(enum.EMAIL_CFG_FILE): os.mknod(enum.EMAIL_CFG_FILE) cfgparser = ConfigParser.ConfigParser() cfgparser.read(enum.EMAIL_CFG_FILE) if cfg_tool.getOption(cfgparser, 'receiver', 'receiver'): receiver = json.loads( cfg_tool.getOption(cfgparser, 'receiver', 'receiver')) else: receiver = [] new_addr = str(data.get('address')) receiver.append(new_addr) cfg_tool.setOption(cfgparser, 'receiver', 'receiver', json.dumps(receiver)) cfgparser.write(open(enum.EMAIL_CFG_FILE, 'w')) operate_log( request.user.username, request.user.roles, "add mail-receiver \"" + data.get('address') + "\" success") return True except Exception as e: operate_log( request.user.username, request.user.roles, "add mail-receiver \"" + data.get('address') + "\" error") return False
def get_initial(self): if os.path.exists(enum.EMAIL_CFG_FILE): cfgparser = ConfigParser.ConfigParser() cfgparser.read(enum.EMAIL_CFG_FILE) return {'server_address':cfg_tool.getOption(cfgparser, 'server', 'ipaddress'), 'server_port':cfg_tool.getOption(cfgparser, 'server', 'port'), 'sender_address':cfg_tool.getOption(cfgparser, 'sender', 'address'), 'sender_username':cfg_tool.getOption(cfgparser, 'sender', 'user'), 'old_password':cfg_tool.getOption(cfgparser, 'sender', 'password')}
def clean(self): cleaned_data = super(AddReceiverForm, self).clean() if os.path.exists(enum.EMAIL_CFG_FILE): cfgparser = ConfigParser.ConfigParser() cfgparser.read(enum.EMAIL_CFG_FILE) if cfg_tool.getOption(cfgparser, 'receiver', 'receiver'): receiver = json.loads( cfg_tool.getOption(cfgparser, 'receiver', 'receiver')) new_addr = str(cleaned_data.get('address')) if new_addr in receiver: raise forms.ValidationError( _('The email address has already exists.')) return cleaned_data
def get_emailreceiver_data(self): if os.path.exists(enum.EMAIL_CFG_FILE): cfgparser = ConfigParser.ConfigParser() cfgparser.read(enum.EMAIL_CFG_FILE) receiver = cfg_tool.getOption(cfgparser, 'receiver', 'receiver') res = [] if receiver: for a in json.loads(receiver): res.append({'address':a}) return DictList2ObjectList(res,'address') return []
def action(self, request, obj_id): try: cfgparser = ConfigParser.ConfigParser() cfgparser.read(enum.EMAIL_CFG_FILE) receiver = cfg_tool.getOption(cfgparser, 'receiver', 'receiver') if receiver: arr = json.loads(receiver) arr.remove(obj_id) cfg_tool.setOption(cfgparser, 'receiver', 'receiver', json.dumps(arr)) cfgparser.write(open(enum.EMAIL_CFG_FILE, 'w')) operate_log(request.user.username, request.user.roles, "delete mail-receiver \"" + obj_id + "\" success") except Exception as e: operate_log(request.user.username, request.user.roles, "delete mail-receiver \"" + obj_id + "\" error")