Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import json
- import sqlite3 as sql
- from pprint import pprint
- class Client(object):
- host = 'http://task.mboatek.com'
- username = 'client1'
- password = 'pass4321'
- def __init__(self, username, password, *args, **kwargs):
- self.username = username
- self.password = password
- self.sess = requests.Session()
- try:
- resp = self.sess.get(self.host+'/start')
- res = resp.json()
- if res['error']:
- raise ValueError("Http error: %s" % res['message'])
- self.token = res['token']
- self.sess.headers["X-CSRFToken"] = self.token
- to_send = {'username': self.username, 'password': self.password}
- resp = self.sess.post(self.host+'/api_login', data=to_send)
- res = resp.json()
- if res['error']:
- raise ValueError('Login error: %s' % res['message'])
- self.token = res['token']
- except:
- pass
- def submit(self, data):
- if not isinstance(data, dict):
- raise TypeError("data must be instance of dict")
- self.sess.headers["X-CSRFToken"] = self.token
- resp = self.sess.post(self.host+'/api_submit', data=data)
- res = resp.json()
- if res['error']:
- raise ValueError("SubmitError: %s" % res['message'])
- self.token = res['token']
- return True
- class Database(object):
- def __init__(self, *args, **kwargs):
- self.con = sql.connect("store.db")
- self.cur = self.con.cursor()
- self.cur.execute('CREATE TABLE IF NOT EXISTS "activity" '\
- '("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "date" date NOT NULL UNIQUE, '\
- '"get_up" bool NULL, "start_work" bool NULL, "exer_ten" bool NULL, '\
- '"deep_stud_first" bool NULL, "deep_stud_snd" bool NULL, "deep_stud_third" bool NULL, '\
- '"deep_stud_bynd" bool NULL, "no_bread" bool NULL, "no_meat" bool NULL, '\
- '"no_milk" bool NULL, "no_sugar" bool NULL, "no_gluten" bool NULL, "dont_eat" bool NULL, '\
- '"salat_lunch" bool NULL, "exercise" bool NULL, "dont_temper" bool NULL, '\
- '"remove_obstacle" bool NULL, "no_news" bool NULL, "clean_desk" bool NULL, '\
- '"plan_next" bool NULL, "no_wine" bool NULL, "limit_glasses" bool NULL, '\
- '"no_tv" bool NULL, "read_book" bool NULL, "arth_meds" bool NULL, '\
- '"meditation" bool NULL, "go_bed" bool NULL)')
- def insert(self, data):
- ins_query = "INSERT INTO `activity` (`date`,`get_up`,`start_work`,`exer_ten`,`deep_stud_first`,"\
- "`deep_stud_snd`,`deep_stud_third`,`deep_stud_bynd`,`no_bread`,`no_meat`,`no_milk`,`no_sugar`,"\
- "`no_gluten`,`dont_eat`,`salat_lunch`,`exercise`,`dont_temper`,`remove_obstacle`,`no_news`,"\
- "`clean_desk`,`plan_next`,`no_wine`,`limit_glasses`,`no_tv`,`read_book`,`arth_meds`,`meditation`,"\
- "`go_bed`) VALUES (:date,:get_up,:start_work,:exer_ten,:deep_stud_first,"\
- ":deep_stud_snd,:deep_stud_third,:deep_stud_bynd,:no_bread,:no_meat,:no_milk,:no_sugar,"\
- ":no_gluten,:dont_eat,:salat_lunch,:exercise,:dont_temper,:remove_obstacle,:no_news,"\
- ":clean_desk,:plan_next,:no_wine,:limit_glasses,:no_tv,:read_book,:arth_meds,:meditation,"\
- ":go_bed);"
- pres_query = "SELECT * FROM `activity` WHERE `date`=:date;"
- mod_query = "UPDATE `activity` SET get_up=:get_up, start_work=:start_work, "\
- "exer_ten=:exer_ten, deep_stud_first=:deep_stud_first, deep_stud_snd=:deep_stud_snd, "\
- "deep_stud_third=:deep_stud_third, deep_stud_bynd=:deep_stud_bynd, no_bread=:no_bread, "\
- "no_meat=:no_meat, no_milk=:no_milk, no_sugar=:no_sugar, no_gluten=:no_gluten, "\
- "dont_eat=:dont_eat, salat_lunch=:salat_lunch, exercise=:exercise, dont_temper=:dont_temper, "\
- "remove_obstacle=:remove_obstacle, no_news=:no_news, clean_desk=:clean_desk, "\
- "plan_next=:plan_next, no_wine=:no_wine, limit_glasses=:limit_glasses, no_tv=:no_tv, "\
- "read_book=:read_book, arth_meds=:arth_meds, meditation=:meditation, go_bed=:go_bed WHERE `date`=:date;"
- self.cur.execute(pres_query, data)
- if self.cur.fetchone() is None:
- to_do = ins_query
- else:
- to_do = mod_query
- res = self.cur.execute(to_do, data)
- self.con.commit()
- pprint(data)
- def update(self, data, date):
- pass
- def get(self, date):
- pres_query = "SELECT * FROM `activity` WHERE `date`=:date;"
- self.cur.execute(pres_query, {'date': date})
- row = self.cur.fetchone()
- if row is None:
- return None
- res = {}
- for i in range(1, len(row)):
- key = self.cur.description[i][0]
- res[key] = row[i]
- return res
- def get_all(self, filter=None):
- pres_query = "SELECT * FROM `activity` ORDER BY id DESC;"
- self.cur.execute(pres_query)
- rows = self.cur.fetchall()
- return rows
- def __del__(self):
- self.con.commit()
- self.cur.close()
- self.con.close()
- if __name__ == '__main__':
- cli = Client()
- db = Database()
- Log
- 2018-11-16 10:17:05.836659-0500 taskcheck[93894:1461451] [framework] CUIThemeStore: No theme registered with id=0
- 2018-11-16 10:17:06.383505-0500 taskcheck[93894:1461451] Available orientation: KIVY_ORIENTATION=LandscapeLeft LandscapeRight Portrait PortraitUpsideDown
- 2018-11-16 10:17:06.383920-0500 taskcheck[93894:1461451] Initializing python
- /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
- 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
- [INFO ] [Kivy ] v1.10.1
- [INFO ] [Python ] v3.7.1 (default, Nov 16 2018, 06:27:35)
- [Clang 10.0.0 (clang-1000.11.45.5)]
- [INFO ] [Factory ] 194 symbols loaded
- [INFO ] [Image ] Providers: img_imageio, img_tex (img_dds, img_sdl2, img_ffpyplayer, img_pil, img_gif ignored)
- [INFO ] [Text ] Provider: sdl2
- [INFO ] [Window ] Provider: sdl2
- [INFO ] [GL ] Using the "OpenGL ES 2" graphics system
- [INFO ] [GL ] Backend used <sdl2>
- [INFO ] [GL ] OpenGL version <b'OpenGL ES 2.0 APPLE-17.0.37'>
- [INFO ] [GL ] OpenGL vendor <b'Apple Inc.'>
- [INFO ] [GL ] OpenGL renderer <b'Apple Software Renderer'>
- [INFO ] [GL ] OpenGL parsed version: 2, 0
- [INFO ] [GL ] Shading version <b'OpenGL ES GLSL ES 1.00'>
- [INFO ] [GL ] Texture max size <4096>
- [INFO ] [GL ] Texture max units <8>
- [INFO ] [Window ] auto add sdl2 input provider
- [INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
- Traceback (most recent call last):
- File "/Users/Eckhardt/Project/kivy-ios/taskcheck-ios/YourApp/main.py", line 13, in <module>
- File "/Users/Eckhardt/Project/kivy-ios/taskcheck-ios/YourApp/core.py", line 1, in <module>
- ModuleNotFoundError: No module named 'requests'
- 2018-11-16 10:17:14.071383-0500 taskcheck[93894:1461451] Application quit abnormally!
- 2018-11-16 10:17:14.104055-0500 taskcheck[93894:1461451] Leaving
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement