diff --git a/BusinessSearch/Selected_businesses_panel.py b/BusinessSearch/Selected_businesses_panel.py index 34e8bb7a3381466822ff6d87def383703c54e2c9..cd9004d65b3093dd7d9086a0f762fbdfd815c6ca 100644 --- a/BusinessSearch/Selected_businesses_panel.py +++ b/BusinessSearch/Selected_businesses_panel.py @@ -70,21 +70,13 @@ class SelectedBusinessPanel(wx.Panel): label = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] - ## Edit this part############## - no_checkins = [ - 941, - 854, - 4595, - 2125, - 942, - 509, - 548, - 149, - 1952, - 161, - 64, - 61, - ] + self.database.psql.execute("SELECT COUNT(*) FROM Checkins WHERE businessid = '" + str(self.businessID) + "' GROUP BY month ORDER BY month") + rows = self.database.psql.fetchall() + + no_checkins = [] + for tup in rows: + no_checkins.append(tup[0]) + ############################### # this is for plotting purpose index = np.arange(len(label)) diff --git a/BusinessSearch/TipsPanel.py b/BusinessSearch/TipsPanel.py index dafbc97ad373aab3b01689fd1a945c9b5d444ae2..a037d4b5cb7f7f9924f7f820ee86df2196aed322 100644 --- a/BusinessSearch/TipsPanel.py +++ b/BusinessSearch/TipsPanel.py @@ -1,11 +1,28 @@ import wx import wx.grid as gridlib +import time +from time import strftime class TipsPanel(wx.Frame): + def cleanStr4SQL(self, s): + return s.replace("'","`").replace("\n"," ") + + def __init__(self, parent, database): wx.Frame.__init__(self, parent=parent, size=(2000, 1900)) self.database = database + self.businessID = parent.businessID + try: + temp = parent.userID.split(" - ") + self.userID = temp[1] + except: + self.userID = None + self.cur_tip_userid = None + self.cur_tip_businessid = None + self.cur_tip_time = None + self.cur_tip_date = None + self.SetTitle("Tips from Users") @@ -21,7 +38,7 @@ class TipsPanel(wx.Frame): flag=wx.ALL | wx.EXPAND, border=5) self.business_tips = wx.grid.Grid(self) - self.business_tips.CreateGrid(5, 4) + self.business_tips.CreateGrid(5, 7) self.business_tips.SetRowLabelSize(1) self.business_tips.SetColLabelValue(0, "Date") self.business_tips.SetColSize(0, 200) @@ -31,9 +48,17 @@ class TipsPanel(wx.Frame): self.business_tips.SetColSize(2, 200) self.business_tips.SetColLabelValue(3, "Text") self.business_tips.SetColSize(3, 600) + self.business_tips.SetColLabelValue(4, "Time") + self.business_tips.SetColSize(4, 0) + self.business_tips.SetColLabelValue(5, "BusinessID") + self.business_tips.SetColSize(5, 0) + self.business_tips.SetColLabelValue(6, "UserID") + self.business_tips.SetColSize(6, 0) sizer.Add(self.business_tips, pos=(2, 0), span=(2, 5), flag=wx.EXPAND | wx.ALL, border=5) + self.business_tips.Bind(gridlib.EVT_GRID_SELECT_CELL, self.onSelectBusinessTip) + sizer.Add(wx.StaticText(self, -1, "Friends who reviewed this business:"), pos=(4, 0), span=(1, 5), flag=wx.ALL | wx.EXPAND, border=5) @@ -49,6 +74,8 @@ class TipsPanel(wx.Frame): sizer.Add(self.friends_tips, pos=(5, 0), span=(2, 5), flag=wx.EXPAND | wx.ALL, border=5) + self.friends_tips.Bind(gridlib.EVT_GRID_SELECT_CELL, self.onSelectFriendTip) + self.new_tip_button = wx.BitmapButton(self, -1, wx.Bitmap("BusinessSearch/images/new_tip_icon.JPG")) self.new_tip_button.Bind(wx.EVT_BUTTON, self.create_tip) sizer.Add(self.new_tip_button, pos=(7, 3), span=(1, 1), @@ -65,14 +92,140 @@ class TipsPanel(wx.Frame): sizer.AddGrowableCol(0) + self.populate_business_tips() + self.populate_friends_tips() + self.SetSizerAndFit(sizer) + def create_tip(self, evt=None): + if(self.userID is None): + return + dlg = wx.TextEntryDialog(self, 'Enter tip below', 'New tip') if dlg.ShowModal() == wx.ID_OK: - print('You entered: %s\n' % dlg.GetValue()) + cur_time = time.localtime() + + cur_date = strftime("%Y-%m-%d", cur_time) + cur_HMS = strftime("%H:%M:%S", cur_time) + + sql_str = "INSERT INTO Tips (userID, businessID, tip_date, tip_time, num_likes, description) " + \ + "VALUES ('" + self.userID + "','" + self.businessID + \ + "','" + cur_date + "','" + cur_HMS + \ + "','" + str(0) + "','" + self.cleanStr4SQL(dlg.GetValue()) + "');" + + print(sql_str) + self.database.psql.execute(sql_str) + self.database.conn.commit() + + dlg.Destroy() def like(self, evt=None): + + # businessID = self.business_tips.GetCellValue(row, 5) + # userID = self.business_tips.GetCellValue(row, 6) + # tip_date = self.business_tips.GetCellValue(row, 0) + # tip_time = self.business_tips.GetCellValue(row, 4) + # print(businessID) + # print(userID) + # print(tip_date) + # print(tip_time) + print(self.cur_tip_businessid) + print(self.cur_tip_userid) + print(self.cur_tip_date) + print(self.cur_tip_time) + + sql_str = "UPDATE Tips AS T " + \ + "SET num_likes = num_likes + 1 " + \ + "WHERE T.businessid = '" + str(self.cur_tip_businessid) + "' " + \ + "AND T.userid = '" + str(self.cur_tip_userid) + "' " + \ + "AND T.tip_date = '" + str(self.cur_tip_date) + "' " + \ + "AND T.tip_time = '" + str(self.cur_tip_time) + "' " + + print(sql_str) + + self.database.psql.execute(sql_str) + self.database.conn.commit() + + + def populate_business_tips(self, event=None): + print("entered populate_business_tips") + self.business_tips.ClearGrid() + + self.database.psql.execute( + "SELECT tip_date, U.name, num_likes, description, tip_time, businessid, U.userid " + \ + "FROM Tips AS T, Users AS U " + \ + "WHERE T.businessid = '" + str(self.businessID) + "' " + \ + "AND U.userid = T.userid" + ) + + rows = self.database.psql.fetchall() + # self.number_results_label.SetLabelText("Your search returned " + str(len(rows)) + " results") + + if len(rows) > self.business_tips.NumberRows: + self.business_tips.AppendRows(len(rows) - self.business_tips.NumberRows) + + for row in range(0, len(rows)): + self.business_tips.SetCellValue(row, 0, str(rows[row][0])) # date + self.business_tips.SetCellValue(row, 1, str(rows[row][1])) # name + self.business_tips.SetCellValue(row, 2, str(rows[row][2])) # likes + self.business_tips.SetCellValue(row, 3, str(rows[row][3])) # description + self.business_tips.SetCellValue(row, 4, str(rows[row][4])) # time + self.business_tips.SetCellValue(row, 5, str(rows[row][5])) # businessid + self.business_tips.SetCellValue(row, 6, str(rows[row][6])) # userid + # self.business_tips.SetCellValue(row, 7, str(rows[row][9])) # checkins + # self.business_tips.SetCellValue(row, 8, str(rows[row][0])) # businessID + + self.Layout() + self.Update() + + + def populate_friends_tips(self, event=None): + print("entered populate_friends_tips") + if (self.userID is None): return + self.friends_tips.ClearGrid() + + sql_str = "SELECT U.name, T.tip_date, T.description " + \ + "FROM Tips AS T, Users AS U " + \ + "WHERE T.businessid = '" + str(self.businessID) + "' " + \ + "AND U.userid = T.userid " + \ + "AND T.userid IN ( " + \ + "SELECT friendid " + \ + "FROM Friend " + \ + "WHERE userid = '" + str(self.userID) + "' " + \ + ")" + + print(sql_str) + self.database.psql.execute(sql_str) + + rows = self.database.psql.fetchall() + # self.number_results_label.SetLabelText("Your search returned " + str(len(rows)) + " results") + + if len(rows) > self.business_tips.NumberRows: + self.friends_tips.AppendRows(len(rows) - self.business_tips.NumberRows) + + for row in range(0, len(rows)): + self.friends_tips.SetCellValue(row, 0, str(rows[row][0])) + self.friends_tips.SetCellValue(row, 1, str(rows[row][1])) + self.friends_tips.SetCellValue(row, 2, str(rows[row][2])) + + self.Layout() + self.Update() + + + def onSelectBusinessTip(self, evt=None): + row = evt.GetRow() + self.cur_tip_businessid = self.business_tips.GetCellValue(row, 5) + self.cur_tip_userid = self.business_tips.GetCellValue(row, 6) + self.cur_tip_date = self.business_tips.GetCellValue(row, 0) + self.cur_tip_time = self.business_tips.GetCellValue(row, 4) + + def onSelectFriendTip(self, evt=None): + row = evt.GetRow() + self.cur_tip_businessid = self.business_tips.GetCellValue(row, 5) + self.cur_tip_userid = self.business_tips.GetCellValue(row, 6) + self.cur_tip_date = self.business_tips.GetCellValue(row, 0) + self.cur_tip_time = self.business_tips.GetCellValue(row, 4) \ No newline at end of file diff --git a/Database.py b/Database.py index c1b5fa46cc81bb0537765710f39d68f46e17f9c2..6dd0e876f6dc2e3457f05a9146e631b88ea48060 100644 --- a/Database.py +++ b/Database.py @@ -5,4 +5,5 @@ class Database: def __init__(self): conn = psycopg2.connect("dbname='milestone2' user='postgres' host='localhost' password='Spring*2014'") print("Database opened successfully") + self.conn = conn self.psql = conn.cursor() \ No newline at end of file diff --git a/TeamRocket_UPDATE.sql b/TeamRocket_UPDATE.sql index e9780661564550d1221e920de0a2555ebc238c72..54ffc6a5083220793669e66836b946e63a614c37 100644 --- a/TeamRocket_UPDATE.sql +++ b/TeamRocket_UPDATE.sql @@ -70,4 +70,4 @@ SET tip_count = ( SELECT COUNT(*) FROM Tips AS T WHERE T.userID = U.userID -); \ No newline at end of file +);