~nchohan/+junk/mytools

« back to all changes in this revision

Viewing changes to sample_apps/ModelUploader/ModelUploader2.py

  • Committer: root
  • Date: 2010-11-03 07:43:57 UTC
  • Revision ID: root@appscale-image0-20101103074357-xea7ja3sor3x93oc
init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import cgi
 
2
import urllib
 
3
import zlib
 
4
 
 
5
from google.appengine.api import users
 
6
from google.appengine.api import mail
 
7
 
 
8
from google.appengine.ext import webapp
 
9
from google.appengine.ext.webapp.util import run_wsgi_app
 
10
from google.appengine.ext import db
 
11
 
 
12
from google.appengine.ext import blobstore
 
13
from google.appengine.ext.webapp import blobstore_handlers
 
14
 
 
15
'''
 
16
Version 2: Using the Blobstore API to handle larger model files
 
17
'''
 
18
 
 
19
class ColladaModel(db.Model):
 
20
    author = db.StringProperty(multiline=True)
 
21
    model = db.StringProperty(multiline=True)
 
22
    fiducial = db.StringProperty(multiline=False)
 
23
    
 
24
class Fiducial(db.Model):
 
25
    name = db.StringProperty(multiline=False)
 
26
    marker = db.BlobProperty()
 
27
    pic = db.BlobProperty()
 
28
    used = db.BooleanProperty()
 
29
    model = db.StringProperty(multiline=False)
 
30
 
 
31
 
 
32
class MainPage(webapp.RequestHandler):
 
33
    def get(self):
 
34
        blob_url = blobstore.create_upload_url('/upload')
 
35
        self.response.out.write("""
 
36
            <html> 
 
37
            <head><title>Model Uploader</title></head> 
 
38
            <body> 
 
39
                <form action="%s" enctype="multipart/form-data" method="POST"> 
 
40
                    Please enter your email:<br> 
 
41
                    <input name="author" type="text" size="30"> 
 
42
                    <br><br> 
 
43
                    Please select your model:<br> 
 
44
                    <input name="model" type="file"> 
 
45
                    <br><br> 
 
46
                    <input type="submit" value="Submit"> 
 
47
                </form> 
 
48
            </body> 
 
49
            </html>""" % '/upload')#blob_url)
 
50
        
 
51
class UploadModel(blobstore_handlers.BlobstoreUploadHandler):
 
52
    status = False
 
53
    def post(self):
 
54
        collada = ColladaModel()
 
55
        # Need to enforce email
 
56
        collada.author = self.request.get('author')
 
57
        # Only one blobstore upload, first elem in the list
 
58
        collada.model = self.request.get('model')#str((self.get_uploads('model'))[0].key())
 
59
 
 
60
        # Testing model compression to gz
 
61
        cModel = zlib.compressobj()
 
62
        cModel.compress(collada.model)
 
63
        collada.compress = cModel.flush()
 
64
 
 
65
        # Get and assign fiducial marker 
 
66
        fiducials = db.GqlQuery("SELECT * FROM Fiducial WHERE used = False")
 
67
        if fiducials.count() > 0:
 
68
            self.status = True
 
69
            marker = (fiducials.fetch(1))[0]
 
70
 
 
71
            # Currently stores string, fix later
 
72
            collada.fiducial = marker.name
 
73
            marker.model = collada.author 
 
74
 
 
75
            # Send an email with an attachment
 
76
            mail.send_mail \
 
77
                (sender="jnoraky@gmail.com",
 
78
                 to = collada.author,
 
79
                 subject = "ARTag",
 
80
                 body = """
 
81
Attached is your ARTag. Print it and bring it to your destination.
 
82
Your unique ARTag identifier is %s. This will be used to load your
 
83
model on the client.
 
84
 
 
85
The Ops Lab
 
86
                 """ % collada.model,
 
87
                 attachments=[(marker.name, marker.pic)]
 
88
                 )
 
89
 
 
90
            # Disable ARTag for other models
 
91
            marker.used = True
 
92
            # Push to server
 
93
            db.put(marker)
 
94
            db.put(collada)    
 
95
        self.redirect('/')
 
96
 
 
97
'''
 
98
    def redirect(self, uri, permanent=False):
 
99
        if self.status:
 
100
            msg = "Model successfully written to the database. Please \
 
101
                   check your email for the ARTag!"       
 
102
        else:
 
103
            # Add debug info later
 
104
            msg = "Sorry, your model could not be uploaded. Please \
 
105
                   try again."
 
106
        self.response.out.write(
 
107
                """
 
108
                <html><head>
 
109
                <script language="javascript" type="text/javascript">
 
110
                alert("%s");
 
111
                window.location="%s";
 
112
                </script>
 
113
                </head></html>
 
114
                """ % (msg, uri))
 
115
'''
 
116
 
 
117
# For use only in development. In actual instantiation, all
 
118
# fiducial markers will be loaded beforehand.
 
119
class FiducialUploader(webapp.RequestHandler):
 
120
    def get(self):
 
121
        self.response.out.write("""
 
122
            <html> 
 
123
            <head><title>Fiducial Uploader</title></head> 
 
124
            <body> 
 
125
                <form action="/fiducialupload" enctype="multipart/form-data" method="POST"> 
 
126
                    Please enter the marker name:<br> 
 
127
                    <input name="marker" type="text" size="30"> 
 
128
                    <br><br> 
 
129
                    Please upload the image of the fiducial:<br> 
 
130
                    <input name="pic" type="file"> 
 
131
                    <br><br>
 
132
                    Please upload the pattern file of the fiducial:<br>
 
133
                    <input name="fiducial" type="file">
 
134
                    <br><br>
 
135
                    <input type="submit" value="Submit"> 
 
136
                </form> 
 
137
            </body>
 
138
            </html>
 
139
        """)
 
140
 
 
141
class UploadFiducial(webapp.RequestHandler):
 
142
    def post(self):
 
143
        fiducial = Fiducial()
 
144
        fiducial.name = self.request.get('marker')
 
145
        fiducial.pic = db.Blob(self.request.get('pic'))
 
146
        fiducial.marker = db.Blob(self.request.get('fiducial'))
 
147
        fiducial.used = False
 
148
        fiducial.put()
 
149
        self.redirect('/fiducial')
 
150
 
 
151
class Download(webapp.RequestHandler):
 
152
    def get(self, resource):
 
153
        resource = str(urllib.unquote(resource))
 
154
        models = db.GqlQuery("SELECT * FROM ColladaModel WHERE author= 'jnoraky@gmail.com'")
 
155
        if models.count() > 0:
 
156
            model = (models.fetch(1))[0]
 
157
            self.response.headers['Content-Type'] = 'text/xml'
 
158
            dModel = zlib.decompressobj()
 
159
            dModel.decompress(model.compress)
 
160
            self.response.out.write(dModel.flush())
 
161
 
 
162
'''
 
163
# Currently will download first entry in db query list
 
164
class Download(blobstore_handlers.BlobstoreDownloadHandler):
 
165
    def get(self, resource):
 
166
        resource = str(urllib.unquote(resource))
 
167
        models = db.GqlQuery("SELECT * FROM ColladaModel WHERE model = '%s'" % resource)
 
168
        if models.count() > 0:
 
169
            model = (models.fetch(1))[0]
 
170
            self.send_blob(model.model, save_as="model.dae")
 
171
'''        
 
172
application = webapp.WSGIApplication(
 
173
                                    [('/', MainPage), 
 
174
                                     ('/upload', UploadModel),
 
175
                                     ('/fiducial', FiducialUploader),
 
176
                                     ('/fiducialupload', UploadFiducial),
 
177
                                     ('/download/([^/]+)?', Download)],
 
178
                                    debug=True)
 
179
 
 
180
def main():
 
181
    run_wsgi_app(application)
 
182
 
 
183
if __name__ == "__main__":
 
184
    main()