def widget_detail(request, widgetsettings_id): """Widget details page where user can set options defined for the widget """ widget = WidgetSettings.objects.get(pk=widgetsettings_id) opts = widget.get_widget().options(widget) if request.method == "POST": form = options_form(opts)(request.POST) if form.is_valid(): for field in form.cleaned_data: set_option(field, form.cleaned_data[field]) if opts: defaults = [opt.get('default') for opt in opts.values()] initial = get_options(opts.keys(), defaults) form = options_form(opts)(initial=initial) else: form = None extra_context = { 'options_form': form } queryset = WidgetSettings.objects.all() return object_detail(request, queryset, widgetsettings_id, template_name="plugins/widget_detail.html", extra_context=extra_context)
def test_set_option(self): set_option('opt', 'val') option = CustomOption.objects.get(name='opt', value='val') option.delete() set_global_option('opt', 'val') option = CustomOption.objects.get(name='opt', value='val') option.delete()
def test_reset_option(self): set_option('opt', 'val') reset_option('opt', 'new_val') value = get_option('opt', 'wrong value') self.assertEqual(value, 'new_val') set_option('opt', 'val', self.user) reset_option('opt', 'new_val', self.user) value = get_option('opt', 'wrong value', user=self.user) self.assertEqual(value, 'new_val')
def test_set_user_option(self): set_option('opt', 'val', self.user) option = CustomOption.objects.get(name='opt', value='val', user=self.user) option.delete() set_user_option('opt', 'val', self.user) option = CustomOption.objects.get(name='opt', value='val', user=self.user) option.delete()
def test_reset_option(self): """ The reset_option() function should remove all previous definitions of an option and reset its value """ set_option('opt', 'val') reset_option('opt', 'new_val') value = get_option('opt', 'wrong value') self.assertEqual(value, 'new_val') set_option('opt', 'val', self.user) reset_option('opt', 'new_val', self.user) value = get_option('opt', 'wrong value', user=self.user) self.assertEqual(value, 'new_val')
def test_unset_option(self): """ The unset_option() function should remove all definitions of an option """ def check_option(user=None): CustomOption.objects.get(name='opt', value='val', user=user) set_option('opt', 'val') unset_option('opt') self.assertRaises(CustomOption.DoesNotExist, check_option) set_option('opt', 'val', self.user) unset_option('opt', self.user) self.assertRaises(CustomOption.DoesNotExist, check_option)
if not db.init(app_path + u"rtm.db"): options.init() #TODO more db initialization rtm = pyrtm.RTM(apiKey, secret) rtm.token = options.get_option('token') if not rtm.token: frob = options.get_option('frob') if frob: rtm.frob = frob try: rtm.getToken() options.set_option('token',rtm.token) except RTMAPIError: frob = rtm.getNewFrob() else: frob = rtm.getNewFrob() if not token: # we did not get token by frob options.set_option('frob',frob) if appuifw.query(u"I'll now open the browser so you can give me access at RTM.",'query'): e32.start_exe('BrowserNG.exe', ' "4 %s"' % rtm.getAuthURL(), 0) exit() appuifw.note(u"Hi, %s" % rtm.checkToken().fullname) exit()
@with_cursor def get_old_options(cursor): cursor.execute("SELECT convid, option, value FROM options") r = cursor.fetchall() return r @with_cursor def get_bad(cursor): cursor.execute("SELECT id FROM _bad_channels") return [x[0] for x in cursor.fetchall()] @with_cursor def get_hidden(cursor): cursor.execute("SELECT id FROM _hidden_channels") return [x[0] for x in cursor.fetchall()] for (convid, optname, optvalue) in get_old_options(): print("%d %s %s" % (convid, optname, repr(optvalue))) if optname in ['potato', 'group', 'talk_prob', 'yiff']: print('Skipping!') continue options.set_option(convid, optname, optvalue, user_only=False) for convid in get_bad(): print("bad: %d" % convid) options.set_option(convid, 'is_bad', '1', user_only=False) for convid in get_hidden(): print("hidden: %d" % convid) options.set_option(convid, 'is_hidden', '1', user_only=False)
def setLang(jid, lang): jid = jid.split('/')[0] return options.set_option(jid, 'lang', lang)
def set_option(self, k, v): options.set_option(self.jid, k, v)
def set_nick(self, groupchat, nick): options.set_option(groupchat, 'nick', nick)
def test_set_and_get_option(self): set_option('opt', 'val') value = get_option('opt', 'wrong value') self.assertEqual(value, 'val') unset_option('opt')
async def on_message(message): start_time = time() msgcolor = '' ci, cn = channelidname(message.channel) si, sn = serveridname(message.guild) ui = message.author.id un = message.author.name txt = message.content if txt == "": return if options.get_option(si, ci, 'blacklisted') > 0: return cansend = can_send(message.guild, message.channel) channel_ignored = False if message.author.bot: msgcolor = '\033[34m' if options.get_option(si, ci, 'ignore_channel') > 0: channel_ignored = True msgcolor = '\033[90m' if not cansend: msgcolor = '\033[31m' if not message.guild: msgcolor = '\033[96m' if ui == client.user.id: msgcolor = '\033[92m' if not channel_ignored: print(msgcolor + ('%s/%s %s/%s %s/%s : %s' % (sn, si, cn, ci, message.author.name, message.author.id, txt)) + '\033[0m') if message.id in cmd_replies: print('(not logging)') return log_chat(message, si, sn, ci, cn, ui, un, txt, message.author.bot) for u in message.mentions: log_mention(u.id, u.name, u.mention) for r in message.role_mentions: log_role(si, r.id, r.name, r.mention) if not cansend: return if ui == client.user.id: return if channel_ignored == True: return if txt.startswith('/!') and message.author.bot: return if txt.startswith('/!help'): await message.channel.send(embed=make_help()) elif txt.startswith('/!set '): if message.guild and not message.author.permissions_in( message.channel).manage_channels: cmd_replies.add((await message.channel.send( "< only people with manage_channels permission can set options >" )).id) return splt = txt.split() if (len(splt) == 3): opt = splt[1] val = splt[2] try: options.set_option(ci, opt, val) await message.channel.send("< option %s set to %s >" % (opt, val)) except options.OptionError as oe: await message.channel.send("< %s >" % str(oe)) elif (len(splt) == 2): opt = splt[1] try: options.set_option(ci, opt, None) await message.channel.send("< option %s unset >" % (opt, )) except options.OptionError as oe: await message.channel.send("< %s >" % str(oe)) else: cmd_replies.add((await message.channel.send( "< invalid syntax, use /!set option value >")).id) return elif txt.startswith('/!clear'): options.optioncache.clear() print('options cache flushed') elif txt.startswith('/!badword '): if message.guild and not message.author.permissions_in( message.channel).manage_channels: await message.channel.send( "< only people with manage_channels permission can change badwords >" ) return splt = txt.split(' ', 1) bw = splt[1] bws = badword_get(si or ci) if bw in bws: badword_del(si or ci, bw) await message.channel.send("< badword %s deleted >" % (bw)) else: badword_add(si or ci, bw, ui) await message.channel.send("< badword %s added >" % (bw)) elif txt == '/!badword': bws = badword_get(si or ci) await message.channel.send("< badwords: %s >" % repr(bws)) elif txt == '/!list_options': await send_option_list(si, ci, message.channel) else: queued = await nn.queued_for_key(str(ci)) if queued > 32: print('Dropping message, %d messages queued' % queued) return (shld_reply, new_text) = should_reply(si, sn, ci, cn, ui, un, txt, message.guild, message.channel, message.author) if (not message.author.bot) or (len(txt) <= options.get_option( si, ci, 'max_bot_msg_length')): txt2 = new_text for u in message.mentions: txt2 = txt2.replace(u.mention, '@' + u.name) txt2 = txt2.replace('<@!%d>' % u.id, '@' + u.name) for r in message.role_mentions: txt2 = txt2.replace(r.mention, '@' + r.name) for c in message.channel_mentions: txt2 = txt2.replace(c.mention, '#' + c.name) if txt2 != new_text: print(" interpreted as %s" % txt2) await nn.put(str(ci), txt2) if shld_reply: if ci not in currently_sending: currently_sending[ci] = 0 if currently_sending[ci] > 8: print('%d messages being sent, not responding' % currently_sending[ci]) return currently_sending[ci] += 1 try: async with message.channel.typing(): rpl_txt = await nn.get(str(ci), bad_words=badword_get(si or ci)) if rpl_txt == '': print('ignoring empty reply') return rpl_msg = await message.channel.send( rpl_txt, allowed_mentions=discord.AllowedMentions( everyone=False)) finally: currently_sending[ci] -= 1 end_time = time() reply_delay = end_time - start_time if reply_delay > 20: await rpl_msg.edit(content=rpl_txt + ('\n*reply delayed by %f seconds*' % (reply_delay))) print('message took %f seconds to generate' % (reply_delay))