def get(self, request): user = request.user all_exps = Exp.objects.all().order_by('name') open_alerts = Alert.objects.filter(dismissed=False).order_by('when') classified = {'RUN_OK': [], 'RUN_ERR': [], 'RUN_ABO': [], 'END_OK': [], 'END_ABO': []} total_aborted, total_errors = 0, 0 for exp in all_exps: try: status, error, aborted = exp.parse_exp_overview() info = exp.parse_exp_info() total_aborted += aborted total_errors += error classified[status].append([exp, info]) except: #what to do if the exp doesn't exist? # ALERT! And this alert never ever goes away alert = Alert.objects.filter(exp=exp).filter(message='NOT FOUND').filter(dismissed=False) if not alert: alert = Alert(exp=exp, message='NOT FOUND') alert.save() return render(request, self.template_name, {'user':user, 'alerts': open_alerts, 'classified':classified, 'total_errors': total_errors, 'total_aborted': total_aborted})
def settings(request): if request.method == 'POST': form = SettingsForm(request.POST) if form.is_valid(): try: Alert.objects.get(user=request.user).delete() except: pass data = form.cleaned_data client_t = data['user_time'] server_t = datetime.now().strftime('%H') tz_diff = int(server_t) - int(client_t) as_time = datetime(2013, 1, 1, data['time'].hour, data['time'].minute) as_time = as_time + timedelta(hours=tz_diff) a = Alert( user=request.user, alert_server_time=as_time, start=data['start'], finish=data['finish'], period=data['period'], time=data['time'], alert_email=data['alert_email'], email=data['email'], alert_sms=data['alert_sms'], phone=data['phone'] ) alert = {} try: a.save() alert['txt'] = 'Настройки сохранены' alert['cls'] = 'alert-success' except: alert['txt'] = 'Не удалось сохранить настройки, попробуйте повторить позже' alert['cls'] = 'alert-danger' return render_to_response('settings.html', {'form': form, 'alert': alert}, RequestContext(request)) else: try: a = Alert.objects.get(user=request.user) form = SettingsForm(instance=a) except: form = SettingsForm() return render_to_response('settings.html', {'form': form}, RequestContext(request))
def post(self, request, *args, **kwargs): try: alert_d = request.data # alert_d['employer_profile_id'] = alert_d.pop('employer_profile')['profile_id'] # alert_d['location_id'] = alert_d.pop('location')['location_id'] if 'alert_id' not in alert_d: okStatus = status.HTTP_201_CREATED else: okStatus = status.HTTP_200_OK if alert_d['query']['terms']: alert_d['query']['terms'] = ','.join(alert_d['query']['terms']) else: alert_d['query']['terms'] = None if alert_d['query']['employer_names']: alert_d['query']['employer_names'] = ','.join(alert_d['query']['employer_names']) else: alert_d['query']['employer_names'] = None if alert_d['query']['locations']: alert_d['query']['locations'] = ','.join(alert_d['query']['locations']) else: alert_d['query']['locations'] = None query = Query(**alert_d.pop('query')) query.save() alert_d['query'] = query alert = Alert(**alert_d) alert.save() return Response(AlertSerializer(alert).data, status=okStatus) except Exception as e: print '%s (%s)' % (e, type(e)) return Response(e.message)
def post(self): """ Create a new alert """ global users_connected json_data, error = get_data(request) if not error: device_data, error = validate_json_payload(json_data, self.fields) if not error: error = self.validate_jetson_data(device_data, data_dict={ 'truck_id': device_data['truck_id'], 'behavior': device_data['behavior'], 'date_': device_data['alert_init'] }) if not error: try: alert_data = self.alert_schema.load({ 'truck_id': device_data['truck_id'], 'behavior': device_data['behavior'], 'alert_init': device_data['alert_init'] }) except marshmallow.exceptions.ValidationError as errors: return response(400, str(errors)) alert = Alert(**alert_data) error = alert.save() if not error: # TODO: add alert.id to msg alert_event_notify(socket_obj, users_connected) return response(200, data={'alert_id': alert.id}) return error
def alert(request): a = Alert() a.alert = request.body a.save() return HttpResponse('success')