Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.00 KB | None | 0 0
  1. import requests
  2. import json
  3. import sqlite3 as sql
  4. from pprint import pprint
  5.  
  6.  
  7. class Client(object):
  8.    
  9.     host = 'http://task.mboatek.com'
  10.     username = 'client1'
  11.     password = 'pass4321'
  12.    
  13.     def __init__(self, username, password, *args, **kwargs):
  14.         self.username = username
  15.         self.password = password
  16.        
  17.         self.sess = requests.Session()
  18.         try:
  19.             resp = self.sess.get(self.host+'/start')
  20.             res = resp.json()
  21.             if res['error']:
  22.                 raise ValueError("Http error: %s" % res['message'])
  23.            
  24.             self.token = res['token']
  25.             self.sess.headers["X-CSRFToken"] = self.token
  26.             to_send = {'username': self.username, 'password': self.password}
  27.             resp = self.sess.post(self.host+'/api_login', data=to_send)
  28.             res = resp.json()
  29.             if res['error']:
  30.                 raise ValueError('Login error: %s' % res['message'])
  31.            
  32.             self.token = res['token']
  33.         except:
  34.             pass
  35.        
  36.     def submit(self, data):
  37.         if not isinstance(data, dict):
  38.             raise TypeError("data must be instance of dict")
  39.        
  40.         self.sess.headers["X-CSRFToken"] = self.token
  41.         resp = self.sess.post(self.host+'/api_submit', data=data)
  42.         res = resp.json()
  43.         if res['error']:
  44.             raise ValueError("SubmitError: %s" % res['message'])
  45.        
  46.         self.token = res['token']
  47.         return True
  48.    
  49.  
  50. class Database(object):
  51.    
  52.     def __init__(self, *args, **kwargs):
  53.         self.con = sql.connect("store.db")
  54.         self.cur = self.con.cursor()
  55.        
  56.         self.cur.execute('CREATE TABLE IF NOT EXISTS "activity" '\
  57.                          '("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "date" date NOT NULL UNIQUE, '\
  58.                          '"get_up" bool NULL, "start_work" bool NULL, "exer_ten" bool NULL, '\
  59.                          '"deep_stud_first" bool NULL, "deep_stud_snd" bool NULL, "deep_stud_third" bool NULL, '\
  60.                          '"deep_stud_bynd" bool NULL, "no_bread" bool NULL, "no_meat" bool NULL, '\
  61.                          '"no_milk" bool NULL, "no_sugar" bool NULL, "no_gluten" bool NULL, "dont_eat" bool NULL, '\
  62.                          '"salat_lunch" bool NULL, "exercise" bool NULL, "dont_temper" bool NULL, '\
  63.                          '"remove_obstacle" bool NULL, "no_news" bool NULL, "clean_desk" bool NULL, '\
  64.                          '"plan_next" bool NULL, "no_wine" bool NULL, "limit_glasses" bool NULL, '\
  65.                          '"no_tv" bool NULL, "read_book" bool NULL, "arth_meds" bool NULL, '\
  66.                          '"meditation" bool NULL, "go_bed" bool NULL)')
  67.        
  68.     def insert(self, data):
  69.         ins_query = "INSERT INTO `activity` (`date`,`get_up`,`start_work`,`exer_ten`,`deep_stud_first`,"\
  70.             "`deep_stud_snd`,`deep_stud_third`,`deep_stud_bynd`,`no_bread`,`no_meat`,`no_milk`,`no_sugar`,"\
  71.             "`no_gluten`,`dont_eat`,`salat_lunch`,`exercise`,`dont_temper`,`remove_obstacle`,`no_news`,"\
  72.             "`clean_desk`,`plan_next`,`no_wine`,`limit_glasses`,`no_tv`,`read_book`,`arth_meds`,`meditation`,"\
  73.             "`go_bed`) VALUES (:date,:get_up,:start_work,:exer_ten,:deep_stud_first,"\
  74.             ":deep_stud_snd,:deep_stud_third,:deep_stud_bynd,:no_bread,:no_meat,:no_milk,:no_sugar,"\
  75.             ":no_gluten,:dont_eat,:salat_lunch,:exercise,:dont_temper,:remove_obstacle,:no_news,"\
  76.             ":clean_desk,:plan_next,:no_wine,:limit_glasses,:no_tv,:read_book,:arth_meds,:meditation,"\
  77.             ":go_bed);"
  78.        
  79.         pres_query = "SELECT * FROM `activity` WHERE `date`=:date;"
  80.        
  81.         mod_query = "UPDATE `activity` SET get_up=:get_up, start_work=:start_work, "\
  82.             "exer_ten=:exer_ten, deep_stud_first=:deep_stud_first, deep_stud_snd=:deep_stud_snd, "\
  83.             "deep_stud_third=:deep_stud_third, deep_stud_bynd=:deep_stud_bynd, no_bread=:no_bread, "\
  84.             "no_meat=:no_meat, no_milk=:no_milk, no_sugar=:no_sugar, no_gluten=:no_gluten, "\
  85.             "dont_eat=:dont_eat, salat_lunch=:salat_lunch, exercise=:exercise, dont_temper=:dont_temper, "\
  86.             "remove_obstacle=:remove_obstacle, no_news=:no_news, clean_desk=:clean_desk, "\
  87.             "plan_next=:plan_next, no_wine=:no_wine, limit_glasses=:limit_glasses, no_tv=:no_tv, "\
  88.             "read_book=:read_book, arth_meds=:arth_meds, meditation=:meditation, go_bed=:go_bed WHERE `date`=:date;"
  89.        
  90.         self.cur.execute(pres_query, data)
  91.         if self.cur.fetchone() is None:
  92.             to_do = ins_query
  93.         else:
  94.             to_do = mod_query
  95.            
  96.         res = self.cur.execute(to_do, data)
  97.         self.con.commit()
  98.         pprint(data)
  99.    
  100.     def update(self, data, date):
  101.         pass
  102.    
  103.     def get(self, date):
  104.         pres_query = "SELECT * FROM `activity` WHERE `date`=:date;"
  105.         self.cur.execute(pres_query, {'date': date})
  106.         row = self.cur.fetchone()
  107.        
  108.         if row is None:
  109.             return None
  110.        
  111.         res = {}
  112.         for i in range(1, len(row)):
  113.             key = self.cur.description[i][0]
  114.             res[key] = row[i]
  115.            
  116.         return res
  117.    
  118.     def get_all(self, filter=None):
  119.         pres_query = "SELECT * FROM `activity` ORDER BY id DESC;"
  120.         self.cur.execute(pres_query)
  121.         rows = self.cur.fetchall()        
  122.        
  123.         return rows
  124.    
  125.     def __del__(self):
  126.         self.con.commit()
  127.         self.cur.close()
  128.         self.con.close()
  129.    
  130.    
  131. if __name__ == '__main__':
  132.     cli = Client()
  133.     db = Database()
  134.  
  135.  
  136.  
  137. Log
  138.  
  139. 2018-11-16 10:17:05.836659-0500 taskcheck[93894:1461451] [framework] CUIThemeStore: No theme registered with id=0
  140. 2018-11-16 10:17:06.383505-0500 taskcheck[93894:1461451] Available orientation: KIVY_ORIENTATION=LandscapeLeft LandscapeRight Portrait PortraitUpsideDown
  141. 2018-11-16 10:17:06.383920-0500 taskcheck[93894:1461451] Initializing python
  142. /Users/Eckhardt/Library/Developer/CoreSimulator/Devices/B912277E-4A57-4E17-844C-49A34376E056/data/Containers/Bundle/Application/9EE46295-89AC-4177-8F9B-9BDBA1FE5BF6/taskcheck.app/taskcheck:1: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  143. 2018-11-16 10:17:06.918016-0500 taskcheck[93894:1461451] Running main.py: /Users/Eckhardt/Library/Developer/CoreSimulator/Devices/B912277E-4A57-4E17-844C-49A34376E056/data/Containers/Bundle/Application/9EE46295-89AC-4177-8F9B-9BDBA1FE5BF6/taskcheck.app/YourApp/main.pyc
  144. [INFO   ] [Kivy        ] v1.10.1
  145. [INFO   ] [Python      ] v3.7.1 (default, Nov 16 2018, 06:27:35)
  146. [Clang 10.0.0 (clang-1000.11.45.5)]
  147. [INFO   ] [Factory     ] 194 symbols loaded
  148. [INFO   ] [Image       ] Providers: img_imageio, img_tex (img_dds, img_sdl2, img_ffpyplayer, img_pil, img_gif ignored)
  149. [INFO   ] [Text        ] Provider: sdl2
  150. [INFO   ] [Window      ] Provider: sdl2
  151. [INFO   ] [GL          ] Using the "OpenGL ES 2" graphics system
  152. [INFO   ] [GL          ] Backend used <sdl2>
  153. [INFO   ] [GL          ] OpenGL version <b'OpenGL ES 2.0 APPLE-17.0.37'>
  154. [INFO   ] [GL          ] OpenGL vendor <b'Apple Inc.'>
  155. [INFO   ] [GL          ] OpenGL renderer <b'Apple Software Renderer'>
  156. [INFO   ] [GL          ] OpenGL parsed version: 2, 0
  157. [INFO   ] [GL          ] Shading version <b'OpenGL ES GLSL ES 1.00'>
  158. [INFO   ] [GL          ] Texture max size <4096>
  159. [INFO   ] [GL          ] Texture max units <8>
  160. [INFO   ] [Window      ] auto add sdl2 input provider
  161. [INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
  162. Traceback (most recent call last):
  163.   File "/Users/Eckhardt/Project/kivy-ios/taskcheck-ios/YourApp/main.py", line 13, in <module>
  164.   File "/Users/Eckhardt/Project/kivy-ios/taskcheck-ios/YourApp/core.py", line 1, in <module>
  165. ModuleNotFoundError: No module named 'requests'
  166. 2018-11-16 10:17:14.071383-0500 taskcheck[93894:1461451] Application quit abnormally!
  167. 2018-11-16 10:17:14.104055-0500 taskcheck[93894:1461451] Leaving
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement