Guest User

Untitled

a guest
Dec 31st, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. from django.db import models
  2.  
  3. class ValidationInfo(models.Model):
  4. val_school_id = models.CharField(max_length=5)
  5. val_school_pass = models.CharField(max_length=10)
  6.  
  7. class User(models.Model):
  8. is_authenticated = True
  9. school_name = models.CharField(max_length=50,default="")
  10. school_id = models.CharField(max_length=50,default="")
  11. password = models.CharField(max_length=100)
  12. email = models.CharField(max_length=100)
  13. username = models.CharField(max_length=20)
  14. sign_up_date = models.DateTimeField(auto_now=True)
  15. last_login = models.DateTimeField(auto_now=True)
  16. profilepic = models.CharField(max_length=255, default="")
  17.  
  18.  
  19. class Photo(models.Model):
  20. baseurl = models.CharField(max_length=255)
  21. url = models.CharField(max_length=255)
  22. date_uploaded = models.DateTimeField(auto_now=True)
  23. owner = models.CharField(max_length=20)
  24. likes = models.IntegerField()
  25. caption = models.CharField(max_length=140, default="")
  26. tags = models.IntegerField(default=0)
  27. main_colour = models.CharField(max_length=15, default="")
  28. owner_school = models.CharField(max_length=30, default="")
  29.  
  30.  
  31. class PhotoLikes(models.Model):
  32. postid = models.IntegerField()
  33. liker = models.CharField(max_length=20)
  34.  
  35.  
  36. class Followers(models.Model):
  37. user = models.CharField(max_length=20, default="")
  38. follower = models.CharField(max_length=20, default="")
  39.  
  40. class PhotoTag(models.Model):
  41. photoid = models.IntegerField()
  42. coords = models.CharField(max_length=40)
  43. tagged_user = models.CharField(max_length=20, default="")
  44. tagged_by = models.CharField(max_length=20, default="")
  45.  
  46. class Ajax(forms.Form):
  47. args = []
  48. user = []
  49.  
  50. def __init__(self, *args, **kwargs):
  51.  
  52. self.args = args
  53. if len(args) > 1:
  54. self.user = args[1]
  55. if self.user.id == None:
  56. self.user = "NL"
  57.  
  58. def error(self, message):
  59. return json.dumps({ "Status": "Error", "Message": message }, ensure_ascii=False)
  60.  
  61. def success(self, message):
  62. return json.dumps({ "Status": "Success", "Message": message }, ensure_ascii=False)
  63.  
  64. def items(self, json):
  65. return json
  66.  
  67. def output(self):
  68. return self.validate()
  69.  
  70. class AjaxSavePhoto(Ajax):
  71. def validate(self):
  72. try:
  73. self.url = self.args[0]["url"]
  74. self.baseurl = self.args[0]["baseurl"]
  75. self.caption = self.args[0]["caption"]
  76. except Exception as e:
  77. return self.error("問題が発生しました。やり直してください。")
  78.  
  79. if self.user == "NL":
  80. return self.error("写真をアップロードするにはログインしてください。")
  81.  
  82. if len(self.caption) > 140:
  83. return self.error("キャプションは140文字以内である必要があります。")
  84.  
  85. if self.url[0:20] != "https://ucarecdn.com" or self.baseurl[0:20] != "https://ucarecdn.com":
  86. return self.error("Invalid image URL")
  87.  
  88. result = urlopen(self.baseurl+"-/preview/-/main_colors/3/")
  89. data = result.read()
  90. data = json.loads(data.decode('utf-8'))
  91.  
  92. main_colour = ""
  93. if data["main_colors"] != []:
  94. for colour in data["main_colors"][randint(0, 2)]:
  95. main_colour = main_colour + str(colour) + ","
  96. main_colour = main_colour[:-1]
  97.  
  98. result = urlopen(self.baseurl+"detect_faces/")
  99. data = result.read()
  100. data = json.loads(data.decode('utf-8'))
  101.  
  102. tag_count = 0
  103. p = Photo(url=self.url, baseurl=self.baseurl, owner=self.user.username, owner_school=self.user.school_name, likes=0, caption=self.caption, main_colour=main_colour)
  104. p.save()
  105. if data["faces"] != []:
  106. for face in data["faces"]:
  107. tag = PhotoTag(photoid=p.id, coords=face).save()
  108. tag_count = len(data["faces"])
  109. p.tags = tag_count
  110. p.save()
  111.  
  112. return self.success("Image Uploaded")
  113.  
  114. """
  115. Django settings for instapic project.
  116.  
  117. Generated by 'django-admin startproject' using Django 1.11.6.
  118.  
  119. For more information on this file, see
  120. https://docs.djangoproject.com/en/1.11/topics/settings/
  121.  
  122. For the full list of settings and their values, see
  123. https://docs.djangoproject.com/en/1.11/ref/settings/
  124. """
  125.  
  126. import os
  127.  
  128. # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
  129. BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  130.  
  131.  
  132. # Quick-start development settings - unsuitable for production
  133. # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
  134.  
  135. # SECURITY WARNING: keep the secret key used in production secret!
  136. SECRET_KEY = 'er7!))(no=x)g%y(qi1x*jj3*9)y-#3__0bwa*_j0d_l-r_%9q'
  137.  
  138. # SECURITY WARNING: don't run with debug turned on in production!
  139. DEBUG = True
  140.  
  141. ALLOWED_HOSTS = ['*']
  142.  
  143.  
  144. # Application definition
  145.  
  146. INSTALLED_APPS = [
  147. 'instapic',
  148. 'django.contrib.admin',
  149. 'django.contrib.auth',
  150. 'django.contrib.contenttypes',
  151. 'django.contrib.sessions',
  152. 'django.contrib.messages',
  153. 'django.contrib.staticfiles',
  154. ]
  155.  
  156. MIDDLEWARE = [
  157. 'django.middleware.security.SecurityMiddleware',
  158. 'whitenoise.middleware.WhiteNoiseMiddleware',
  159. 'django.contrib.sessions.middleware.SessionMiddleware',
  160. 'django.middleware.common.CommonMiddleware',
  161. 'django.middleware.csrf.CsrfViewMiddleware',
  162. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  163. 'django.contrib.messages.middleware.MessageMiddleware',
  164. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  165. ]
  166.  
  167. ROOT_URLCONF = 'instapic.urls'
  168.  
  169. TEMPLATES = [
  170. {
  171. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  172. 'DIRS': [os.path.join(BASE_DIR, 'templates')],
  173. 'APP_DIRS': True,
  174. 'OPTIONS': {
  175. 'context_processors': [
  176. 'django.template.context_processors.debug',
  177. 'django.template.context_processors.request',
  178. 'django.contrib.auth.context_processors.auth',
  179. 'django.contrib.messages.context_processors.messages',
  180. ],
  181. },
  182. },
  183. ]
  184.  
  185. WSGI_APPLICATION = 'instapic.wsgi.application'
  186.  
  187.  
  188. # Database
  189. # https://docs.djangoproject.com/en/1.11/ref/settings/#databases
  190.  
  191. DATABASES = {
  192. "default": {
  193. "ENGINE": "django.db.backends.postgresql_psycopg2",
  194. "NAME": "instapic",
  195. "USER": "dj.usagi",
  196. "PASSWORD": "drsm0619",
  197. "HOST": "localhost",
  198. "PORT": "",
  199. }
  200. }
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207. # Password validation
  208. # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
  209.  
  210. AUTH_PASSWORD_VALIDATORS = [
  211. {
  212. 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
  213. },
  214. {
  215. 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
  216. },
  217. {
  218. 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
  219. },
  220. {
  221. 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
  222. },
  223. ]
  224.  
  225.  
  226. # Internationalization
  227. # https://docs.djangoproject.com/en/1.11/topics/i18n/
  228.  
  229. LANGUAGE_CODE = 'ja'
  230.  
  231. TIME_ZONE = 'Asia/Tokyo'
  232.  
  233. USE_I18N = True
  234.  
  235. USE_L10N = True
  236.  
  237. USE_TZ = True
  238.  
  239.  
  240. # Static files (CSS, JavaScript, Images)
  241. # https://docs.djangoproject.com/en/1.11/howto/static-files/
  242.  
  243. STATIC_URL = '/static/'
  244.  
  245. STATICFILES_DIRS = [
  246. os.path.join(BASE_DIR, "static")
  247. ]
  248.  
  249. STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
  250.  
  251.  
  252. AUTHENTICATION_BACKENDS = ( 'instapic.authb.AuthB', )
  253.  
  254. # Heroku: Update database configuration from $DATABASE_URL.
  255. import dj_database_url
  256. db_from_env = dj_database_url.config(conn_max_age=500)
  257. DATABASES['default'].update(db_from_env)
  258.  
  259.  
  260. # Simplified static file serving.
  261. # https://warehouse.python.org/project/whitenoise/
  262. STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
Add Comment
Please, Sign In to add comment