File size: 5,581 Bytes
f84bf8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from django.contrib.auth.decorators import login_required, user_passes_test
from django.shortcuts import render
import json
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from .models import Tip, Disease, News

# ✅ Check if User is Admin
def is_admin(user):
    return user.is_authenticated and user.is_admin

# -------------------------------------
# ✅ CROP TIPS (ADD, UPDATE, DELETE)
# -------------------------------------
@csrf_exempt
@login_required
@user_passes_test(is_admin)
def add_or_update_tip(request):
    """ POST /admin_dashboard/add_tip """
    if request.method == 'POST':
        try:
            body = json.loads(request.body.decode('utf-8'))
            crop_name = body.get('crop_name')
            crop_tips = body.get('crop_tips')

            tip, created = Tip.objects.update_or_create(
                crop_name=crop_name, defaults={"crop_tips": crop_tips}
            )

            return JsonResponse({"message": "Tip added/updated successfully"}, status=200)
        except json.JSONDecodeError:
            return JsonResponse({"error": "Invalid JSON format"}, status=400)
    return JsonResponse({"error": "Method not allowed"}, status=405)


@csrf_exempt
@login_required
@user_passes_test(is_admin)
def delete_tip(request):
    """ DELETE /admin_dashboard/delete_tip """
    if request.method == 'DELETE':
        try:
            body = json.loads(request.body.decode('utf-8'))
            crop_name = body.get('crop_name')

            try:
                tip = Tip.objects.get(crop_name=crop_name)
                tip.delete()
                return JsonResponse({"message": f"Tip for {crop_name} deleted successfully"}, status=200)
            except Tip.DoesNotExist:
                return JsonResponse({"error": "Tip not found"}, status=404)

        except json.JSONDecodeError:
            return JsonResponse({"error": "Invalid JSON format"}, status=400)
    return JsonResponse({"error": "Method not allowed"}, status=405)

# -------------------------------------
# ✅ DISEASES (ADD, UPDATE, DELETE)
# -------------------------------------
@csrf_exempt
@login_required
@user_passes_test(is_admin)
def add_or_update_disease(request):
    """ POST /admin_dashboard/add_disease """
    if request.method == 'POST':
        try:
            body = json.loads(request.body.decode('utf-8'))
            disease_name = body.get('disease_name')
            cure = body.get('cure')
            commonness = body.get('commonness', '')

            disease, created = Disease.objects.update_or_create(
                disease_name=disease_name, defaults={"cure": cure, "commonness": commonness}
            )

            return JsonResponse({"message": "Disease added/updated successfully"}, status=200)
        except json.JSONDecodeError:
            return JsonResponse({"error": "Invalid JSON format"}, status=400)
    return JsonResponse({"error": "Method not allowed"}, status=405)


@csrf_exempt
@login_required
@user_passes_test(is_admin)
def delete_disease(request):
    """ DELETE /admin_dashboard/delete_disease """
    if request.method == 'DELETE':
        try:
            body = json.loads(request.body.decode('utf-8'))
            disease_name = body.get('disease_name')

            try:
                disease = Disease.objects.get(disease_name=disease_name)
                disease.delete()
                return JsonResponse({"message": f"Disease {disease_name} deleted successfully"}, status=200)
            except Disease.DoesNotExist:
                return JsonResponse({"error": "Disease not found"}, status=404)

        except json.JSONDecodeError:
            return JsonResponse({"error": "Invalid JSON format"}, status=400)
    return JsonResponse({"error": "Method not allowed"}, status=405)

# -------------------------------------
# ✅ NEWS (ADD, UPDATE, DELETE)
# -------------------------------------
@csrf_exempt
@login_required
@user_passes_test(is_admin)
def add_or_update_news(request):
    """ POST /admin_dashboard/add_news """
    if request.method == 'POST':
        try:
            body = json.loads(request.body.decode('utf-8'))
            title = body.get('title')
            subtitle = body.get('subtitle', '')
            content = body.get('content')
            author_name = body.get('author_name')

            news, created = News.objects.update_or_create(
                title=title, defaults={"subtitle": subtitle, "content": content, "author_name": author_name}
            )

            return JsonResponse({"message": "News added/updated successfully"}, status=201)
        except json.JSONDecodeError:
            return JsonResponse({"error": "Invalid JSON format"}, status=400)
    return JsonResponse({"error": "Method not allowed"}, status=405)


@csrf_exempt
@login_required
@user_passes_test(is_admin)
def delete_news(request):
    """ DELETE /admin_dashboard/delete_news """
    if request.method == 'DELETE':
        try:
            body = json.loads(request.body.decode('utf-8'))
            title = body.get('title')

            try:
                news = News.objects.get(title=title)
                news.delete()
                return JsonResponse({"message": f"News '{title}' deleted successfully"}, status=200)
            except News.DoesNotExist:
                return JsonResponse({"error": "News not found"}, status=404)

        except json.JSONDecodeError:
            return JsonResponse({"error": "Invalid JSON format"}, status=400)
    return JsonResponse({"error": "Method not allowed"}, status=405)