def add(request, provider_id=None): all_for_provider = notifications_model.get_all_for_provider(provider_id=provider_id) provider_form = PROVIDERS.get(provider_id, False) if not provider_form: return redirect(reverse('notifications_all')) if request.method == "POST": form = provider_form(request.POST) if form.is_valid(): data = form.cleaned_data notifications_model.save(data=data, provider_id=provider_id) messages.add_message(request, messages.INFO, '{0} settings updated'.format(provider_id.title())) return redirect(reverse('notifications_edit', kwargs={'provider_id': provider_id})) else: form = provider_form() return render(request, 'notifications/view.html', { "form": form, "provider_id": provider_id, "add_form": True, "all_for_provider": all_for_provider })
def add(request, provider_id=None): all_for_provider = notifications_model.get_all_for_provider(provider_id=provider_id) provider_form = PROVIDERS.get(provider_id, False) if not provider_form: return redirect(reverse('notifications_all')) if request.method == "POST": form = provider_form(request.POST) if form.is_valid(): data = form.cleaned_data notifications_model.save(data=data, provider_id=provider_id) messages.add_message(request, messages.INFO, '{0} settings updated'.format(provider_id.title())) return redirect(reverse('notifications_edit', kwargs={'provider_id': provider_id})) else: form = provider_form() return render_to_response('notifications/view.html', { "form": form, "provider_id": provider_id, "add_form": True, "all_for_provider": all_for_provider }, context_instance=RequestContext(request))
def save(self): email = self.cleaned_data.get('email') password = self.cleaned_data.get('password') user = User.objects.create_user(email, password) user.is_admin = True user.is_staff = True user.is_superuser = True user.save() notifications_model.save(data={"email": email}, provider_id='email') alerts_model.add_initial_data() api_key_model.add_initial_data()
def setUp(self): User.objects.all().delete() self.c = Client() self.user = User.objects.create_user(password='******', email='*****@*****.**') self.account_id = 1 self.c.login(username='******', password='******') self.server_collection = alerts_model.mongo.get_collection('servers') self.server_collection.insert({ "name": "test", "key": "test_me", "account_id": self.account_id }) server = self.server_collection.find_one( {'account_id': self.account_id}) self.server_id = server['_id'] self.process_collection = alerts_model.mongo.get_collection( 'processes') self.process_collection.insert({ "name": "test", "account_id": self.account_id }) process = self.process_collection.find_one() self.process_id = process['_id'] notifications_model.save(data={"email": "*****@*****.**"}, provider_id="email") notifications = notifications_model.get_all_formated() self.notifications_list = [x['formated_id'] for x in notifications] notifications_model.save(data={"email": "*****@*****.**"}, provider_id="email") notifications = notifications_model.get_all_formated() self.updated_notifications_list = [ x['formated_id'] for x in notifications ] self.example_alert_dict = { "above_below": "above", "email_recepients": [], "rule_type": "global", "server": "all", "account_id": self.account_id, "period": 300, }
def setUp(self): User.objects.all().delete() self.c = Client() self.user = User.objects.create_user(password='******', email='*****@*****.**') self.account_id = 1 self.c.login(username='******', password='******') self.server_collection = alerts_model.mongo.get_collection('servers') self.server_collection.insert({ "name" : "test", "key": "test_me", "account_id": self.account_id }) server = self.server_collection.find_one({'account_id': self.account_id}) self.server_id = server['_id'] self.process_collection = alerts_model.mongo.get_collection('processes') self.process_collection.insert({ "name" : "test", "account_id": self.account_id }) process = self.process_collection.find_one() self.process_id = process['_id'] notifications_model.save(data={"email": "*****@*****.**"}, provider_id="email") notifications = notifications_model.get_all_formated() self.notifications_list = [x['formated_id'] for x in notifications] notifications_model.save(data={"email": "*****@*****.**"}, provider_id="email") notifications = notifications_model.get_all_formated() self.updated_notifications_list = [x['formated_id'] for x in notifications] self.example_alert_dict = { "above_below": "above", "email_recepients": [], "rule_type": "global", "server": "all", "account_id": self.account_id, "period": 300, }
def setUp(self): self.c = Client() self.user = User.objects.create_user(password='******', email='*****@*****.**') self.account_id = 1 self.c.login(username='******', password='******') server_key = server_model.add('test', account_id=self.account_id) self.server = server_model.get_server_by_key(server_key) self.server_id = self.server['_id'] notifications_model.save(data={"email": "*****@*****.**"}, provider_id="email") notifications = notifications_model.get_all_formated() self.notifications_list = [x['formated_id'] for x in notifications] self.emails = [x['email'] for x in notifications] self.process = process_model.get_or_create(server_id=self.server_id, name='testprocess') self.process_id = self.process['_id']
def setUp(self): self.c = Client() self.user = User.objects.create_user(password='******', email='*****@*****.**') self.account_id = 1 self.c.login(username='******', password='******') server_key = server_model.add('test', account_id=self.account_id) self.server = server_model.get_server_by_key(server_key) self.server_id = self.server['_id'] email_model.insert({'sent_from': '*****@*****.**'}) notifications_model.save(data={"email": "*****@*****.**"}, provider_id="email") notifications = notifications_model.get_all_formated() self.notifications_list = [x['formated_id'] for x in notifications] self.emails = [x['email'] for x in notifications] self.process = process_model.get_or_create(server_id=self.server_id, name='testprocess') self.process_id = self.process['_id']
def migrate_four_to_five(request): from amon.apps.notifications.models import notifications_model from amon.apps.notifications.legacymodels import email_recepient_model from amon.apps.notifications.legacymodels import webhooks_model from amon.apps.alerts.models import alerts_model # Move emails all_emails = email_recepient_model.get_all() for e in all_emails: email_exists = notifications_model.collection.find_one({ 'email': e.get('email'), 'provider_id': 'email' }) if email_exists == None: notifications_model.save(data={"email": e.get('email')}, provider_id='email') # Move webhooks all_webhooks = webhooks_model.get_all() for e in all_webhooks: hook_exists = notifications_model.collection.find_one({ 'url': e.get('url'), 'provider_id': 'webhook' }) if hook_exists == None: data = { 'url': e.get('url'), 'name': e.get('url'), 'secret': e.get('secret') } notifications_model.save(data=data, provider_id='webhook') # Set default names all_notifications = notifications_model.get_all() for noti in all_notifications: if noti.get('provider_id') not in ['webhook', 'email']: name = noti.get('name', None) if name == None: notifications_model.update(data={'name': 'default'}, id=noti.get('_id')) # Update all alerts alerts = alerts_model.get_all() for alert in alerts: new_notifications_list = [] emails = alert.get('email_recepients', []) webhooks = alert.get('webhooks', []) old_notifications = alert.get('notifications', []) for email in emails: email_from_old_model = email_recepient_model.get_by_id(email) if email_from_old_model: new_email_location = notifications_model.collection.find_one({ 'email': email_from_old_model.get('email'), 'provider_id': 'email' }) if new_email_location: _id = "email:{0}".format(new_email_location.get('_id')) new_notifications_list.append(_id) for hook in webhooks: hook_from_old_model = webhooks_model.get_by_id(hook) if hook_from_old_model: new_hook_location = notifications_model.collection.find_one({ 'url': hook_from_old_model.get('url'), 'provider_id': 'webhook' }) if new_hook_location: _id = "webhook:{0}".format(new_hook_location.get('_id')) new_notifications_list.append(_id) for provider in old_notifications: new_notification = notifications_model.collection.find_one( {'provider_id': provider}) if new_notification: _id = "{0}:{1}".format(provider, new_notification.get('_id')) new_notifications_list.append(_id) try: del alert['email_recepients'] del alert['webhooks'] except: pass alert['notifications'] = new_notifications_list alerts_model.update(alert, alert.get('_id')) messages.add_message(request, messages.INFO, 'Migration complete.') return redirect(reverse('servers'))