Commit 52e88645 authored by Rafal's avatar Rafal

Add user_promotions

parent d76a29f1
...@@ -15,4 +15,6 @@ class Command(BaseCommand): ...@@ -15,4 +15,6 @@ class Command(BaseCommand):
call_command('init_promotionsets') call_command('init_promotionsets')
call_command('init_localizations') call_command('init_localizations')
call_command('init_places') call_command('init_places')
call_command('init_userpromotions')
from django.contrib.auth.models import User
from App.loyaltyMe.models import Place, Promotion
from App.user.models import UserPromotion
__author__ = 'Rafal'
from django.core.management.base import BaseCommand
class Command(BaseCommand):
help = 'Closes the specified poll for voting'
def handle(self, *args, **options):
user_promotions = [
# user = models.ForeignKey(User, related_name='user_promotion',
# blank=False, null=False, default=None)
# place = models.ForeignKey(Place)
# promotion = models.ForeignKey(Promotion)
# status = models.CharField(max_length=30)
# code = models.CharField(max_length=20)
['Klaudia', 'Kocia Kawiarnia', '10%', 'used' ,'abcd'],
['Klaudia', 'Kafcia u Olczaka', '20%', 'in_progress', None],
['Antek', 'Kafcia u Olczaka', '50%', 'in_progress', None],
['admin', 'Kafcia u Olczaka', '10%', 'in_progress', None],
]
print("Creating Awards")
for user_promotion in user_promotions:
print('Create {0}'.format(user_promotion[0]))
user = User.objects.filter(username=user_promotion[0])[0]
place = Place.objects.filter(name=user_promotion[1])[0]
promotion = Promotion.objects.filter(name=user_promotion[2])[0]
new_user_promotion = UserPromotion.objects.create(user=user, place=place, promotion=promotion, status=user_promotion[3], code=user_promotion[4])
new_user_promotion.save()
\ No newline at end of file
...@@ -5,6 +5,8 @@ from django.dispatch import receiver ...@@ -5,6 +5,8 @@ from django.dispatch import receiver
from rest_framework.authtoken.models import Token from rest_framework.authtoken.models import Token
# Create your models here. # Create your models here.
from App.loyaltyMe.models import Place, Promotion
@receiver(post_save, sender=User) @receiver(post_save, sender=User)
def create_auth_token(sender, instance=None, created=False, **kwargs): def create_auth_token(sender, instance=None, created=False, **kwargs):
...@@ -16,3 +18,12 @@ class UserProfile(models.Model): ...@@ -16,3 +18,12 @@ class UserProfile(models.Model):
blank=False, null=False, default=None) blank=False, null=False, default=None)
photo = models.ImageField() photo = models.ImageField()
class UserPromotion(models.Model):
user = models.ForeignKey(User, related_name='user_promotion',
blank=False, null=False, default=None)
place = models.ForeignKey(Place)
promotion = models.ForeignKey(Promotion)
status = models.CharField(max_length=30)
code = models.CharField(max_length=20, null=True)
...@@ -4,7 +4,8 @@ from django.shortcuts import get_object_or_404 ...@@ -4,7 +4,8 @@ from django.shortcuts import get_object_or_404
from rest_framework import serializers from rest_framework import serializers
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from App.user.models import UserProfile from App.loyaltyMe.serializers import PlaceSerializer, PromotionSerializer
from App.user.models import UserProfile, UserPromotion
class UserSerializer(serializers.HyperlinkedModelSerializer): class UserSerializer(serializers.HyperlinkedModelSerializer):
...@@ -48,3 +49,13 @@ class AuthTokenSerializerByEmail(serializers.Serializer): ...@@ -48,3 +49,13 @@ class AuthTokenSerializerByEmail(serializers.Serializer):
attrs['user'] = user attrs['user'] = user
return attrs return attrs
class UserPromotionSerializer(serializers.HyperlinkedModelSerializer):
user = UserSerializer()
place = PlaceSerializer()
promotion = PromotionSerializer()
class Meta:
model = UserPromotion
fields = ('url', 'id', 'user', 'place', 'promotion', 'status', 'code')
...@@ -13,8 +13,10 @@ from rest_framework.response import Response ...@@ -13,8 +13,10 @@ from rest_framework.response import Response
from rest_framework import viewsets, permissions from rest_framework import viewsets, permissions
from rest_framework.views import APIView from rest_framework.views import APIView
from App.user.models import UserProfile from App.loyaltyMe.serializers import LocalizationSerializer
from App.user.serializers import UserSerializer, AuthTokenSerializerByEmail, UserProfileSerializer from App.user.models import UserProfile, UserPromotion
from App.user.serializers import UserSerializer, AuthTokenSerializerByEmail, UserProfileSerializer, \
UserPromotionSerializer
from rest_framework import parsers, renderers from rest_framework import parsers, renderers
class UserViewSet(viewsets.ModelViewSet): class UserViewSet(viewsets.ModelViewSet):
...@@ -73,3 +75,18 @@ class ObtainAuthToken(APIView): ...@@ -73,3 +75,18 @@ class ObtainAuthToken(APIView):
obtain_auth_token = ObtainAuthToken.as_view() obtain_auth_token = ObtainAuthToken.as_view()
class UserPromotionViewSet(viewsets.ModelViewSet):
"""
API for articles
"""
queryset = UserPromotion.objects.all()
serializer_class = UserPromotionSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
def list(self, request):
user = request.user
queryset = UserPromotion.objects.filter(user=user)
page = self.paginate_queryset(queryset)
serializer = UserPromotionSerializer(page, many=True, context={'request': request})
return self.get_paginated_response(serializer.data)
\ No newline at end of file
...@@ -26,8 +26,9 @@ from App.loyaltyMe import views as loyalty_views ...@@ -26,8 +26,9 @@ from App.loyaltyMe import views as loyalty_views
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register(r'user', user_views.UserViewSet) router.register(r'user', user_views.UserViewSet)
router.register(r'user_profile', user_views.UserProfileViewSet) router.register(r'user_profile', user_views.UserProfileViewSet)
router.register(r'user_promotions', user_views.UserPromotionViewSet)
router.register(r'products', loyalty_views.ProductViewSet) router.register(r'products', loyalty_views.ProductViewSet)
router.register(r'offerts', loyalty_views.OfferViewSet) router.register(r'offers', loyalty_views.OfferViewSet)
router.register(r'promotions', loyalty_views.PromotionViewSet) router.register(r'promotions', loyalty_views.PromotionViewSet)
router.register(r'promotion_set', loyalty_views.PromotionSetViewSet) router.register(r'promotion_set', loyalty_views.PromotionSetViewSet)
router.register(r'places', loyalty_views.PlaceViewSet) router.register(r'places', loyalty_views.PlaceViewSet)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment