Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get product transactions endpoint #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions mongoose_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.core.validators import MaxValueValidator, MinValueValidator
#from django.utils.translation import Trans


class Category(models.Model):
name = models.CharField(max_length=30)
alcoholic = models.BooleanField(default=False)
Expand Down Expand Up @@ -92,6 +93,23 @@ def show_price(self):
class Meta:
verbose_name_plural = "Products"


class NamedTransactionProductTotal(models.Model):
name = models.CharField(max_length=60)
product_id = models.IntegerField()
amount = models.IntegerField()

class Meta:
managed = False

def serialize(self) -> dict:
return {
'name': self.name,
'id': self.product_id,
'amount': self.amount
}


# Every transaction has at least a link with a user, an id and a sum
# Whether this sum has a negative or positive influence on the total credit depends on the type of transaction
class Transaction(models.Model):
Expand Down
1 change: 1 addition & 0 deletions mongoose_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
path('card', views.get_card, name='get_card'),
path('products', views.get_products, name='get_products'),
path('confirm', views.confirm_card, name='confirm_card'),
path('product_transactions', views.get_product_transactions, name="get_product_transactions"),

# POST endpoints
path('transaction', views.create_transaction, name='create_transaction'),
Expand Down
50 changes: 49 additions & 1 deletion mongoose_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from django.conf import settings
from .middleware import authenticated
from .models import CardConfirmation, Category, Card, Product, ProductTransactions, SaleTransaction, User
from .models import CardConfirmation, Category, Card, Product, ProductTransactions, SaleTransaction, User, NamedTransactionProductTotal
from datetime import datetime, date
from django.views.decorators.csrf import csrf_exempt
import requests
Expand Down Expand Up @@ -61,6 +61,54 @@ def get_products(request):
serialized_categories = [c.serialize() for c in categories]
return JsonResponse(serialized_categories, safe=False)

@authenticated
@require_http_methods(['GET'])
def get_product_transactions(request):
"""
Retrieve all historical sale transactions
"""
query_for_date_str = request.GET.get('date')
if query_for_date_str:
query_for_dt = datetime.fromtimestamp(int(query_for_date_str))
else:
query_for_dt = datetime.now()
query_for_date = datetime(query_for_dt.year, query_for_dt.month, query_for_dt.day)

transactions = ProductTransactions.objects.all()

product_counts = {}
product_names = {}

for transaction_product in transactions:
transaction = transaction_product.transaction_id

transaction_dt = datetime(transaction.date.year, transaction.date.month, transaction.date.day)

if transaction_dt != query_for_date:
continue

product = transaction_product.product_id

if product.id in product_counts:
product_counts[product.id] = product_counts[product.id] + 1
else:
product_counts[product.id] = 1

if product.id not in product_names:
product_names[product.id] = product.name

result = []

for product_id in product_counts:
result.append(NamedTransactionProductTotal(
product_id=product_id,
amount=product_counts[product_id],
name=product_names[product_id]
))

serialized_result = [c.serialize() for c in result]
return JsonResponse(serialized_result, safe=False)


# POST endpoints
@csrf_exempt
Expand Down