限制唯一序列从one2many字段删除一行

时间:2019-07-08 07:31:53

标签: python odoo

我创建了一个对象“ scanning.invoice.approval.rule”,在其中定义了一个“ one2many”字段:


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    public static final int PICK_IMAGE_REQUEST = 1;
    public static final int DELAY_MILLIS = 5000;

    private Button buttonChooseImage, buttonUpload;
    private TextView textViewShowUpload;
    private EditText editTextFileName;
    private ImageView imageView;
    private ProgressBar progressBar;

    private Uri imageUri;
    private StorageReference storageReference;
    private DatabaseReference databaseReference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonChooseImage = findViewById(R.id.button_choose_image);
        buttonUpload = findViewById(R.id.button_upload);
        textViewShowUpload = findViewById(R.id.text_view_show_uploads);
        editTextFileName = findViewById(R.id.edit_text_file_name);
        imageView = findViewById(R.id.image_view);
        progressBar = findViewById(R.id.progress_bar);

        buttonChooseImage.setOnClickListener(this);
        buttonUpload.setOnClickListener(this);
        textViewShowUpload.setOnClickListener(this);

        storageReference = FirebaseStorage.getInstance().getReference("uploads");
        databaseReference = FirebaseDatabase.getInstance().getReference("uploads");


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_choose_image:
                openFileChooser();
                break;
            case R.id.button_upload:
                uploadFile();
                break;
            case R.id.text_view_show_uploads:

                break;
        }
    }

    private String getFileExtension(Uri uri) {
        ContentResolver cr = getContentResolver();
        MimeTypeMap mine = MimeTypeMap.getSingleton();
        return mine.getExtensionFromMimeType(cr.getType(uri));
    }

    private void uploadFile() {
        if (imageUri != null) {
            StorageReference fileRefrence = storageReference.child(System.currentTimeMillis()
                    + "." + getFileExtension(imageUri));
            fileRefrence.putFile(imageUri).
                    addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            Handler handler = new Handler();
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    progressBar.setProgress(0);
                                }
                            }, DELAY_MILLIS);

                            Toast.makeText(MainActivity.this, "Upload Successful", Toast.LENGTH_LONG).show();
                            Upload upload = new Upload(editTextFileName.getText().toString().trim(),
                                    taskSnapshot.getMetadata().getReference().getDownloadUrl().toString());
                            String uploadId = databaseReference.push().getKey();
                            databaseReference.child(uploadId).setValue(upload);

                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            e.printStackTrace();
                            Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    })
                    .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                            double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
                            progressBar.setProgress((int) progress);
                        }
                    });
        } else {
            Toast.makeText(this, "No File Selected", Toast.LENGTH_SHORT).show();

        }
    }

    private void openFileChooser() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, PICK_IMAGE_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
                && data != null && data.getData() != null) {
            imageUri = data.getData();
            Picasso.get().load(imageUri).into(imageView);

        }
    }
}


此行对象的定义如下:


private void uploadFile() {
        if (imageUri != null) {
            StorageReference fileReference = storageReference.child("images/" + imageUri.getLastPathSegment());
            UploadTask uploadTask = fileReference.putFile(imageUri);
            uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            progressBar.setProgress(0);
                        }
                    }, DELAY_MILLIS);

                    Toast.makeText(MainActivity.this, "Upload Successful", Toast.LENGTH_LONG).show();
                    Upload upload = new Upload(editTextFileName.getText().toString().trim(),
                            taskSnapshot.getMetadata().getReference().getDownloadUrl().toString());
                    String uploadId = databaseReference.push().getKey();
                    databaseReference.child(uploadId).setValue(upload);
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    e.printStackTrace();
                    Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                    double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
                    progressBar.setProgress((int) progress);
                }
            });



        } else {
            Toast.makeText(this, "No File Selected", Toast.LENGTH_SHORT).show();

        }
    }


问题是当我添加一条规则和行并保存,然后再次编辑,例如删除序列1的一行,然后添加与删除的行具有相同序列1的新行然后保存时,尽管我提出了约束刚刚删除了一行并以相同的顺序添加了另一行,我希望这很清楚,并非常感谢您的所有帮助

关于如何解决它的任何想法?

1 个答案:

答案 0 :(得分:0)

这与Odoo的操作方式相同,Odoo使用命令来更新X2many字段 只需确保首先进行删除,然后再进行添加即可。

我不知道这是否行得通,但我认为只需订购列表即可解决问题

           @api.multi
           def write(self, vals):
               """ make sure deletion happends before adding."""
               if 'approval_line_ids' in vals:
                    deleting_commands = []
                    other_commands = []
                    for command in vals.get('approval_line_ids', []):
                        if command[0] in [2,3,5]:
                            deleting_commands.append(command)
                        else:
                            other_commands.append(command)

                    ordered_commands = deleting_commands + other_commands
                    vals['approval_line_ids'] =  ordered_commands

                return super(ClassName, self).write(vals)

如果第一个解决方案无效,请尝试以下操作: 这可能会使处理速度变慢,因为您会两次调用write:

   @api.multi
   def write(self, vals):
       """ make sure deletion happends before adding."""
       if 'approval_line_ids' in vals:
            deleting_commands = []
            other_commands = []
            for command in vals.get('approval_line_ids', []):
                if command[0] in [2,3,5]:
                    deleting_commands.append(command)
                else:
                    other_commands.append(command)

            if deleting_commands:
                # If there is a deleting do it first
                vals['approval_line_ids'] = deleting_commands
                super(ClassName, self).write(vals)
                # redefine vals to perform just other commands
                vals = {'approval_line_ids': other_commands}

        return super(ClassName, self).write(vals)