~nchohan/+junk/mytools

« back to all changes in this revision

Viewing changes to sample_apps/ModelUploader/ModelUploaderBlobstoreAPI

  • 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>""" % 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 = str((self.get_uploads('model')[0].key()))
 
59
 
 
60
        # Get and assign fiducial marker 
 
61
        fiducials = db.GqlQuery("SELECT * FROM Fiducial WHERE used = False")
 
62
        if fiducials.count() > 0:
 
63
            self.status = True
 
64
            marker = (fiducials.fetch(1))[0]
 
65
 
 
66
            # Currently stores string, fix later
 
67
            collada.fiducial = marker.name
 
68
            marker.model = collada.author 
 
69
 
 
70
            # Send an email with an attachment
 
71
            mail.send_mail \
 
72
                (sender="jnoraky@gmail.com",
 
73
                 to = collada.author,
 
74
                 subject = "ARTag",
 
75
                 body = """
 
76
Attached is your ARTag. Print it and bring it to your destination.
 
77
Your unique ARTag identifier is %s. This will be used to load your
 
78
model on the client.
 
79
 
 
80
The Ops Lab
 
81
                 """ % collada.model,
 
82
                 attachments=[(marker.name, marker.pic)]
 
83
                 )
 
84
 
 
85
            # Disable ARTag for other models
 
86
            marker.used = True
 
87
            # Push to server
 
88
            db.put(marker)
 
89
            db.put(collada)    
 
90
        self.redirect('/')
 
91
 
 
92
'''
 
93
    def redirect(self, uri, permanent=False):
 
94
        if self.status:
 
95
            msg = "Model successfully written to the database. Please \
 
96
                   check your email for the ARTag!"       
 
97
        else:
 
98
            # Add debug info later
 
99
            msg = "Sorry, your model could not be uploaded. Please \
 
100
                   try again."
 
101
        self.response.out.write(
 
102
                """
 
103
                <html><head>
 
104
                <script language="javascript" type="text/javascript">
 
105
                alert("%s");
 
106
                window.location="%s";
 
107
                </script>
 
108
                </head></html>
 
109
                """ % (msg, uri))
 
110
'''
 
111
 
 
112
# For use only in development. In actual instantiation, all
 
113
# fiducial markers will be loaded beforehand.
 
114
class FiducialUploader(webapp.RequestHandler):
 
115
    def get(self):
 
116
        self.response.out.write("""
 
117
            <html> 
 
118
            <head><title>Fiducial Uploader</title></head> 
 
119
            <body> 
 
120
                <form action="/fiducialupload" enctype="multipart/form-data" method="POST"> 
 
121
                    Please enter the marker name:<br> 
 
122
                    <input name="marker" type="text" size="30"> 
 
123
                    <br><br> 
 
124
                    Please upload the image of the fiducial:<br> 
 
125
                    <input name="pic" type="file"> 
 
126
                    <br><br>
 
127
                    Please upload the pattern file of the fiducial:<br>
 
128
                    <input name="fiducial" type="file">
 
129
                    <br><br>
 
130
                    <input type="submit" value="Submit"> 
 
131
                </form> 
 
132
            </body>
 
133
            </html>
 
134
        """)
 
135
 
 
136
class UploadFiducial(webapp.RequestHandler):
 
137
    def post(self):
 
138
        fiducial = Fiducial()
 
139
        fiducial.name = self.request.get('marker')
 
140
        fiducial.pic = db.Blob(self.request.get('pic'))
 
141
        fiducial.marker = db.Blob(self.request.get('fiducial'))
 
142
        fiducial.used = False
 
143
        fiducial.put()
 
144
        self.redirect('/fiducial')
 
145
 
 
146
# Currently will download first entry in db query list
 
147
class Download(blobstore_handlers.BlobstoreDownloadHandler):
 
148
    def get(self, resource):
 
149
        resource = str(urllib.unquote(resource))
 
150
        models = db.GqlQuery("SELECT * FROM ColladaModel WHERE model = '%s'" % resource)
 
151
        if models.count() > 0:
 
152
            model = (models.fetch(1))[0]
 
153
            self.send_blob(model.model, save_as="model.dae")
 
154
        
 
155
application = webapp.WSGIApplication(
 
156
                                    [('/', MainPage), 
 
157
                                     ('/upload', UploadModel),
 
158
                                     ('/fiducial', FiducialUploader),
 
159
                                     ('/fiducialupload', UploadFiducial),
 
160
                                     ('/download/([^/]+)?', Download)],
 
161
                                    debug=True)
 
162
 
 
163
def main():
 
164
    run_wsgi_app(application)
 
165
 
 
166
if __name__ == "__main__":
 
167
    main()