def shareGameLog(user, checkin_json): logging.info('#### posting to Facebook '+user.access_token+'####') # Build Game data ... game = models.Game.get_by_key_name(checkin_json['game']['mid']) thumbnail = 'http://api.freebase.com/api/trans/image_thumb'+game.mid+'?maxwidth=80&maxheight=100' if game.image_url is not None: thumbnail = game.image_url+'=s100' gamelog = checkin_json['gamelog'] description = utils.smart_truncate(game.description, length=300) # Build String of Player Scores ... # TODO: It would be great if Facebook players could be tagged. players = [] for s in gamelog['scores']: player = s['name']+' - '+s['points']+' points.' players.append(player) s = ' '.join(players) message = s + ' ' + gamelog['note'] # Build GraphAPI Request ... url = 'http://www.supermeeple.com/user/' + user.fb_id caption = "SuperMeeple: Board Game Database, Tools and Apps" attachment = {} attachment['caption'] = caption attachment['name'] = 'Scored a game of ' + game.name attachment['link'] = url #url attachment['description'] = description attachment['picture'] = thumbnail action_link = 'http://www.supermeeple.com/game'+str(game.mid)+'/'+str(game.bgg_id) action_name = "Check In!" actions = {"name": action_name, "link": action_link} attachment['actions'] = actions results = facebook.GraphAPI( user.access_token).put_wall_post(message, attachment) return
def referred(self, user, db): user_id = user.id log.info(user_id) if not self.session.get('referred', None): return log.info(self.session.get('referred')) try: referred_user = ReferralUser() referred_user.refered_id = self.session.get('referred') name = '{} {}'.format(user.facebook_data.get('first_name'), user.facebook_data.get('last_name', '')) referred_user.face_data = { 'name': smart_truncate(name, 24), 'picture': user.avatar.get('small') } referred_user.user_id = user_id referred_user.created_at = datetime.utcnow() db.sync(referred_user) thisweek = (datetime.utcnow() - timedelta(days=2)) start = (thisweek - timedelta(days=thisweek.weekday())).replace( hour=0, minute=0, second=0, microsecond=0) end = (start + timedelta(days=6)).replace(hour=23, minute=59, second=59, microsecond=999) log.info(start) log.info(end) references = db.query(ReferralUser).filter( ReferralUser.refered_id == referred_user.refered_id, ReferralUser.created_at <= end, ReferralUser.created_at >= start).count() log.info(references) if references < 15: udata = db.get(Life, rr='life', user_id=referred_user.refered_id) udata.incr_(life_qtd=1) log.info('added live') try: udata.sync(constraints=[Life.life_qtd <= 15]) self.application.db_conn.get('ping').set( referred_user.refered_id, 1) referred_user.life = 1 db.sync(referred_user) log.info('pingued') except CheckFailed: log.warn( 'User {} already received more than 15 lives this week.' .format(self.session.get('referred', None))) finally: self.session.delete('referred') except Exception as e: log.error( 'User {} could not receive an extra life from user {}. traceback: {}' .format(self.session.get('referred', None), user_id, e))
def render_summary(post): """Return the post's summary rendered to HTML.""" renderer = get_renderer(post) match = re.search(CUT_SEPARATOR_REGEX, post.body) if match: return renderer(post.body[:match.start(0)]) else: return utils.smart_truncate(renderer(clean_content(post.body)), config.summary_length)
def shareCheckin(user, game): _trace = TRACE+'shareCheckin():: ' logging.info(_trace+'posting to Facebook '+user.access_token) attachment = {} description = utils.smart_truncate(game.description, length=300) url = 'http://www.supermeeple.com' + game.mid + '/' + game.bgg_id thumbnail = 'http://api.freebase.com/api/trans/image_thumb'+game.mid+'?maxwidth=80&maxheight=100' if game.image_url is not None: thumbnail = game.image_url+'=s100' caption = "SuperMeeple: Board Game Database, Tools and Apps" attachment['caption'] = caption attachment['name'] = game.name attachment['link'] = url #url attachment['description'] = description attachment['picture'] = thumbnail action_link = 'http://www.supermeeple.com'+str(mid)+'/'+str(bgg_id) action_name = "Check In!" actions = {"name": action_name, "link": action_link} attachment['actions'] = actions results = facebook.GraphAPI( user.access_token).put_wall_post(message, attachment) return
def launch_notification(self, reminder_id): due = note = None try: with session_scope() as session: reminder = session.query(Reminder).get(int(reminder_id)) due = reminder.due note = reminder.note reminder.complete = True except Exception as uexc: logger.error(str(uexc)) QtGui.QMessageBox(self, 'Unexpected exception', 'Could not mark due reminder as complete') return # Get local datetime for output to user and format note as html local_due = dt2str(utcstr2local(due, self.time_zone, date_format='%Y-%m-%d %H:%M'), date_format='%d %b %I:%M%p') htmlcontent = '<p>%s</p>' % note # QApplication.instance().beep() # if QtGui.QSound.isAvailable(): # # Seems I would have to recompile with NAS support, but # # what does that mean for python when pyside was pip installed?? # QtGui.QSound.play("media/alarm_beep.wav") media = Phonon.MediaObject() audio = Phonon.AudioOutput(Phonon.MusicCategory) Phonon.createPath(media, audio) # alarm_file = os.path.join(os.getcwd(), 'media/alarm_beep.wav') alarm_file = resource_path('alarm_beep.wav') logger.debug('Trying to open alarm file...%s' % alarm_file) f = QtCore.QFile(alarm_file) if f.exists(): source = Phonon.MediaSource(alarm_file) if source.type() != -1: # -1 stands for invalid file media.setCurrentSource(source) media.play() else: logger.debug('Alert media missing: %s' % alarm_file) # Systray notification self.tray_icon.showMessage(unicode('Reminder due at %s' % local_due), smart_truncate(note, length=100), QtGui.QSystemTrayIcon.Information, 5000) # Dialog notification self.show() dlg = NotificationDialog() dlg.notificationTextBrowser.setHtml(htmlcontent) dlg.dtLabel.setText(local_due) dlg.setWindowTitle(unicode('Due at %s' % local_due)) # Change std buttons to "Reschedule" and "Mark Complete". # Resched will set complete=False and launch the edit reminder with # time selected. "Mark Complete" does nothing, since we already # marked complete to prevent further popups dlg.notificationButtonBox.button( QtGui.QDialogButtonBox.Ok).setText('Mark Complete') dlg.notificationButtonBox.button( QtGui.QDialogButtonBox.Cancel).setText('Reschedule') if dlg.exec_(): logger.debug( 'User wants to close dlg and keep the reminder as completed') else: # Launch edit reminder logger.debug('User wants to reschedule') self.addedit_rem_action_triggered(reminder_id=reminder_id) # Refresh table to account for this reminder completion self.refresh_table()
def refresh_table(self): """ Refreshes (or initially loads) the table according to db First we find which (if any) category is selected, then we filter our reminders based on that. With a list of reminders we repopulate the table, setting human readable dates in due column, which tooltip for exact date, categories in the second column, with tooltip of all categories, note in the third column with tooltip of full note, and fourth and fifth hidden columns that record the utc and id. Storing the UTC in a hidden col is a convenience that saves having to query the db again with the id, e.g. in refresh_human_dates() If the category is the "Complete" category set a dynamic property that will trigger the alt stylsheet row coloring. """ # Init reverse_dates = False root = self.mainTreeWidget.topLevelItem(0) category_name = None indx = None # Get current category if there is one cat = self.mainTreeWidget.currentItem() if cat: category_name = cat.text(self.mainTreeWidget.currentColumn()) indx = root.indexOfChild(cat) with session_scope() as session: # Get reminder instances from database for the given category if indx == 0 and category_name == 'Upcoming': # Selected Upcoming reminders = session.query(Reminder).filter( Reminder.complete == False).all() elif indx == 1 and category_name == 'Complete': # all completed reverse_dates = True reminders = session.query(Reminder).filter( Reminder.complete == True).all() elif indx == 2 and category_name == 'Uncategorized': reminders = session.query(Reminder).filter( Reminder.complete == False).filter( ~Reminder.categories.any()).all() elif indx > 2 and category_name: reminders = session.query(Reminder).filter( Reminder.complete == False).filter( Reminder.categories.any( Category.category_name == category_name)).all() else: # Nothing selected reminders = session.query(Reminder).filter( Reminder.complete == False).all() logger.debug('Refreshing table: cat %s, indx: %s. Reminders %s' % (category_name, indx, reminders)) # Populate the table with the reminders sorted by datetimes reminders = sorted(reminders, key=lambda reminder: datetime.strptime( reminder.due, '%Y-%m-%d %H:%M'), reverse=reverse_dates) self.mainTableWidget.setRowCount( 0) # Delete rows ready to repopulate for inx, reminder in enumerate(reminders): # Insert the row self.mainTableWidget.insertRow(inx) # Due col utc_datetime_str = reminder.due # UTC local_datetime_str = dt2str( utcstr2local(utc_datetime_str, self.time_zone)) arrow_utc_dt = arrow.get(utc_datetime_str, 'YYYY-MM-DD HH:mm') human_due = arrow_utc_dt.humanize() # Human readable dt self.mainTableWidget.setItem(inx, 0, QtGui.QTableWidgetItem(human_due)) self.mainTableWidget.item(inx, 0).setToolTip(local_datetime_str) hours_before = ( (arrow_utc_dt - arrow.utcnow()).total_seconds()) / 3600.0 if hours_before <= 24 and hours_before > 0: # Highlight self.mainTableWidget.item(inx, 0).setBackground(self.soon_color) # Categories col categories = ', '.join([ category.category_name for category in reminder.categories ]) catItem = QtGui.QTableWidgetItem(categories) catTip = u'<font color="black">%s</font>' % '<br/>'.join( [c.category_name for c in reminder.categories]) catItem.setToolTip(catTip) self.mainTableWidget.setItem(inx, 1, catItem) # Note col noteItem = QtGui.QTableWidgetItem(smart_truncate( reminder.note)) noteTip = u"<div style='width: 300px;'>%s</div>" % smart_truncate( reminder.note, length=1000) noteItem.setToolTip(noteTip) self.mainTableWidget.setItem(inx, 2, noteItem) # Hidden cols for UTC string and ID self.mainTableWidget.setItem( inx, 3, QtGui.QTableWidgetItem(utc_datetime_str)) self.mainTableWidget.setItem( inx, 4, QtGui.QTableWidgetItem(unicode(reminder.reminder_id))) # Set the row coloring accordingly if category_name == 'Complete': # Could just directly update stylesheet on table, # but using dynamic properties and [complete=true] # targeting in the style sheet is a little nicer self.mainTableWidget.setProperty('complete', True) self.mainTableWidget.style().unpolish(self.mainTableWidget) self.mainTableWidget.style().polish(self.mainTableWidget) else: self.mainTableWidget.setProperty('complete', False) self.mainTableWidget.style().unpolish(self.mainTableWidget) self.mainTableWidget.style().polish(self.mainTableWidget)
def __repr__(self): return '<Reminder id: %s, complete: %s, due: %s, note: %s>' % (self.reminder_id, self.complete, self.due, smart_truncate(self.note, 100))