My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from waveapi import events
from waveapi import model
from waveapi import robot
from waveapi import document
from waveapi import robot_abstract
from waveapi import simplejson as json

import string
import logging

def AddGadget(properties, context):

root_wavelet = context.GetRootWavelet()
root_blip = context.GetBlipById(root_wavelet.GetRootBlipId())
wavetype = string.lower(root_blip.GetDocument().GetText()[0])

if wavetype == 'a':
waveid = context.GetWaves()[0].GetId()

gadget_arguments = {'waveid' : waveid}
gadget_address = 'http://pgch.co.uk/gadgets/debatewise.Admin.xml'
debatewise_gadget = document.Gadget(gadget_address, gadget_arguments)

root_blip.GetDocument().AppendElement(debatewise_gadget)

def GadgetEventHandler(properties, context):

# get the data from the current wave and its resident gadget
root_wavelet = context.GetRootWavelet()
root_blip = context.GetBlipById(root_wavelet.GetRootBlipId())

debatewise_gadget = None
try:
debatewise_gadget = [x for x in root_blip.GetElements().values() if x.type == document.ELEMENT_TYPE.GADGET][0]
except:
logging.debug('Failed to find the resident gadget. This should not generally happen.')

# make sure we are responding to a legitimate event, not some other blip changes
if int(debatewise_gadget.get('requesttype', '0')) == 1:
gadget_addresses = ['http://pgch.co.uk/gadgets/debatewise.Admin.xml',
'http://pgch.co.uk/gadgets/debatewise.Index.xml',
'http://pgch.co.uk/gadgets/debatewise.Debate.xml',
'http://pgch.co.uk/gadgets/debatewise.Point.xml']
gadget_address = debatewise_gadget.get('url')
new_gadget_address = gadget_addresses[gadget_addresses.index(gadget_address) + 1]

new_wave_titles = ["Admin Gadget",
"Debatewise: Index of debates",
"Debatewise: Debate",
"Debatewise: Point"]
new_wave_title = new_wave_titles[gadget_addresses.index(gadget_address) + 1]

new_content_id = debatewise_gadget.get('nextcontentid')
participants = debatewise_gadget.get('participants').split(',')

bot_address = 'aleksei-testbot@appspot.com'
participants.append(bot_address)

waveid = context.GetWaves()[0].GetId()

# create a wave
new_wave = robot_abstract.NewWave(context, participants)
new_wave.SetTitle(new_wave_title)
new_wave_id = new_wave.GetWaveId()

logging.debug(waveid)
logging.debug(new_wave_id)

# add a gadget to the new wave
gadget_arguments = {'waveid' : new_wave_id, 'contentid' : new_content_id}
new_gadget = document.Gadget(new_gadget_address, gadget_arguments)

new_root_blip = context.GetBlipById(new_wave.GetRootBlipId())
new_root_blip.GetDocument().AppendElement(new_gadget)

# return the new waveId to the current gadget
debatewise_gadget.SubmitDelta({'nextwaveid' : new_wave_id})
debatewise_gadget.SubmitDelta({'requesttype' : 0})

elif int(debatewise_gadget.get('requesttype', '0')) == 2:
waveid = context.GetWaves()[0].GetId()
root_wavelet = context.GetRootWavelet()

new_participant_sets_count = int(debatewise_gadget.get('participantssetscount', '0'))
if new_participant_sets_count > 0:
new_participants_sets_json = debatewise_gadget.get('participantssetsjson', '')

for participant_set in json.loads(new_participants_sets_json):
if waveid == participant_set['waveid']:
for participant in participant_set['participants'].split(','):
root_wavelet.AddParticipant(participant)

debatewise_gadget.SubmitDelta({'requesttype' : 0})

if __name__ == '__main__':
debatewise_robot = robot.Robot('aleksei-testbot',
image_url='http://aleksei-testbot.appspot.com/assets/icon.png',
version='7',
profile_url='http://aleksei-testbot.appspot.com/')

debatewise_robot.RegisterHandler(events.WAVELET_SELF_ADDED, AddGadget)
debatewise_robot.RegisterHandler(events.BLIP_SUBMITTED, GadgetEventHandler)

debatewise_robot.Run()

Change log

r127 by aleksei.gorny on Dec 6, 2009   Diff
Attempt to receive the correct it.
Go to: 
Project members, sign in to write a code review

Older revisions

r123 by aleksei.gorny on Dec 6, 2009   Diff
Reset the gadget state.
r122 by aleksei.gorny on Dec 6, 2009   Diff
Refactoring the bot.
r74 by osirisx11 on Dec 4, 2009   Diff
Updated google code path in testbot
All revisions of this file

File info

Size: 4360 bytes, 104 lines
Powered by Google Project Hosting