A django-rest-framework application that provides many varieties of coupons
This project depends on:
djangorestframework
django-filter
-
Install
drf-coupons
via pip:$ pip install drf-coupons
-
Add
'rest_framework'
toINSTALLED_APPS
insettings.py
. -
Add
'coupons'
toINSTALLED_APPS
insettings.py
. -
Migrate database:
$ python manage.py migrate
Note: this package was not developed to be compatible side-by-side with django-coupons
, as they serve very similar needs.
-
Specify permissions for interacting with coupon endpoints.
You can specify a list of groups that can perform specific actions against the coupons, such as restricting who can create or list coupons.
By default all endpoints are open except list.
retrieve
does not allow restriction because it doesn't generally need to support such permissions.patch
is not supported as an endpoint and is therefore also not in theCOUPON_PERMISSIONS
.COUPON_PERMISSIONS = { 'CREATE': ['groupa', 'groupb'], 'LIST': ['groupa'], 'DELETE': ['groupb'], 'UPDATE': ['groupb'], 'REDEEMED': ['groupc'], }
You don't need to specify every endpoint in the list and can provide an empty list for an endpoint.
The groups specified for
REDEEMED
are used in bothGET /coupon/{pk}/redeemed
andGET /redeemed
.The groups specified for
DELETE
are used in bothDELETE /coupon/{pk}
andDELETE /redeemed/{pk}
. -
Communicate with coupon endpoints.
You can place the urls into a subpath, however you like:
urlpatterns = [ # just adding here, but you can put into a subordinate path. url(r'^', include('coupons.urls')), ]
As stated above, by default any user in the system can touch any of the below endpoints, except where specified in bold.
Endpoint Details GET /coupon
List all coupons in the system, only superuser or in group can see all. GET /coupon/{pk}
Retrieve details about a coupon by database id POST /coupon
Create a new coupon PUT /coupon/{pk}
Update a coupon DELETE /coupon/{pk}
Delete a coupon PUT /coupon/{pk}/redeem
Redeem a coupon by database id GET /coupon/{pk}/redeemed
List all times specified coupon was redeemed, superuser or group member can see all PATCH /coupon/{pk}
Not supported GET /redeemed
List all redeemed instances, filter-able only superuser or in group can do see all
GET /coupon
supports querying by coupon code, and filter by user
, bound
, type
or by ranges of discount via max_value
, min_value
GET /redeemed
supports filtering by user
.
There are two objects provided:
-
Coupon
- allows you to specify the properties of the coupon itself.Field Type Meaning code
string
the code for the coupon, case insensitive code_l
string
automatically set lowercase version of the coupon code type
string
either percent
orvalue
, how thevalue
field should be interpretedexpires
datetime
optional field to set when the coupon expires value
decimal
the value for the coupon, such as 100
or0.50
bound
boolean
if true
then the coupon can only be used by the specified user in theuser
fielduser
foreign key
set when bound to point to the user repeat
integer
if 0
the coupon can be used infinitely, otherwise it specifies how often any system user can use it -
ClaimedCoupon
- allows you to track whenever a user redeems a coupon.Field Type Meaning redeemed
datetime
automatically set when a coupon is redeemed coupon
foreign key
automatically set to point at the coupon when redeemed user
foreign key
automatically set to point at the coupon when redeemed
It supports the following variations of coupons:
- Coupons can be a value, or a percentage.
- They can be bound to a specific user in the system.
- They can be single-use:
- per user (a pre-specified user can use it once),
case I
- globally (any user in the system can use it, but only once)
case II
- per user (a pre-specified user can use it once),
- They can be infinite:
- per a specific user (a pre-specified user can use it repeatedly infinitely)
case III
- infinite globally (any user can use it repeatedly infinitely)
case IV
- per a specific user (a pre-specified user can use it repeatedly infinitely)
- They can be used a specific number of times:
- per user (a pre-specified user can use it a specific number of times)
case V
- globally (any user can use it a specific number of times)
case VI
- per user (a pre-specified user can use it a specific number of times)
- (They can be used by a specific list of users?) ... maybe later.
You create coupons in the system that are then claimed by users.
The unit-tests should automatically be run when you run python manage.py test
and they are isolated.
If you'd like to contribute, please fork, and develop, branch from the development
branch to and submit a pull request when ready.