Запись в бд

This commit is contained in:
iriska-gri committed 2023-09-08 09:59:00 +03:00
1 parent d4099e264d
commit bc26a6a50d
4 files changed
+83 -25

No files matched your search

BIN
View File
Binary file not shown.
+3 -1
View File
@@ -2,6 +2,8 @@ from django.urls import path
from .views import *
urlpatterns = [
path('baseCriteria/', BaseCriteriaAPI.as_view(), name='baseCriteria'),
path('userComment/', UserCommentAPI.as_view(), name='userComment'),
path('formfeed/', FormFeedAPI.as_view(), name='form'),
path('comment/', CommentAPI.as_view(), name='comment'),
path('formupdate/', FormUpdateAPI.as_view(), name='formupdate'),
]
+41 -7
View File
@@ -1,6 +1,6 @@
from rest_framework.response import Response
from django.shortcuts import render
from rest_framework.generics import ListCreateAPIView
from rest_framework.generics import *
from django.forms.models import model_to_dict
from accounts.models import User
@@ -9,7 +9,31 @@ import datetime
from .serializers import *
from .models import *
class FormFeedAPI(LinkAPIView):
class BaseCriteriaAPI(ListAPIView):
queryset = BaseCriteria.objects.all()
serializer_class = BaseCriteriaSerializer
class UserCommentAPI(ListCreateAPIView):
serializer_class = UserCommentSerializer
def get_queryset(self):
queryset = UserComment.objects.filter(user_id=self.request.user.id, time_create__contains=datetime.date.today())
return queryset
def perform_create(self, serializer):
serializer.save(user_id=self.request.user.id, comment=self.request.data['comment']) # updated after feedback
# class UserCommentUpdateAPI(UpdateAPIView):
# queryset = UserComment.objects.all()
# serializer_class = UserCommentSerializer
class FormFeedAPI(ListCreateAPIView):
def get(self, request):
serializer_class1 = BaseCriteriaSerializer
serializer_class2 = UserCommentSerializer
@@ -19,13 +43,23 @@ class FormFeedAPI(LinkAPIView):
baseCriteria = serializer_class1(queryset1, many=True)
userComment = serializer_class2(queryset2, many=True)
return Response({'baseCriteria':baseCriteria.data, 'userComment':userComment.data})
class CommentAPI(ListCreateAPIView):
def post(self, request):
comid = UserComment.objects.create(user_id = request.user.id, comment = request.data['comment'])
comid = UserComment.objects.update_or_create(user_id = request.user.id, comment = request.data['comment'])
for x in request.data['ratingstatus']:
RatingStatus.objects.create(crit_id=x['id'], rating = x['rating'], usercom_id= comid.id)
RatingStatus.objects.update_or_create(crit_id=x['id'], rating = x['rating'], usercom_id= comid.id)
return Response(print('Запись создана в таблицу feedback_usercomment'))
class FormUpdateAPI(UpdateAPIView):
def put(self, request):
print(request.data, 'pr------------------------------------ovetka')
return Response(print('Запись в таблице feedback_usercomment обновлена'))
+39 -17
View File
@@ -7,12 +7,12 @@
<v-container>
<v-row>
<v-col class="flex-grow-0 py-0 text-no-wrap align-self-center text-button">
Оцените сайт
Оцените сайт
</v-col>
<v-divider/>
</v-row>
<v-row
v-for="(x, key) in tab_baseCriteria"
v-for="(x, key) in t_baseCriteria"
:key = key
>
<v-col class="flex-grow-0 pb-0">
@@ -52,6 +52,7 @@
</v-card-text>
<v-card-actions class="pt-0">
<small v-if="average < 5 || comment.length > 0" class="pl-5">
Вы оценили сайт на <span class="font-weight-black">{{average}}</span> {{myLabel()}}
</small>
<v-spacer/>
@@ -74,7 +75,7 @@
color="blue-darken-1"
variant="text"
text="Обновить"
@click="feedAll"
@click="proba"
/>
</v-card-actions>
</v-card>
@@ -94,30 +95,43 @@ export default {
setup(props, context) {
const comment = ref("")
const tab_baseCriteria = ref([])
const t_baseCriteria = ref([])
const tab_userComment = ref([])
const t_userComment = ref([])
onMounted(() => {
axiosApiInstance.get('api/feedback/formfeed/')
.then(data => {
tab_baseCriteria.value = data.data.baseCriteria
tab_userComment.value = data.data.userComment
}
)
axiosApiInstance.get('api/feedback/baseCriteria/')
.then(data => {
t_baseCriteria.value = data.data.results
})
axiosApiInstance.get(`api/feedback/userComment/`)
.then(data => {
t_userComment.value = data.data.results
})
})
function feedAll() {
const feedObj = {}
// feedObj.ratingstatus = ratingStatus.value.decFeed
feedObj.ratingstatus = ratingStatus.value.decFeed
feedObj.comment = comment.value
axiosApiInstance.post('api/feedback/comment/', feedObj)
axiosApiInstance.post('api/feedback/formfeed/', feedObj)
comment.value=""
tab_baseCriteria.value.map(e=>e.rating=5)
t_baseCriteria.value.map(e=>e.rating=5)
}
function feedUp() {
const feedObj = {}
feedObj.comment = comment.value
feedObj.ratingstatus = ratingStatus.value.decFeed
axiosApiInstance.put('api/feedback/formupdate/', feedObj)
}
const ratingStatus = computed(() => {
const decFeed = []
tab_baseCriteria.value.map(e =>
t_baseCriteria.value.map(e =>
{
const { id, rating } = {...e}
decFeed.push({id:id, rating:rating})
@@ -126,8 +140,8 @@ export default {
});
const average = computed(()=> {
const sum = tab_baseCriteria.value.reduce((total, obj)=> total + obj.rating, 0)
const sumlen = sum/tab_baseCriteria.value.length
const sum = t_baseCriteria.value.reduce((total, obj)=> total + obj.rating, 0)
const sumlen = sum/t_baseCriteria.value.length
const ukr = Number.isInteger(sumlen)?sumlen:sumlen.toFixed(1)
return ukr
})
@@ -149,7 +163,15 @@ export default {
context.emit('menusClose')
}
return {tab_baseCriteria, tab_userComment, comment, average, feedAll, menusCloses, myLabel}
function proba() {
const feedObj = {}
feedObj.comment = comment.value
axiosApiInstance.post(`api/feedback/userComment/`, feedObj)
// console.log(t_userComment.value, 't_userComment.value');
}
return {t_baseCriteria, tab_userComment, comment, average, feedAll, feedUp, menusCloses, myLabel, proba}
}