Browse Source

上传文件至 '后端/fridge'

master
李思涵 2 years ago
parent
commit
ec827e135d
7 changed files with 108 additions and 0 deletions
  1. BIN
      后端/fridge/__init__.py
  2. +3
    -0
      后端/fridge/admin.py
  3. +6
    -0
      后端/fridge/apps.py
  4. +13
    -0
      后端/fridge/models.py
  5. +3
    -0
      后端/fridge/tests.py
  6. +26
    -0
      后端/fridge/urls.py
  7. +57
    -0
      后端/fridge/views.py

BIN
后端/fridge/__init__.py View File


+ 3
- 0
后端/fridge/admin.py View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

+ 6
- 0
后端/fridge/apps.py View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class FridgeConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'fridge'

+ 13
- 0
后端/fridge/models.py View File

@ -0,0 +1,13 @@
from django.db import models
from general.models import *
class FoodStock(models.Model):
type = models.ForeignKey(FoodType, on_delete= models.CASCADE)
amount = models.IntegerField(default=1)
unit = models.CharField(max_length=15,default='')
percentage = models.IntegerField(default=100)
date = models.DateTimeField(auto_now_add=True)
owner_id = models.CharField(max_length=40)
is_active = models.BooleanField(default=True)

+ 3
- 0
后端/fridge/tests.py View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

+ 26
- 0
后端/fridge/urls.py View File

@ -0,0 +1,26 @@
"""yicanyishi URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('new-food/', views.add_new_food),
path('search/', views.search_food_type),
path('food-stock/', views.get_food_stock),
path('food-stock/change/', views.change_food_stock)
]

+ 57
- 0
后端/fridge/views.py View File

@ -0,0 +1,57 @@
from django.db.models import Q
from fridge.models import *
from general.models import *
from general.views import *
from datetime import datetime, timedelta
def add_new_food(request):
openid = request.META['HTTP_X_WX_OPENID']
name = request.POST['name']
kind = request.POST['kind']
amount = request.POST['amount']
unit = request.POST['unit']
food_type = FoodType.objects.get(name=name, kind=kind)
obj = FoodStock.objects.create(type=food_type, amount=amount, unit=unit, owner_id=openid)
obj.date += timedelta(hours=8)
obj.save()
return JsonResponse({'message': "success"})
def search_food_type(request):
content = request.GET['content']
print(content)
results = FoodType.objects.filter(Q(name__contains=content) | Q(kind__contains=content)).values()
results = process_json(results)
return JsonResponse({'result': results})
def get_food_stock(request):
openid = request.META['HTTP_X_WX_OPENID']
day_limit = int(request.GET['day_limit'])
results = FoodStock.objects.filter(owner_id=openid, is_active=1).values()
for item in results:
del item['owner_id']
del item['is_active']
item['kind'] = FoodType.objects.get(id=item['type_id']).kind
item['name'] = FoodType.objects.get(id=item['type_id']).name
item['days'] = (datetime.now()+timedelta(hours=8)-item['date']).days
item['date'] = str(item['date'])[:16]
item['outdated'] = 1 if item['days'] >= day_limit else 0
del item['type_id']
results = process_json(results)
return JsonResponse({'result': results})
def change_food_stock(request):
openid = request.META['HTTP_X_WX_OPENID']
stock_id = request.POST['id']
new_percentage = request.POST['percentage']
stock = FoodStock.objects.filter(id=stock_id)
stock.update(percentage=new_percentage)
if new_percentage == '0':
stock.update(is_active=0)
return JsonResponse({'message': 'Successfully updated!'})

Loading…
Cancel
Save