def post(self, request, *args, **kwargs): """Updates an Alert and its events""" events = filter(lambda item: item[0] != 'csrfmiddlewaretoken' and item[0] != 'contract', request.POST.items()) auth_code = request.GET.get('code') contract = request.POST.get('contract') operation_type = request.POST.get('operation_type') return_message = '' if auth_code: if operation_type == 'DELETE': try: dapp_obj = DApp.objects.get(authentication_code=auth_code) dapp_obj.delete() return_message = 'DApp was deleted' except DApp.DoesNotExist: return self.get(request, *args, **kwargs) else: try: alert_obj = Alert.objects.get( dapp__authentication_code=auth_code, contract=contract ) except Alert.DoesNotExist: return self.get(request, *args, **kwargs) if events: events_obj = Event.objects.filter(alert=alert_obj.id) events_obj.delete() for event in events: event_obj = Event() event_obj.name = event[0] event_obj.alert = alert_obj event_obj.save() return_message = 'Alert was updated.' else: # Delete Alert alert_obj.delete() return_message = 'Alert was deleted.' kwargs['message'] = return_message return self.get(request, *args, **kwargs)
def create(self, validated_data): # Create the objects eventvalue_obj = None event_obj = None alert_obj = None try: alert_obj = Alert.objects.get( dapp__authentication_code=self.context['request'].auth. authentication_code, contract=validated_data.get('contract')) except Alert.DoesNotExist: alert_obj = Alert() alert_obj.abi = validated_data.get('abi') alert_obj.dapp = self.context['request'].auth alert_obj.contract = validated_data.get('contract') alert_obj.save() if validated_data.get('events'): events_obj = Event.objects.filter(alert=alert_obj.id) events_obj.delete() for key in validated_data.get('events'): properties_dict = validated_data.get('events').get(key) event_obj = Event() event_obj.name = key event_obj.alert = alert_obj event_obj.save() for propertykey, propertyvalue in properties_dict.iteritems(): # Event Value object eventvalue_obj = EventValue() eventvalue_obj.property = propertykey eventvalue_obj.value = propertyvalue eventvalue_obj.event = event_obj eventvalue_obj.save() else: # Delete Alert when no Events in request body alert_obj.delete() return alert_obj
def post(self, request, *args, **kwargs): """Updates an Alert and its events""" events = filter( lambda item: item[0] != 'csrfmiddlewaretoken' and item[0] != 'contract', request.POST.items()) auth_code = request.GET.get('code') contract = request.POST.get('contract') operation_type = request.POST.get('operation_type') abi = request.POST.get('abi') # used in add contract form return_message = '' if auth_code: if operation_type == 'DELETE': # deletes a DApp and its alerts try: dapp_obj = DApp.objects.get(authentication_code=auth_code) dapp_obj.delete() return_message = 'DApp was deleted' except DApp.DoesNotExist: return self.get(request, *args, **kwargs) elif operation_type == 'ADD_CONTRACT': # Creates a new alert for the provided contract try: kwargs['contract_errors'] = [] json_abi = json.loads(abi) if abi and isinstance(json_abi, list) and contract: # check if the alert already exists dapp_obj = DApp.objects.get( authentication_code=auth_code) try: Alert.objects.get(contract=contract, dapp_id=dapp_obj.id) kwargs['contract_errors'].append( "The contract already exists.") except Alert.DoesNotExist: # Create the new alert alert_obj = Alert() alert_obj.abi = abi alert_obj.dapp = dapp_obj alert_obj.contract = contract alert_obj.save() return_message = "Contract created, please set the events." else: # form errors if not contract or len(contract) == 0: kwargs['contract_errors'].append( "Contract is mandatory") if not abi or len(abi) == 0 or not isinstance( json.loads(abi), list): kwargs['contract_errors'].append( "Please provide a valid ABI string") except ValueError: kwargs['contract_errors'].append( "Please provide a valid ABI string") finally: kwargs['contract'] = contract kwargs['abi'] = abi else: try: alert_obj = Alert.objects.get( dapp__authentication_code=auth_code, contract=contract) except Alert.DoesNotExist: return self.get(request, *args, **kwargs) if events: events_obj = Event.objects.filter(alert=alert_obj.id) events_obj.delete() for event in events: event_obj = Event() event_obj.name = event[0] event_obj.alert = alert_obj event_obj.save() return_message = 'Alert was updated.' else: # Delete Alert if no events are selected alert_obj.delete() return_message = 'Alert was deleted.' kwargs['message'] = return_message return self.get(request, *args, **kwargs)