Skip to content
1 change: 1 addition & 0 deletions file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
37 changes: 19 additions & 18 deletions server/farmapp/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from django.db import models

# Create your models here.
from django.db import models
from django.contrib.auth.models import AbstractBaseUser
from django.utils import timezone


# 1. User Model
class User(AbstractBaseUser):
ROLE_CHOICES = [
Expand All @@ -17,23 +15,27 @@ class User(AbstractBaseUser):
name = models.CharField(max_length=255)
email = models.EmailField(unique=True)
password = models.CharField(max_length=255) # Password field
password = models.CharField(max_length=255) # Password field
role = models.CharField(max_length=10, choices=ROLE_CHOICES)
phone = models.CharField(max_length=15, null=True, blank=True)
address = models.CharField(max_length=255, null=True, blank=True)
createdAt = models.DateTimeField(auto_now_add=True)
updatedAt = models.DateTimeField(auto_now=True)
last_login = models.DateTimeField(null=True, blank=True)
reset_password_token = models.CharField(max_length=36, null=True, blank=True) # UUID token
reset_token_created_at = models.DateTimeField(null=True, blank=True) # Token timestamp
last_login = models.DateTimeField(null=True, blank=True) # Last login timestamp
is_active = models.BooleanField(default=True)

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name']

def __str__(self):
return self.name


class Meta:
db_table = 'User'
managed = True # Set to True or remove to allow migrations

managed = True

# 2. Farmer Model
class Farmer(models.Model):
Expand All @@ -44,7 +46,6 @@ class Farmer(models.Model):
]

farmerId = models.AutoField(primary_key=True)
# user = models.OneToOneField(User, on_delete=models.CASCADE, limit_choices_to={'role': 'farmer'})
userId = models.OneToOneField(User, on_delete=models.CASCADE, limit_choices_to={'role': 'farmer'}, related_name='farmer', db_column='userId')
farmName = models.CharField(max_length=255)
location = models.CharField(max_length=255)
Expand All @@ -59,7 +60,7 @@ def __str__(self):

class Meta:
db_table = 'Farmer'
managed = False
managed = True

# 3. Product Model
class Product(models.Model):
Expand All @@ -70,7 +71,6 @@ class Product(models.Model):
]

productId = models.AutoField(primary_key=True)
# farmer = models.ForeignKey(Farmer, on_delete=models.CASCADE)
farmer = models.ForeignKey(Farmer, on_delete=models.CASCADE, db_column='farmerId')
productName = models.CharField(max_length=255)
description = models.TextField()
Expand All @@ -87,7 +87,7 @@ def __str__(self):

class Meta:
db_table = 'Product'
managed = False
managed = True

# 4. Order Model
class Order(models.Model):
Expand All @@ -99,7 +99,7 @@ class Order(models.Model):
]

# orderId = models.AutoField(primary_key=True)
# customer = models.ForeignKey(User, on_delete=models.CASCADE, limit_choices_to={'role': 'customer'})
# customer = models.ForeignKey(User, on_delete=models.CASCADE, db_column='customerId')
orderId = models.AutoField(primary_key=True) # Primary key field
customer = models.ForeignKey(User, on_delete=models.CASCADE, db_column='customerId') # ForeignKey to User model
orderItems = models.JSONField() # Store an array of products, including productId, quantity, and price
Expand All @@ -108,13 +108,14 @@ class Order(models.Model):
createdAt = models.DateTimeField(auto_now_add=True)
updatedAt = models.DateTimeField(auto_now=True)
deliveryDate = models.DateTimeField(null=True, blank=True)
deliveryDate = models.DateTimeField(null=True, blank=True)

def __str__(self):
return self.orderId
return str(self.orderId)

class Meta:
db_table = 'Order'
managed = False
managed = True

# 5. Cart Model (for Customers)
class Cart(models.Model):
Expand All @@ -126,11 +127,11 @@ class Cart(models.Model):
updatedAt = models.DateTimeField(auto_now=True)

def __str__(self):
return self.customer
return str(self.cartId)

class Meta:
db_table = 'Cart'
managed = False
managed = True

# 6. Review Model
class Review(models.Model):
Expand All @@ -143,11 +144,11 @@ class Review(models.Model):
updatedAt = models.DateTimeField(auto_now=True)

def __str__(self):
return self.reviewId
return str(self.reviewId)

class Meta:
db_table = 'Review'
managed = False
managed = True

# 7. Admin Activity Log Model
class AdminActivityLog(models.Model):
Expand All @@ -162,4 +163,4 @@ def __str__(self):

class Meta:
db_table = 'AdminActivityLog'
managed = False
managed = True
59 changes: 36 additions & 23 deletions server/farmapp/serializers.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,43 @@
from rest_framework import serializers
from .models import Order, Product
from django.contrib.auth.hashers import make_password
from .models import User, Order, Product

# User Management Serializers
class RegisterSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True)

class Meta:
model = User
fields = ['name', 'email', 'password', 'role', 'phone', 'address']

def create(self, validated_data):
validated_data['password'] = make_password(validated_data['password'])
return super().create(validated_data)

class LoginSerializer(serializers.Serializer):
email = serializers.EmailField()
password = serializers.CharField(write_only=True)

class ChangePasswordSerializer(serializers.Serializer):
old_password = serializers.CharField(write_only=True)
new_password = serializers.CharField(write_only=True)

class ForgotPasswordSerializer(serializers.Serializer):
email = serializers.EmailField()

class ResetPasswordSerializer(serializers.Serializer):
token = serializers.CharField()
new_password = serializers.CharField(write_only=True)

class UserStatusSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['userId', 'name', 'email', 'is_active', 'role']

# Order Management Serializers
class OrderDetailSerializer(serializers.ModelSerializer):
"""
Serializer for displaying detailed information about an order.

Fields:
- orderId: The unique ID of the order.
- customerName: Name of the customer who placed the order.
- productName: Name of the product in the order.
- quantity: Quantity of the product ordered.
- totalPrice: Total price of the order.
- orderStatus: Status of the order.
- dateOrdered: The date the order was placed.
- deliveryDate: The expected delivery date for the order.
- address: Address of the customer.
- phone: Contact phone number of the customer.
"""
orderId = serializers.IntegerField() # Removed 'source' argument
customerName = serializers.CharField(source='customer.name')
Expand All @@ -31,7 +53,6 @@ class OrderDetailSerializer(serializers.ModelSerializer):
def get_productName(self, obj):
"""
Retrieves the name of the product from the Product model based on productId.
Assumes that each order item is structured to include 'productId' in orderItems.
"""
product_ids = [item['productId'] for item in obj.orderItems]
products = Product.objects.filter(productId__in=product_ids)
Expand All @@ -40,25 +61,17 @@ def get_productName(self, obj):
def get_quantity(self, obj):
"""
Retrieves the quantity of the product ordered.
Assumes 'quantity' is part of each entry in orderItems.
"""
return sum(item.get('quantity', 0) for item in obj.orderItems)

class Meta:
model = Order
fields = ['orderId', 'customerName', 'productName', 'phone', 'orderItems', 'totalPrice', 'orderStatus', 'address', 'quantity', 'dateOrdered', 'deliveryDate']




class OrderStatusUpdateSerializer(serializers.ModelSerializer):
"""
Serializer for updating the status of an order.

Fields:
- status: New status for the order, validated against allowed transitions.
"""

status = serializers.ChoiceField(choices=Order.ORDER_STATUS_CHOICES)

def validate_status(self, value):
Expand All @@ -80,4 +93,4 @@ def validate_status(self, value):

class Meta:
model = Order
fields = ['status']
fields = ['status']
31 changes: 24 additions & 7 deletions server/farmapp/urls.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
from django.urls import path
from .views import DeleteReviewView, OrderDetailView, UpdateOrderStatusView, DeleteOrderView
from .views import (
RegisterView,
LoginView,
ForgotPasswordView,
ResetPasswordView,
ChangePasswordView,
CheckStatusView,
SignoutView,
DeleteReviewView,
OrderDetailView,
UpdateOrderStatusView,
DeleteOrderView,
)

urlpatterns = [
# URL for retrieving order details
# Auth-related endpoints
path('farmer/auth/register/', RegisterView.as_view(), name='register'),
path('farmer/auth/login/', LoginView.as_view(), name='login'),
path('farmer/auth/forgot-password/', ForgotPasswordView.as_view(), name='forgot-password'),
path('farmer/auth/reset-password/', ResetPasswordView.as_view(), name='reset-password'),
path('farmer/auth/change-password/', ChangePasswordView.as_view(), name='change-password'),
path('farmer/auth/check-status/', CheckStatusView.as_view(), name='check-status'),
path('farmer/auth/signout/', SignoutView.as_view(), name='signout'),

# Order-related endpoints
path("api/farmer/<int:farmerId>/orders/<int:orderId>", OrderDetailView.as_view(), name="order-detail"),

# URL for updating order status
path("api/farmer/<int:farmerId>/orders/<int:orderId>/status", UpdateOrderStatusView.as_view(), name="update-order-status"),

# URL for deleting an order (or marking it as cancelled, depending on logic)
path("api/farmer/<int:farmerId>/orders/<int:orderId>/delete", DeleteOrderView.as_view(), name="delete-order"),

# URL for deleting a review
# Review-related endpoints
path("api/product/<int:productId>/reviews/<int:reviewId>", DeleteReviewView.as_view(), name="delete-review"),
]
Loading