Skip to content

Commit 7584719

Browse files
authored
Fixed README.rst code formatting and updated setup.py long description content type (#371)
* Update README.rst * Update setup.py * Fixed test.
1 parent 5ae3d6f commit 7584719

File tree

3 files changed

+121
-85
lines changed

3 files changed

+121
-85
lines changed

README.rst

Lines changed: 113 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,23 @@ Important Warning about Time Zones
3232
==================================
3333

3434
.. warning::
35+
If you change the Django ``TIME_ZONE`` setting your periodic task schedule
36+
will still be based on the old timezone.
3537

36-
If you change the Django ``TIME_ZONE`` setting your periodic task schedule
37-
will still be based on the old timezone.
38+
To fix that you would have to reset the "last run time" for each periodic task:
3839

39-
To fix that you would have to reset the "last run time" for each periodic
40-
task::
40+
.. code-block:: Python
4141
4242
>>> from django_celery_beat.models import PeriodicTask, PeriodicTasks
4343
>>> PeriodicTask.objects.all().update(last_run_at=None)
4444
>>> for task in PeriodicTask.objects.all():
4545
>>> PeriodicTasks.changed(task)
4646
47-
Note that this will reset the state as if the periodic tasks have never run
48-
before.
47+
48+
49+
.. note::
50+
This will reset the state as if the periodic tasks have never run before.
51+
4952

5053
Models
5154
======
@@ -76,24 +79,28 @@ incremented, which tells the ``celery beat`` service to reload the schedule
7679
from the database.
7780

7881
If you update periodic tasks in bulk, you will need to update the counter
79-
manually::
82+
manually:
83+
84+
.. code-block:: Python
8085
81-
>>> from django_celery_beat.models import PeriodicTasks
82-
>>> PeriodicTasks.update_changed()
86+
>>> from django_celery_beat.models import PeriodicTasks
87+
>>> PeriodicTasks.update_changed()
8388
8489
Example creating interval-based periodic task
8590
---------------------------------------------
8691

8792
To create a periodic task executing at an interval you must first
88-
create the interval object::
93+
create the interval object:
8994

90-
>>> from django_celery_beat.models import PeriodicTask, IntervalSchedule
95+
.. code-block:: Python
9196
92-
# executes every 10 seconds.
93-
>>> schedule, created = IntervalSchedule.objects.get_or_create(
94-
... every=10,
95-
... period=IntervalSchedule.SECONDS,
96-
... )
97+
>>> from django_celery_beat.models import PeriodicTask, IntervalSchedule
98+
99+
# executes every 10 seconds.
100+
>>> schedule, created = IntervalSchedule.objects.get_or_create(
101+
... every=10,
102+
... period=IntervalSchedule.SECONDS,
103+
... )
97104
98105
That's all the fields you need: a period type and the frequency.
99106

@@ -107,46 +114,52 @@ You can choose between a specific set of periods:
107114
- ``IntervalSchedule.MICROSECONDS``
108115

109116
.. note::
110-
111117
If you have multiple periodic tasks executing every 10 seconds,
112118
then they should all point to the same schedule object.
113119

114120
There's also a "choices tuple" available should you need to present this
115-
to the user::
121+
to the user:
122+
123+
124+
.. code-block:: Python
116125
117-
>>> IntervalSchedule.PERIOD_CHOICES
126+
>>> IntervalSchedule.PERIOD_CHOICES
118127
119128
120129
Now that we have defined the schedule object, we can create the periodic task
121-
entry::
130+
entry:
122131

123-
>>> PeriodicTask.objects.create(
124-
... interval=schedule, # we created this above.
125-
... name='Importing contacts', # simply describes this periodic task.
126-
... task='proj.tasks.import_contacts', # name of task.
127-
... )
132+
.. code-block:: Python
133+
134+
>>> PeriodicTask.objects.create(
135+
... interval=schedule, # we created this above.
136+
... name='Importing contacts', # simply describes this periodic task.
137+
... task='proj.tasks.import_contacts', # name of task.
138+
... )
128139
129140
130141
Note that this is a very basic example, you can also specify the arguments
131142
and keyword arguments used to execute the task, the ``queue`` to send it
132143
to[*], and set an expiry time.
133144

134145
Here's an example specifying the arguments, note how JSON serialization is
135-
required::
146+
required:
147+
148+
.. code-block:: Python
136149
137-
>>> import json
138-
>>> from datetime import datetime, timedelta
150+
>>> import json
151+
>>> from datetime import datetime, timedelta
139152
140-
>>> PeriodicTask.objects.create(
141-
... interval=schedule, # we created this above.
142-
... name='Importing contacts', # simply describes this periodic task.
143-
... task='proj.tasks.import_contacts', # name of task.
144-
... args=json.dumps(['arg1', 'arg2']),
145-
... kwargs=json.dumps({
146-
... 'be_careful': True,
147-
... }),
148-
... expires=datetime.utcnow() + timedelta(seconds=30)
149-
... )
153+
>>> PeriodicTask.objects.create(
154+
... interval=schedule, # we created this above.
155+
... name='Importing contacts', # simply describes this periodic task.
156+
... task='proj.tasks.import_contacts', # name of task.
157+
... args=json.dumps(['arg1', 'arg2']),
158+
... kwargs=json.dumps({
159+
... 'be_careful': True,
160+
... }),
161+
... expires=datetime.utcnow() + timedelta(seconds=30)
162+
... )
150163
151164
152165
.. [*] you can also use low-level AMQP routing using the ``exchange`` and
@@ -157,37 +170,43 @@ Example creating crontab-based periodic task
157170

158171
A crontab schedule has the fields: ``minute``, ``hour``, ``day_of_week``,
159172
``day_of_month`` and ``month_of_year``, so if you want the equivalent
160-
of a ``30 * * * *`` (execute every 30 minutes) crontab entry you specify::
161-
162-
>>> from django_celery_beat.models import CrontabSchedule, PeriodicTask
163-
>>> schedule, _ = CrontabSchedule.objects.get_or_create(
164-
... minute='30',
165-
... hour='*',
166-
... day_of_week='*',
167-
... day_of_month='*',
168-
... month_of_year='*',
169-
... timezone=pytz.timezone('Canada/Pacific')
170-
... )
173+
of a ``30 * * * *`` (execute every 30 minutes) crontab entry you specify:
174+
175+
.. code-block:: Python
176+
177+
>>> from django_celery_beat.models import CrontabSchedule, PeriodicTask
178+
>>> schedule, _ = CrontabSchedule.objects.get_or_create(
179+
... minute='30',
180+
... hour='*',
181+
... day_of_week='*',
182+
... day_of_month='*',
183+
... month_of_year='*',
184+
... timezone=pytz.timezone('Canada/Pacific')
185+
... )
171186
172187
The crontab schedule is linked to a specific timezone using the 'timezone' input parameter.
173188

174189
Then to create a periodic task using this schedule, use the same approach as
175190
the interval-based periodic task earlier in this document, but instead
176-
of ``interval=schedule``, specify ``crontab=schedule``::
191+
of ``interval=schedule``, specify ``crontab=schedule``:
192+
193+
.. code-block:: Python
177194
178-
>>> PeriodicTask.objects.create(
179-
... crontab=schedule,
180-
... name='Importing contacts',
181-
... task='proj.tasks.import_contacts',
182-
... )
195+
>>> PeriodicTask.objects.create(
196+
... crontab=schedule,
197+
... name='Importing contacts',
198+
... task='proj.tasks.import_contacts',
199+
... )
183200
184201
Temporarily disable a periodic task
185202
-----------------------------------
186203

187-
You can use the ``enabled`` flag to temporarily disable a periodic task::
204+
You can use the ``enabled`` flag to temporarily disable a periodic task:
205+
206+
.. code-block:: Python
188207
189-
>>> periodic_task.enabled = False
190-
>>> periodic_task.save()
208+
>>> periodic_task.enabled = False
209+
>>> periodic_task.save()
191210
192211
193212
Example running periodic tasks
@@ -202,24 +221,20 @@ Both the worker and beat services need to be running at the same time.
202221

203222
1. Start a Celery worker service (specify your Django project name)::
204223

205-
206-
$ celery -A [project-name] worker --loglevel=info
224+
$ celery -A [project-name] worker --loglevel=info
207225

208226

209227
2. As a separate process, start the beat service (specify the Django scheduler)::
210228

211-
212-
$ celery -A [project-name] beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler
213-
229+
$ celery -A [project-name] beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler
214230

215231
**OR** you can use the -S (scheduler flag), for more options see ``celery beat --help``)::
216232

217-
$ celery -A [project-name] beat -l info -S django
233+
$ celery -A [project-name] beat -l info -S django
218234

219235
Also, as an alternative, you can run the two steps above (worker and beat services)
220236
with only one command (recommended for **development environment only**)::
221237

222-
223238
$ celery -A [project-name] worker --beat --scheduler django --loglevel=info
224239

225240

@@ -234,35 +249,47 @@ Installation
234249
You can install django-celery-beat either via the Python Package Index (PyPI)
235250
or from source.
236251

237-
To install using ``pip``::
252+
To install using ``pip``:
253+
254+
.. code-block:: bash
238255
239-
$ pip install -U django-celery-beat
256+
$ pip install -U django-celery-beat
240257
241258
Downloading and installing from source
242259
--------------------------------------
243260

244261
Download the latest version of django-celery-beat from
245262
http://pypi.python.org/pypi/django-celery-beat
246263

247-
You can install it by doing the following,::
264+
You can install it by doing the following :
265+
266+
.. code-block:: bash
248267
249-
$ tar xvfz django-celery-beat-0.0.0.tar.gz
250-
$ cd django-celery-beat-0.0.0
251-
$ python setup.py build
252-
# python setup.py install
268+
$ tar xvfz django-celery-beat-0.0.0.tar.gz
269+
$ cd django-celery-beat-0.0.0
270+
$ python setup.py build
271+
# python setup.py install
253272
254273
The last command must be executed as a privileged user if
255274
you are not currently using a virtualenv.
256275

257276

258-
After installation, add ``django_celery_beat`` to Django settings file::
277+
After installation, add ``django_celery_beat`` to Django's settings module:
259278

260-
INSTALLED_APPS = [
261-
...,
262-
'django_celery_beat',
263-
]
264279

265-
python manage.py migrate django_celery_beat
280+
.. code-block:: Python
281+
282+
INSTALLED_APPS = [
283+
...,
284+
'django_celery_beat',
285+
]
286+
287+
288+
Run the ``django_celery_beat`` migrations using:
289+
290+
.. code-block:: bash
291+
292+
$ python manage.py migrate django_celery_beat
266293
267294
268295
Using the development version
@@ -272,17 +299,21 @@ With pip
272299
~~~~~~~~
273300

274301
You can install the latest snapshot of django-celery-beat using the following
275-
pip command::
302+
pip command:
303+
304+
.. code-block:: bash
276305
277-
$ pip install https://github.com/celery/django-celery-beat/zipball/master#egg=django-celery-beat
306+
$ pip install https://github.com/celery/django-celery-beat/zipball/master#egg=django-celery-beat
278307
279308
280309
Developing django-celery-beat
281310
-----------------------------
282311

283-
To spin up a local development copy of django-celery-beat with Django admin at http://127.0.0.1:58000/admin/ run::
312+
To spin up a local development copy of django-celery-beat with Django admin at http://127.0.0.1:58000/admin/ run:
313+
314+
.. code-block:: bash
284315
285-
$ docker-compose up --build
316+
$ docker-compose up --build
286317
287318
Log-in as user ``admin`` with password ``admin``.
288319

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@ def reqs(*f):
107107

108108
if os.path.exists('README.rst'):
109109
long_description = codecs.open('README.rst', 'r', 'utf-8').read()
110+
long_description_content_type = 'text/x-rst'
110111
else:
111112
long_description = 'See http://pypi.python.org/pypi/%s' % (NAME,)
113+
long_description_content_type = 'text/markdown'
112114

113115
# -*- %%% -*-
114116

@@ -132,7 +134,7 @@ def run_tests(self):
132134
version=meta['version'],
133135
description=meta['doc'],
134136
long_description=long_description,
135-
long_description_content_type='text/markdown',
137+
long_description_content_type=long_description_content_type,
136138
keywords='django celery beat periodic task database',
137139
author=meta['author'],
138140
author_email=meta['contact'],

t/unit/test_admin.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
from itertools import combinations
1+
import pytest
2+
from django.core.exceptions import ValidationError
23
from django.test import TestCase
4+
from itertools import combinations
35

46
from django_celery_beat.admin import PeriodicTaskAdmin
57
from django_celery_beat.models import \
@@ -9,9 +11,9 @@
911
IntervalSchedule, \
1012
SolarSchedule, \
1113
ClockedSchedule
12-
from django.core.exceptions import ValidationError
1314

1415

16+
@pytest.mark.django_db()
1517
class ActionsTests(TestCase):
1618

1719
@classmethod
@@ -76,6 +78,7 @@ def test_toggle_action_all_disabled(self):
7678
self.assertTrue(e3)
7779

7880

81+
@pytest.mark.django_db()
7982
class ValidateUniqueTests(TestCase):
8083

8184
def test_validate_unique_raises_if_schedule_not_set(self):

0 commit comments

Comments
 (0)