Beispiel #1
0
def multiple_loading_pattern(request,format=None):
    query_params=request.query_params
    if request.method=='DELETE':
        try:
            pk=query_params['pk']
            mlp=MultipleLoadingPattern.objects.get(pk=pk)   
            if request.user!=mlp.user and not request.user.is_superuser or mlp.authorized:
                error_message={'error_message':"you have no permission"}
                return Response(data=error_message,status=550)
            mlp.delete()
            success_message={'success_message':'your request has been handled successfully'}
            return Response(data=success_message,status=200,)
        except Exception as e:
            error_message={'error_message':e}
            return Response(data=error_message,status=404)
            
        
    try:
        plantname=query_params['plant']
        unit_num=query_params['unit']
        cycle_num=query_params['cycle']
        plant=Plant.objects.get(abbrEN=plantname)
        unit=UnitParameter.objects.get(plant=plant,unit=unit_num)
        cycle=Cycle.objects.get(unit=unit,cycle=cycle_num)
    except Exception as e:
        error_message={'error_message':e}
        return Response(data=error_message,status=404)
    
        
    data=request.data
 
    if request.method == 'POST':
        file=data['file']
        name=request.query_params['name']
        print(request.query_params)
        try: 
            pre_pk=request.query_params['pre_pk']  
            if pre_pk:
                pre_loading_pattern=MultipleLoadingPattern.objects.get(pk=pre_pk)
                mlp=MultipleLoadingPattern(user=request.user,name=name,xml_file=file,cycle=cycle,pre_loading_pattern=pre_loading_pattern)
            else:
                mlp=MultipleLoadingPattern(user=request.user,name=name,xml_file=file,cycle=cycle)
            mlp.full_clean()
            mlp.save()
            success_message={'success_message':'your request has been handled successfully','pk':mlp.pk}
            return Response(data=success_message,status=200)
        except Exception as e:
            error_message={'error_message':e}
            return Response(data=error_message,status=404)
    
    if request.method=='GET':
        user=request.user
        same_group_users=get_same_group_users(user)   
        mlps=MultipleLoadingPattern.objects.filter(Q(user__in=same_group_users)&Q(visibility=2)|Q(user=user)|Q(visibility=3),cycle=cycle)
       
        if mlps.exists():
            serializer = MultipleLoadingPatternSerializer(mlps,many=True)
            return Response(data=serializer.data,status=200)  
        else:
            return Response(data={},status=200)
    
    if request.method == 'PUT':
        try:
            file=data['file']
            name=request.query_params['name']
            loading_pattern=MultipleLoadingPattern.objects.get(name=name,cycle=cycle,user=request.user,)
       
            #check if has permission
            if request.user!=loading_pattern.user and (not request.user.is_superuser) or loading_pattern.authorized:
                error_message={'error_message':"you have no permission"}
                return Response(data=error_message,status=550)
            
            if loading_pattern.post_loading_patterns.all().exists():
                error_message={'error_message':'The loading pattern is referenced by other loading patterns'}
                return Response(data=error_message,status=404)
            
            loading_pattern.xml_file.delete()
            del_fieldfile.send(sender=MultipleLoadingPattern,pk=loading_pattern.pk)
            loading_pattern.xml_file=file
            loading_pattern.full_clean()
            loading_pattern.save()
            success_message={'success_message':'your request has been handled successfully'}
            return Response(data=success_message,status=200)
        except Exception as e:
            return Response(data={'error_message':e},status=404)