Django仅在更改某个特定字段时发送邮件

时间:2016-08-01 09:10:31

标签: python django redis

我正在使用: RQ scheduler

我的项目中有一个Job模型:

class Job(models.Model):
    # Other fields
    status = models.CharField(
        max_length=2, choices=choices.JOB_STATE_CHOICES, default=choices.PENDING_APPROVAL)

我每天晚上安排一项任务,检查今天批准的所有工作,并向我的用户发送有关它的邮件。

所以查询:

jobs = Job.objects.filter(status='APPROVED',
                          updated_at__date=datetime.today().date())

此查询的问题是我如何知道此作业状态字段仅在今天更改。

即使今天更改了职位的职位并且状态在5天后发生了变化,此查询也可以完成工作。

我只需要提取今天“状态”字段已更改的作业。

1 个答案:

答案 0 :(得分:1)

您应该使用django-dirtyfields并立即发送电子邮件或将此job_id添加到RQ。 完整文档here

或仅在status通过写自己的save()方法更改{再次django-dirtyfields时更新def save(self): if self.status = 'aproved' and 'status' in self.get_dirty_fields(): # add to RQ # or set updated_at = datetime.now() # if you dont use on_update=datetime.now super(...).save() 字段。

package com.domen.myapplication.Files;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.PowerManager;
import android.widget.Toast;


import com.domen.myapplication.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by DL  on 01.08.2016.
 */
public class DownloadTask extends AsyncTask<String, Integer, String> {

    private Context context;
    private PowerManager.WakeLock mWakeLock;
    public int progress=0;
    public int progressShow=0;
    public ProgressDialog mProgressDialog;




    public DownloadTask(Context context) {
        this.context = context;

        mProgressDialog = new ProgressDialog(this.context);
        mProgressDialog.setMessage(this.context.getResources().getString(R.string.loading_empty));
        mProgressDialog.setIndeterminate(true);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mProgressDialog.setCancelable(true);


    }

    @Override
    protected String doInBackground(String... sUrl) {
        InputStream input = null;
        OutputStream output = null;
        HttpURLConnection connection = null;
        try {
            URL url = new URL(sUrl[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            // expect HTTP 200 OK, so we don't mistakenly save error report
            // instead of the file
            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                return "Server returned HTTP " + connection.getResponseCode()
                        + " " + connection.getResponseMessage();
            }

            // this will be useful to display download percentage
            // might be -1: server did not report the length
            int fileLength = connection.getContentLength();

            // download the file
            input = connection.getInputStream();
            //output = new FileOutputStream("/sdcard/file_name.extension");


            File cacheFile = new File(this.context.getCacheDir(), "FIRMWARE");

            output = new FileOutputStream(cacheFile);

            byte data[] = new byte[4096];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                // allow canceling with back button
                if (isCancelled()) {
                    input.close();
                    return null;
                }
                total += count;
                // publishing the progress....
                if (fileLength > 0) // only if total length is known
                    publishProgress((int) (total * 100 / fileLength));
                output.write(data, 0, count);
            }
        } catch (Exception e) {
            return e.toString();
        } finally {
            try {
                if (output != null)
                    output.close();
                if (input != null)
                    input.close();
            } catch (IOException ignored) {
            }

            if (connection != null)
                connection.disconnect();
        }
        return null;
    }


    //метод выше (doInBackground) запускается отдельным поток все сотальное ниже в главном потоке
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // take CPU lock to prevent CPU from going off if the user
        // presses the power button during download
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                getClass().getName());
        mWakeLock.acquire();
        progressShow=1;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        // if we get here, length is known, now set indeterminate to false
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setMax(100);
        mProgressDialog.setProgress(progress[0]);
        this.progress=progress[0];
    }

    @Override
    protected void onPostExecute(String result) {
        mWakeLock.release();
        progressShow=1;
        mProgressDialog.dismiss();
        if (result != null)
            Toast.makeText(context, "Download error: " + result, Toast.LENGTH_LONG).show();
        else
            Toast.makeText(context, "File downloaded", Toast.LENGTH_SHORT).show();
    }


}