Skip to content

Commit 677a523

Browse files
committed
Fix delete bug
1 parent 79db0df commit 677a523

14 files changed

+144
-1
lines changed

api/admin.py

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from .models.user import User
55
from .models.mango import Mango
6+
from prompt.models import Prompt
67

78
class UserAdmin(BaseUserAdmin):
89
ordering = ['id']
@@ -40,3 +41,4 @@ class UserAdmin(BaseUserAdmin):
4041
# class to format the pages:
4142
admin.site.register(User, UserAdmin)
4243
admin.site.register(Mango)
44+
admin.site.register(Prompt)

poco_grow/settings.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
INSTALLED_APPS = [
6666
# Our custom apps
6767
'api',
68+
'prompt',
6869
# DRF
6970
'rest_framework',
7071
'rest_framework.authtoken',
@@ -162,7 +163,7 @@
162163

163164
USE_L10N = True
164165

165-
USE_TZ = True
166+
USE_TZ = False
166167

167168

168169
# Static files (CSS, JavaScript, Images)

poco_grow/urls.py

+1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919
urlpatterns = [
2020
path('admin/', admin.site.urls),
2121
path('', include('api.urls')),
22+
path('prompt/', include('prompt.urls'))
2223
]

prompt/060_insert_into_prompts.sql

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
INSERT INTO prompt_prompt(content, created_at, updated_at) VALUES
2+
('List 3 things you love about your current surroundings', 'now()', 'now()');

prompt/__init__.py

Whitespace-only changes.

prompt/admin.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

prompt/apps.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class PromptConfig(AppConfig):
5+
name = 'prompt'

prompt/migrations/0001_initial.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Generated by Django 3.0 on 2021-07-15 18:26
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
initial = True
9+
10+
dependencies = [
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name='Prompt',
16+
fields=[
17+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18+
('content', models.CharField(max_length=100)),
19+
('created_at', models.DateTimeField(auto_now_add=True)),
20+
('updated_at', models.DateTimeField(auto_now=True)),
21+
],
22+
),
23+
]

prompt/migrations/__init__.py

Whitespace-only changes.

prompt/models.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from django.db import models
2+
3+
class Prompt(models.Model):
4+
"""Prompt model"""
5+
content = models.CharField(max_length=100)
6+
created_at = models.DateTimeField(auto_now_add=True)
7+
updated_at = models.DateTimeField(auto_now=True)
8+
9+
def __str__(self):
10+
"""Return a string representation of a prompt"""
11+
return f'{self.content}'
12+
13+
def as_dict(self):
14+
"""Return a dictionary representation of a prompt"""
15+
return {
16+
'id': self.id,
17+
'content': self.content,
18+
'created_at': self.created_at,
19+
'updated_at': self.updated_at
20+
}

prompt/serializers.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from rest_framework import serializers
2+
from .models import Prompt
3+
4+
class PromptSerializer(serializers.ModelSerializer):
5+
class Meta:
6+
model = Prompt
7+
fields = '__all__'
8+
9+
# class PromptReadSerializer(serializers.ModelSerializer):
10+
# class Meta:
11+
# model = Prompt
12+
# fields = '__all__'

prompt/tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

prompt/urls.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from django.urls import path
2+
# from . import views
3+
from .views import Prompts, PromptDetail
4+
5+
urlpatterns = [
6+
path('', Prompts.as_view(), name='prompts'),
7+
path('<int:pk>/', PromptDetail.as_view(), name='prompt_detail'),
8+
]
9+
10+
# urlpatterns = [
11+
# path('', views.index, name='index')
12+
# ]

prompt/views.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from django.shortcuts import get_object_or_404
2+
from rest_framework.views import APIView
3+
from rest_framework.response import Response
4+
from rest_framework import status
5+
from .models import Prompt
6+
from .serializers import PromptSerializer
7+
# from .serializers import PromptReadSerializer
8+
9+
10+
# from django.http import JsonResponse
11+
12+
class Prompts(APIView):
13+
permission_classes=()
14+
serializer_class=PromptSerializer
15+
16+
def get(self, request):
17+
"""Index Request"""
18+
prompts = Prompt.objects.all()
19+
data = PromptSerializer(prompts, many=True).data
20+
return Response({"prompt": data})
21+
# prompts = Prompt.objects.filter(owner=request.user.id)
22+
23+
def post(self, request):
24+
"""Create a Prompt"""
25+
serializer = PromptSerializer(data=request.data)
26+
if serializer.is_valid():
27+
serializer.save()
28+
return Response(serializer.data, status=status.HTTP_201_CREATED)
29+
else:
30+
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
31+
32+
33+
class PromptDetail(APIView):
34+
permission_classes=()
35+
def get(self, request, pk):
36+
"""Show one Prompt"""
37+
prompt = get_object_or_404(Prompt, pk=pk)
38+
39+
# if not request.user.id == mango.owner.id:
40+
# raise PermissionDenied('Unauthorized, you do not own this mango')
41+
42+
# Run the data through the serializer so it's formatted
43+
data = PromptSerializer(prompt).data
44+
return Response({ 'prompt': data })
45+
46+
def patch(self, request, pk):
47+
"""Update a Prompt"""
48+
prompt = get_object_or_404(Prompt, pk=pk)
49+
ms = PromptSerializer(prompt, data=request.data, partial=True)
50+
if ms.is_valid():
51+
ms.save()
52+
return Response(ms.data)
53+
return Response(ms.errors, status=status.HTTP_400_BAD_REQUEST)
54+
55+
def delete(self, request, pk):
56+
"""Delete Request"""
57+
prompt = get_object_or_404(Prompt, pk=pk)
58+
prompt.delete()
59+
return Response(status=status.HTTP_204_NO_CONTENT)

0 commit comments

Comments
 (0)