def getRatingInfo(extID): ratingCount = Rating.gql('WHERE extID = :1 AND value != :2',extID,0).count(limit=None) if ratingCount > 0: # prevent dividing by zero; the percents already default to zero upvotePercent = Rating.gql('WHERE extID = :1 AND value = :2',extID,1).count(limit=None) * 1.0 / ratingCount * 100 downvotePercent = 100 - upvotePercent else: upvotePercent = 0.0 downvotePercent = 0.0 return ratingCount,upvotePercent,downvotePercent
def get(self): user = users.get_current_user() if user: # Create a new Extension entry newExt = Extension() newExt.developer = user # Give the Extension a random unique ID (I decided on 16 hexadecimal # characters, but other suggestions are welcome). newExt.extID = hashlib.md5(os.urandom(128)).hexdigest()[:16] while Extension.gql('WHERE extID = :1', newExt.extID).count(limit=1) > 0: newExt.extID = hashlib.md5(os.urandom(128)).hexdigest()[:16] newExt.put() # Automatically upvote the extension as the developer # Check if a rating for that extension ID exists # (it should not, but just in case). rating = Rating.gql('WHERE user = :1 AND extID = :2',user,newExt.extID).get() # Create the rating if it does not exist # (which, again, it should not). if not rating: rating = Rating() rating.user = user rating.extID = newExt.extID rating.value = 1 rating.put() # Redirect to the editing page self.redirect('/dev/edit/' + newExt.extID) else: self.redirect(users.create_login_url(self.request.uri))
def get(self,extID): ext = Extension.gql('WHERE extID = :1',extID).get() if not ext: path = os.path.join(os.path.dirname(__file__), 'templates/head.html') self.response.out.write(template.render(path, {'title':'Extension Not Found'})) path = os.path.join(os.path.dirname(__file__), 'templates/ext404.html') self.response.out.write(template.render(path, {})) self.response.set_status(404); else: # Convert the Markdown in the description to HTML, # but escape any HTML added by the user. ext.description = markdown.markdown(text=gfm.gfm(ext.description), safe_mode='escape') # Add the Extension to the template vars dictionary and # set other template vars to their default values. templateVars = { 'ext':ext, 'upvotePercent':0, 'downvotePercent':0, 'ratingCount':0, 'userRating':None, 'starred':False, 'userIsDev':False } if ext.developer: templateVars['devname'] = ext.developer.nickname() templateVars['ratingCount'],templateVars['upvotePercent'],templateVars['downvotePercent'] = getRatingInfo(extID) user = users.get_current_user() if user: userEntry = User.gql('WHERE user = :1',user).get() if userEntry: if extID in userEntry.starred: templateVars['starred'] = True userRating = Rating.gql('WHERE user = :1 AND extID = :2',user,extID).get() if userRating: templateVars['userRating'] = userRating.value else: templateVars['userRating'] = 0 # Every user's default vote is zero if user == ext.developer: templateVars['userIsDev'] = True path = os.path.join(os.path.dirname(__file__), 'templates/head.html') self.response.out.write(template.render(path, {'title':ext.title,'stylesheet':'gallery'})) path = os.path.join(os.path.dirname(__file__), 'templates/info.html') self.response.out.write(template.render(path, templateVars)) path = os.path.join(os.path.dirname(__file__), 'templates/foot.html') self.response.out.write(template.render(path, {}))
def get(self): user = users.get_current_user() if user: self.response.headers['Content-Type'] = 'text/plain' if users.is_current_user_admin(): # Clear the existing index while True: # Get a list of documents populating only the doc_id field and extract the ids. docIDs = [doc.doc_id for doc in galleryIndex.list_documents(ids_only=True)] if not docIDs: break # Remove the documents for the given ids from the Index. galleryIndex.remove(docIDs) extlist = Extension.gql('').fetch(limit=None) ratinglist = Rating.gql('').fetch(limit=None) for ext in extlist: if ext.title == None: ext.title = '' if ext.description == None: ext.description = '' if ext.type == None: ext.type = 'gadget' if ext.category == None: ext.category = 'other' rating = getRatingInfo(ext.extID)[1] doc = search.Document( doc_id=ext.extID, fields=[ search.TextField(name='title', value=ext.title), search.TextField(name='description', value=ext.description), search.AtomField(name='type', value=ext.type), search.AtomField(name='category', value=ext.category), search.NumberField(name='rating', value=rating) ] ) self.response.out.write('Created document for ' + ext.title + ' (' + ext.extID + ')\n') try: galleryIndex.add(doc) self.response.out.write('Successfully added document to index\n') except search.Error: self.response.out.write('Failed to add document to index\n') self.response.out.write('Done!') else: self.response.out.write('Access denied') self.response.set_status(401) else: self.redirect(users.create_login_url(self.request.uri))
def get(self,value,extID): user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) else: if Extension.gql('WHERE extID = :1',extID).count(limit=1) == 0: self.error(404) else: rating = Rating.gql('WHERE user = :1 AND extID = :2',user,extID).get() if not rating: rating = Rating() rating.user = user rating.extID = extID if value == 'up': rating.value = 1 elif value == 'down': rating.value = -1 else: rating.value = 0 rating.put() self.redirect('/gallery/info/' + extID)
def get(self,extID): ext = Extension.gql('WHERE extID = :1',extID).get() if not ext: path = os.path.join(os.path.dirname(__file__), 'templates/head.html') self.response.out.write(template.render(path, {'title':'Extension Not Found'})) path = os.path.join(os.path.dirname(__file__), 'templates/ext404.html') self.response.out.write(template.render(path, {})) self.response.set_status(404); else: # Add the Extension to the template vars dictionary and # set other template vars to their default values. templateVars = { 'ext':ext, 'userRating':None, 'starred':False, 'userIsDev':False } if ext.developer: templateVars['devname'] = ext.developer.nickname() user = users.get_current_user() if user: userEntry = User.gql('WHERE user = :1',user).get() if userEntry: if extID in userEntry.starred: templateVars['starred'] = True userRating = Rating.gql('WHERE user = :1 AND extID = :2',user,extID).get() if userRating: templateVars['userRating'] = userRating.value else: templateVars['userRating'] = 0 # Every user's default vote is zero if user == ext.developer: templateVars['userIsDev'] = True path = os.path.join(os.path.dirname(__file__), 'templates/head.html') self.response.out.write(template.render(path, {'title':ext.title,'stylesheet':'gallery'})) path = os.path.join(os.path.dirname(__file__), 'templates/info.html') self.response.out.write(template.render(path, templateVars)) path = os.path.join(os.path.dirname(__file__), 'templates/foot.html') self.response.out.write(template.render(path, {}))