· 7 years ago · Jun 15, 2018, 09:54 PM
1from django.db import models
2
3
4class Tick(models.Model):
5 temperature = models.DecimalField(decimal_places=2, max_digits=6)
6
7 created_date = models.DateTimeField(auto_now_add=True)
8 modified_date = models.DateTimeField(auto_now=True)
9
10 class Meta:
11 ordering = ['-created_date']
12
13 def __str__(self):
14 return '{} {}'.format(self.temperature, self.created_date)
15
16
17
18from django.contrib import admin
19
20
21from .models import Tick
22
23
24@admin.register(Tick)
25class TickAdmin(admin.ModelAdmin):
26 list_display = ['created_date']
27
28
29
30from django.apps import AppConfig
31
32
33class CoreConfig(AppConfig):
34 name = 'core'
35
36
37
38from rest_framework.serializers import ModelSerializer
39
40from .models import Tick
41
42
43class TickSerializer(ModelSerializer):
44 class Meta:
45 model = Tick
46 fields = ['id', 'temperature', 'created_date']
47
48
49
50from django.conf.urls import url
51
52from .views import (
53 InputView, ListView
54)
55
56app_name = 'core'
57
58urlpatterns = [
59 url(r'^input-data/$', InputView.as_view(), name='tick_create'),
60 url(r'^ticks/$', ListView.as_view(), name='tick_list'),
61]
62
63
64
65
66from rest_framework.views import APIView
67from rest_framework.response import Response
68from rest_framework.renderers import JSONRenderer
69from rest_framework.generics import ListAPIView
70
71from .models import Tick
72from .serialisers import TickSerializer
73
74
75class InputView(APIView):
76
77 def get(self, request, format=None):
78 tick = self.request.GET.get('temp', None)
79 if tick:
80 Tick.objects.create(temperature=tick)
81 return Response({'status': 'CREATED'})
82 else:
83 return Response({'status': 'sooqa bomj'})
84
85
86class ListView(ListAPIView):
87 renderer_classes = [JSONRenderer]
88 queryset = Tick.objects.all()
89 serializer_class = TickSerializer
90
91 def create(request, *args, **kwargs):
92 tick = request.GET.get('temp', None)
93 if tick:
94 Tick.objects.create(temperature=tick)
95 return Response({'status': 'CREATED'})
96
97
98
99
100"""
101Django settings for diplom project.
102
103Generated by 'django-admin startproject' using Django 2.0.6.
104
105For more information on this file, see
106https://docs.djangoproject.com/en/2.0/topics/settings/
107
108For the full list of settings and their values, see
109https://docs.djangoproject.com/en/2.0/ref/settings/
110"""
111
112import os
113
114# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
115BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
116
117
118# Quick-start development settings - unsuitable for production
119# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
120
121# SECURITY WARNING: keep the secret key used in production secret!
122SECRET_KEY = 'ba()xj^l-i4*o479c1^)$6%9+ywky4^(u+$e(5zh3(5-e_zop2'
123
124# SECURITY WARNING: don't run with debug turned on in production!
125DEBUG = True
126
127ALLOWED_HOSTS = ['*']
128
129
130# Application definition
131
132INSTALLED_APPS = [
133 'grappelli',
134 'django.contrib.admin',
135 'django.contrib.auth',
136 'django.contrib.contenttypes',
137 'django.contrib.sessions',
138 'django.contrib.messages',
139 'django.contrib.staticfiles',
140 # third party
141 'rest_framework',
142 'django_extensions',
143 # custom
144 'core',
145 'landing',
146]
147
148MIDDLEWARE = [
149 'django.middleware.security.SecurityMiddleware',
150 'django.contrib.sessions.middleware.SessionMiddleware',
151 'django.middleware.common.CommonMiddleware',
152 'django.middleware.csrf.CsrfViewMiddleware',
153 'django.contrib.auth.middleware.AuthenticationMiddleware',
154 'django.contrib.messages.middleware.MessageMiddleware',
155 'django.middleware.clickjacking.XFrameOptionsMiddleware',
156]
157
158ROOT_URLCONF = 'diplom.urls'
159
160TEMPLATES = [
161 {
162 'BACKEND': 'django.template.backends.django.DjangoTemplates',
163 'DIRS': [
164 os.path.join(BASE_DIR, 'templates'),
165 ],
166 'APP_DIRS': True,
167 'OPTIONS': {
168 'context_processors': [
169 'django.template.context_processors.debug',
170 'django.template.context_processors.request',
171 'django.contrib.auth.context_processors.auth',
172 'django.contrib.messages.context_processors.messages',
173 ],
174 },
175 },
176]
177
178WSGI_APPLICATION = 'diplom.wsgi.application'
179
180
181# Database
182# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
183
184DATABASES = {
185 'default': {
186 'ENGINE': 'django.db.backends.sqlite3',
187 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
188 }
189}
190
191
192# Password validation
193# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
194
195AUTH_PASSWORD_VALIDATORS = [
196 {
197 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
198 },
199 {
200 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
201 },
202 {
203 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
204 },
205 {
206 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
207 },
208]
209
210
211# Internationalization
212# https://docs.djangoproject.com/en/2.0/topics/i18n/
213
214LANGUAGE_CODE = 'en-us'
215
216TIME_ZONE = 'UTC'
217
218USE_I18N = True
219
220USE_L10N = True
221
222USE_TZ = True
223
224
225# Static files (CSS, JavaScript, Images)
226# https://docs.djangoproject.com/en/2.0/howto/static-files/
227
228STATIC_URL = '/static/'
229STATIC_ROOT = 'prod_static/'
230
231MEDIA_URL = '/media/'
232MEDIA_ROOT = 'prod_media/'
233
234# GRAPPELLI SETTINGS
235GRAPPELLI_ADMIN_TITLE = 'ADP (Artem Diplom Project)'
236
237
238STATICFILES_DIRS = [
239 os.path.join(BASE_DIR, 'static'),
240]
241
242
243REST_FRAMEWORK = {
244 'DEFAULT_PERMISSION_CLASSES': [
245 'rest_framework.permissions.IsAuthenticatedOrReadOnly'
246 ],
247 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
248 'PAGE_SIZE': 100,
249}
250
251
252
253from django.conf.urls import url, include
254from django.conf import settings
255from django.conf.urls.static import static
256from django.contrib import admin
257
258
259urlpatterns = [
260 url(r'^admin/', admin.site.urls),
261 url(r'^', include('core.urls', namespace='core')),
262 url(r'^', include('landing.urls', namespace='landing')),
263 url(r'^grappelli/', include('grappelli.urls')), # grappelli URLS
264] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
265
266
267
268"""
269WSGI config for diplom project.
270
271It exposes the WSGI callable as a module-level variable named ``application``.
272
273For more information on this file, see
274https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
275"""
276
277import os
278
279from django.core.wsgi import get_wsgi_application
280
281os.environ.setdefault("DJANGO_SETTINGS_MODULE", "diplom.settings")
282
283application = get_wsgi_application()