class csvworker: def __init__(self): self.db = sqlite3.connect("resources/UserData.db") self.cursor = self.db.cursor() self.c = Crypto(False, 0) # Method - exportcsv # Parameters - idtype: string # Return - None # Purpose - Creates a csv file from the user's selected database table (notes or reminders) def exportcsv(self, idtype): file = open("resources/output.csv", "w") writer = csv.writer(file) sql = """SELECT {}ID, Title, Content, Date FROM {}s ORDER BY Title""".format(idtype, idtype) self.cursor.execute(sql) data = self.cursor.fetchall() writer.writerow(["ID", "Title", "Content", "Date"]) for i in range(len(data)): writer.writerow([data[i][0], self.c.decrypt(data[i][1]), self.c.decrypt(data[i][2]), data[i][3]]) # Method - email # Parameters - username: string, password: string, target: string # Return - None # Purpose - Sends an email with the exported csv file attached over gmail servers using the user's email and # password, to a user defined target address def email(self, username, password, target): try: msg = MIMEMultipart() msg['From'] = username msg['To'] = target msg['Subject'] = "Assistant - Data output" body = "Attached is the output data in csv format, as created by Assistant" msg.attach(MIMEText(body, 'plain')) filename = "resources/output.csv" attachment = open("resources/output.csv", "rb") file = MIMEBase('application', 'octet-stream') file.set_payload(attachment.read()) encoders.encode_base64(file) file.add_header('Content-Disposition', "attachment; filename= %s" % filename) msg.attach(file) server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(username, password) text = msg.as_string() server.sendmail(username, target, text) server.quit() return True except: return False
def getweather(self): with sqlite3.connect("resources/UserData.db") as db: cursor = db.cursor() cursor.execute("SELECT city FROM userInfo") city = cursor.fetchone() cursor.execute("SELECT country FROM userInfo") country = cursor.fetchone() c = Crypto(False, 0) city = c.decrypt(city[0]) country = c.decrypt(country[0]) w = Weather4Day(country, city) self.lblLocation.text = "The weather in {}, {} is".format( city, country) self.lblWeatherText.text = w.forecasttodaytext() self.lblWeatherHigh.text = w.forecasttodayhigh() + "C" self.lblWeatherLow.text = w.forecasttodaylow() + "C"
def __init__(self, **kwargs): super(HomeScreen, self).__init__(**kwargs) with sqlite3.connect("resources/UserData.db") as db: cursor = db.cursor() cursor.execute("SELECT Name FROM userInfo") username = cursor.fetchone() c = Crypto(False, 0) username = c.decrypt(username[0]) self.lblName.text = "Welcome, {}!".format(username)
class RemindersScreen(Screen): # Method - RemindersScreen init # Parameters - None # Return - None # Purpose - Initialises instances of the reminders and crypto classes, connects to the database, and runs the # latestreminder method def __init__(self, **kwargs): super(RemindersScreen, self).__init__(**kwargs) self.c = Crypto(False, 0) self.r = Reminders() self.latestreminder() self.db = sqlite3.connect("resources/UserData.db") self.cursor = self.db.cursor() # Method - latestreminder # Parameters - data: list of strings # Return - None # Purpose - If the user has any reminders, display the most recent one in labels def latestreminder(self): data = self.r.mostrecent() if data is False: self.lblLastReminderTitle.text = "No reminders found!" self.lblLastReminderContent.text = " " else: recenttitle = data[0] recentcontent = data[1] recenttitle = self.c.decrypt(recenttitle) recentcontent = self.c.decrypt(recentcontent) self.lblLastReminderTitle.text = recenttitle self.lblLastReminderContent.text = recentcontent # Method - newreminder # Parameters - None # Return - None # Purpose - Clears the content of the text inputs of the newreminder screen, then displays it def newreminder(self): newreminders = self.manager.get_screen("newreminders") newreminders.inputNewReminderTitle.text = "" newreminders.inputNewReminderContent.text = "" newreminders.inputNewReminderYear.text = "" newreminders.inputNewReminderMonth.text = "" newreminders.inputNewReminderDay.text = "" newreminders.inputNewReminderHour.text = "" newreminders.inputNewReminderMinute.text = "" newreminders.inputNewReminderSecond.text = "" self.parent.current = "newreminders" # Method - remindersbytime # Parameters - data: list of strings # Return - None # Purpose - Retrieves a list of the user's reminders sorted by time, then passes it to the setupmorereminders # function def remindersbytime(self): data = self.r.sort("Date") count = len(data) self.setupmorereminders(count, data) # Method - remindersbytime # Parameters - data: list of strings # Return - None # Purpose - Retrieves a list of the user's reminders sorted by title alphabetically, then passes it to the # setupmorereminders function def remindersbytitle(self): data = self.r.sort("Title") count = len(data) self.setupmorereminders(count, data) # Method - searchreminders # Parameters - searchterm: string, data: list of strings # Return - None # Purpose - Gets a list of the user's reminders containing a given search term, then passes it to the # setupmorereminders function def searchreminders(self): searchterm = self.inputSearchReminders.text if searchterm == "": pass else: searchterm = self.c.encrypt(searchterm) data = self.r.search(searchterm) count = len(data) self.setupmorereminders(count, data) # Method - setupmorereminders # Parameters - count: integer , data: list of strings # Return - None # Purpose - Adds widgets displaying the title and content of each reminder within the given list, as well as their # corresponding edit and delete buttons to the morereminders screen. A "back" button is then added and the screen is # displayed. def setupmorereminders(self, count, data): morereminders = self.manager.get_screen("morereminders") morereminders.layoutMoreReminders.clear_widgets( morereminders.layoutMoreReminders.children) for i in range(count): reminderid = data[i][0] title = data[i][1] title = self.c.decrypt(title) content = data[i][2] content = self.c.decrypt(content) date = data[i][3] date = datetime.datetime.fromtimestamp(date) lbltitle = Label(text=title, size_hint_y=None) lbltitle.texture_update() morereminders.layoutMoreReminders.add_widget(lbltitle) lbltext = Label(text=content, size_hint_y=None) lbltext.texture_update() morereminders.layoutMoreReminders.add_widget(lbltext) lbldate = Label(text=str(date), size_hint_y=None) lbldate.texture_update() morereminders.layoutMoreReminders.add_widget(lbldate) grid = GridLayout(cols=2, size_hint_y=None) btnedit = Button(text="Edit", size_hint_y=None, on_press=lambda a: self.edit(reminderid)) btndelete = Button(text="Delete", size_hint_y=None, on_press=lambda a: self.delete(reminderid)) btnedit.texture_update() btndelete.texture_update() grid.add_widget(btnedit) grid.add_widget(btndelete) morereminders.layoutMoreReminders.add_widget(grid) grid = GridLayout(cols=2, size_hint_y=None) btnback = (Button(text="Back", height=dp(80), on_press=lambda a: self.back())) grid.add_widget(btnback) morereminders.layoutMoreReminders.add_widget(grid) self.manager.current = "morereminders" # Method - back # Parameters - None # Return - None # Purpose - Runs the latestreminder method, then displays the reminders screen def back(self): self.latestreminder() self.manager.current = "reminders" # Method - delete # Parameters - reminderid: string # Return - None # Purpose - Deletes the reminder corresponding to the given reminderid, then calls the latestreminder method and # displays the reminders screen def delete(self, reminderid): self.r.delete(reminderid) self.latestreminder() self.manager.current = "reminders" # Method - edit # Parameters - reminderid: string # Return - None # Purpose - Displays the editreminders screen, and passes reminderid to it def edit(self, reminderid): editreminders = self.manager.get_screen("editreminders") editreminders.currentreminderid = reminderid self.manager.current = "editreminders"
class TwitterScreen(Screen): # Method - TwitterScreen init # Parameters - None # Return - None # Purpose - When kivy has built the screen, initialises instances of the Crypto and Twitter classes for this class, # then calls the latesttweet function def __init__(self, **kwargs): super(TwitterScreen, self).__init__(**kwargs) self.db = sqlite3.connect("resources/UserData.db") self.cursor = self.db.cursor() self.c = Crypto(False, 0) self.t = Twitter() self.latesttweet() # Method - latesttweet # Parameters - username # Return - None # Purpose - Retrieves the username the user last searched for, fetches their latest tweet from the API, then # displays their username latest tweet in labels def latesttweet(self): self.cursor.execute("SELECT LastTwitterSearch FROM userInfo") username = self.cursor.fetchone() username = self.c.decrypt(username[0]) self.lblRecentTweet.text = self.t.userlatest(username) self.lblRecentUsername.text = "Latest tweet from @" + username # Method - getmoretweets # Parameters - un:string # Return - None # Purpose - Obtains and displays the data on the moretweets screen using the user's chosen location def getmoretweets(self): un = self.inputTwitterUsername.text if un == "": pass else: moretwitter = self.manager.get_screen("moretwitter") moretwitter.layoutMoreTwitter.clear_widgets( moretwitter.layoutMoreTwitter.children) tweets = self.t.user10(un) lblun = Label(text=("Latest tweets from @" + un), size_hint_y=None) moretwitter.layoutMoreTwitter.add_widget(lblun) for i in range(len(tweets)): lbltweet = Label(text=tweets[i], size_hint_y=None) lbltweet.texture_update() moretwitter.layoutMoreTwitter.add_widget(lbltweet) btnback = Button(text="Back", height=dp(40), size_hint_y=None, on_press=lambda a: self.back()) moretwitter.layoutMoreTwitter.add_widget(btnback) self.manager.current = "moretwitter" # Method - back # Parameters - username: string, secureusername:s tring # Return - None # Purpose - Updates the main twitter screen with the latest tweet from the username the user last searched for, # then displays the twitter screen. def back(self): username = self.inputTwitterUsername.text secureusername = self.c.encrypt(username) sql = """UPDATE userInfo SET LastTwitterSearch='{}'""".format( secureusername) self.cursor.execute(sql) self.db.commit() self.lblRecentUsername.text = "Latest tweet from @{}".format(username) self.lblRecentTweet.text = self.t.userlatest(username) self.manager.current = "twitter"