Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/extra/upload.py
blob: e39d0993bab2366eb3d9f3f66044fa8e0a95e589 (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/python

try:
    import pycurl
    import xmlrpclib
    _UPLOAD_AVAILABLE = True
except ImportError, e:
    print "Import Error: %s. Project upload is disabled." % (e)
    _UPLOAD_AVAILABLE = False

import gtk
from gettext import gettext as _

class Uploader():
    MAX_FILE_SIZE = 950000
    UPLOAD_SERVER = 'http://turtleartsite.appspot.com'

    def __init__(self, upload_server = None, max_file_size = 0):
        self.uploading = False

        if upload_server is None:
            self._upload_server = self.UPLOAD_SERVER

        if max_file_size is None:
            self._max_file_size = self.MAX_FILE_SIZE

    def set_tw(self, turtleart_window):
        self.tw = turtleart_window

    def enabled(self):
        return _UPLOAD_AVAILABLE

    def do_upload_to_web(self, widget = None):
        if self.uploading:
            return

        self.uploading = False
        self.pop_up = gtk.Window()
        self.pop_up.set_default_size(600, 400)
        self.pop_up.connect('delete_event', self._stop_uploading)
        table = gtk.Table(8, 1, False)
        self.pop_up.add(table)

        login_label = gtk.Label(_('You must have an account at \
http://turtleartsite.sugarlabs.org to upload your project.'))
        table.attach(login_label, 0, 1, 0, 1)
        self.login_message = gtk.Label('')
        table.attach(self.login_message, 0, 1, 1, 2)

        self.Hbox1 = gtk.HBox()
        table.attach(self.Hbox1, 0, 1, 2, 3, xpadding=5, ypadding=3)
        self.username_entry = gtk.Entry()
        username_label = gtk.Label(_('Username:') + ' ')
        username_label.set_size_request(150, 25)
        username_label.set_alignment(1.0, 0.5)
        self.username_entry.set_size_request(450, 25)
        self.Hbox1.add(username_label)
        self.Hbox1.add(self.username_entry)

        self.Hbox2 = gtk.HBox()
        table.attach(self.Hbox2, 0, 1, 3, 4, xpadding=5, ypadding=3)
        self.password_entry = gtk.Entry()
        password_label = gtk.Label(_('Password:') + ' ')
        self.password_entry.set_visibility(False)
        password_label.set_size_request(150, 25)
        password_label.set_alignment(1.0, 0.5)
        self.password_entry.set_size_request(450, 25)
        self.Hbox2.add(password_label)
        self.Hbox2.add(self.password_entry)

        self.Hbox3 = gtk.HBox()
        table.attach(self.Hbox3, 0, 1, 4, 5, xpadding=5, ypadding=3)
        self.title_entry = gtk.Entry()
        title_label = gtk.Label(_('Title:') + ' ')
        title_label.set_size_request(150, 25)
        title_label.set_alignment(1.0, 0.5)
        self.title_entry.set_size_request(450, 25)
        self.Hbox3.add(title_label)
        self.Hbox3.add(self.title_entry)

        self.Hbox4 = gtk.HBox()
        table.attach(self.Hbox4, 0, 1, 5, 6, xpadding=5, ypadding=3)
        self.description_entry = gtk.TextView()
        description_label = gtk.Label(_('Description:') + ' ')
        description_label.set_size_request(150, 25)
        description_label.set_alignment(1.0, 0.5)
        self.description_entry.set_wrap_mode(gtk.WRAP_WORD)
        self.description_entry.set_size_request(450, 50)
        self.Hbox4.add(description_label)
        self.Hbox4.add(self.description_entry)

        self.Hbox5 = gtk.HBox()
        table.attach(self.Hbox5, 0, 1, 6, 7, xpadding=5, ypadding=3)
        self.submit_button = gtk.Button(_('Submit to Web'))
        self.submit_button.set_size_request(300, 25)
        self.submit_button.connect('pressed', self._do_remote_logon)
        self.Hbox5.add(self.submit_button)
        self.cancel_button = gtk.Button(_('Cancel'))
        self.cancel_button.set_size_request(300, 25)
        self.cancel_button.connect('pressed', self._stop_uploading)
        self.Hbox5.add(self.cancel_button)

        self.pop_up.show_all()

    def _stop_uploading(self, widget, event=None):
        """ Hide the popup when the upload is complte """
        self.uploading = False
        self.pop_up.hide()

    def _do_remote_logon(self, widget):
        """ Log into the upload server """
        username = self.username_entry.get_text()
        password = self.password_entry.get_text()
        server = xmlrpclib.ServerProxy(self._upload_server + '/call/xmlrpc')
        logged_in = server.login_remote(username, password)
        if logged_in:
            upload_key = logged_in
            self._do_submit_to_web(upload_key)
        else:
            self.login_message.set_text(_('Login failed'))

    def _do_submit_to_web(self, key):
        """ Submit project to the server """
        title = self.title_entry.get_text()
        description = self.description_entry.get_buffer().get_text(
            *self.description_entry.get_buffer().get_bounds())
        tafile, imagefile = self.tw.save_for_upload(title)

        # Set a maximum file size for image to be uploaded.
        if int(os.path.getsize(imagefile)) > self.max_file_size:
            import Image
            while int(os.path.getsize(imagefile)) > self._max_file_size:
                big_file = Image.open(imagefile)
                smaller_file = big_file.resize(int(0.9 * big_file.size[0]),
                                               int(0.9 * big_file.size[1]),
                                               Image.ANTIALIAS)
                smaller_file.save(imagefile, quality = 100)

        c = pycurl.Curl()
        c.setopt(c.POST, 1)
        c.setopt(c.FOLLOWLOCATION, 1)
        c.setopt(c.URL, self._upload_server + '/upload')
        c.setopt(c.HTTPHEADER, ["Expect:"])
        c.setopt(c.HTTPPOST, [('file', (c.FORM_FILE, tafile)),
                              ('newimage', (c.FORM_FILE, imagefile)),
                              ('small_image', (c.FORM_FILE, imagefile)),
                              ('title', title),
                              ('description', description),
                              ('upload_key', key), ('_formname',
                                                   'image_create')])
        c.perform()
        error_code = c.getinfo(c.HTTP_CODE)
        c.close
        os.remove(imagefile)
        os.remove(tafile)
        if error_code == 400:
            self.login_message.set_text(_('Failed to upload!'))
        else:
            self.pop_up.hide()
            self.uploading = False

if __name__ == "__main__":
    # TODO: create test data...
    u = Uploader(None)
    if u.enabled():
        print "Uploader is enabled... trying to upload"
        u.do_upload_to_web()
        gtk.main()
    else:
        print "Uploader is not enabled... exiting"