Exemplo n.º 1
0
  def put(self, guid):
    # find the matching user
    key = db.Key.from_path('User', int(guid))
    user = db.get(key)
    if not user == None:

      # collect the data from the record
      user_json = simplejson.loads(self.request.body)
      # The following keeps Guests and Developers and Testers from being able
      # to change their role.
      currentUserId = self.request.params['UUID']
      cukey = db.Key.from_path('User', int(currentUserId))
      cuser = db.get(cukey)
      if str(user.role) != user_json['role'] and str(cuser.role) != "_Manager":
        user_json['role'] = str(user.role)
        self.response.set_status(401, "Not Authorized")
      # update the record
      user = helpers.apply_json_to_model_instance(user, user_json)
      # save the record
      user.put()
      # return the same record...
      self.response.headers['Content-Type'] = 'application/json'
      self.response.out.write(simplejson.dumps(user_json))
    else:
      self.response.set_status(404, "User not found")
Exemplo n.º 2
0
	def get(self):
		logout = users.create_logout_url(self.request.uri)
		
		user = users.get_current_user()
		category_key = self.request.get('id')
		pairKey = self.request.get('pair')
		showAnswer = self.request.get('show-answer')
		error_message = None
		category = db.get(category_key)
		path = os.path.join(os.path.dirname(__file__), '../templates/category.html')
		pair = None
		if pairKey:
			pair = db.get(pairKey)
		else:
			pair = category.nextPair
		if pair is None:
			self.redirect('/view-stats?category=' + str(category.key()))
		else:
			remaining = category.remaining
			answer = pair.answer
			if category.reverse:
				answer = re.sub(r'\d+\.*', '', answer)
			self.response.out.write(template.render(path, {'pair': pair,
															'answer': answer,
															'reverse': category.reverse,
															'category': category,
															'remaining': remaining,
															'show_answer': showAnswer,
															'logout': logout,
															}))
Exemplo n.º 3
0
	def get(self):
		id=db.Key(self.request.get('id'))
		if not id:
			self.redirect('/error?reason=error')
			return

		allowed= (users.get_current_user() in BlogAdmin)
	
		if not allowed:
			self.redirect('/error?reason=forbidden')
			return
		if not self.request.get('confirm')=='yes':
			object_to_remove=""
			main=MainPage()
			main.isadmin=False
			main.call_user=0
			object=db.get(id)
			if id.kind()=='BlogEntry':
				object_to_remove=main.get_entry(object)
			elif id.kind()=='BlogComment':
				object_to_remove=render_comment(object,main)
				
			values={
				'target':object_to_remove,
				'id':str(object.key())
				}
			path = os.path.join(os.path.dirname(__file__), '../../htmls/page_remove.html')
			self.response.out.write(template.render(path, values))
		else:
			self.remove_daughters(id)
			db.delete(db.get(id))
			self.redirect('/')
Exemplo n.º 4
0
 def delete(self, guid):
   """Delete the task with the given id"""
   if helpers.authorized(self.request.params['UUID'], self.request.params['ATO'], self.request.params['action']):
     # search for the Project and delete if found
     key = db.Key.from_path('Task', int(guid))
     task = db.get(key)
     wantsNotifications = {"true": True, "false": False}.get(self.request.params['notify'].lower())
     currentUserId = self.request.params['UUID']
     cukey = db.Key.from_path('User', int(currentUserId))
     user = db.get(cukey)
     if not task == None:
       # cache current values before updates
       taskName = task.name
       taskType = task.type
       taskPriority = task.priority
       taskStatus = task.developmentStatus
       taskValidation = task.validation
       taskSubmitterId = task.submitterId
       taskAssigneeId = task.assigneeId
       taskEffort = task.effort
       taskProjectId = task.projectId
       taskDescription = task.description
       # Push notification email on the queue if we need to notify
       if notification.should_notify(currentUserId,task,"deleteTask",wantsNotifications):
         taskqueue.add(url='/mailer', params={'taskId': int(guid), 'currentUUID': self.request.params['UUID'], 'action': "deleteTask", 'name': taskName, 'type': taskType, 'priority': taskPriority, 'status': taskStatus, 'validation': taskValidation, 'submitterId': taskSubmitterId, 'assigneeId': taskAssigneeId, 'effort': taskEffort, 'projectId': taskProjectId, 'description': taskDescription})
       task.delete()
       self.response.set_status(204, "Deleted")
     else:
       self.response.set_status(404, "Not Found")
   else:
     self.response.set_status(401, "Not Authorized")
Exemplo n.º 5
0
 def get(self):
     aquery = db.Query(College)
     mycollege= aquery.get()
     username = self.current_user
     key = self.request.get('key')
     ride = db.get(key)
     if ride == None:
         doRender(self, 'error.html', {
             'error_message': "No such ride exists."})
     else:
         ride.passengerobjects = []
         ride.jsmonth = ride.ToD.month
         ride.jsyear = ride.ToD.year
         ride.jsday = ride.ToD.day
         if ride.start_point_title == mycollege.name:
             ride.doOrPu = 0
         else:
             ride.doOrPu = 1
         for p in ride.passengers:
             passenger = db.get(p)
             if (ride.start_point_lat == passenger.lat and ride.start_point_long == passenger.long) or (ride.destination_lat == passenger.lat and ride.destination_long == passenger.lng):
                 passenger.samePlace = True;
             else:
                 passenger.samePlace = False;
             ride.passengerobjects.append(passenger)
         doRender(self, 'rideinfo.html', {
             'ride': ride,
             'mapkey':MAP_APIKEY
         })
Exemplo n.º 6
0
Arquivo: blog.py Projeto: mloar/bloog
def process_article_edit(handler, permalink):
    # For http PUT, the parameters are passed in URIencoded string in body
    body = handler.request.body
    params = cgi.parse_qs(body)
    for key,value in params.iteritems():
        params[key] = value[0]
    property_hash = restful.get_sent_properties(params.get,
        ['title',
         ('body', get_sanitizer_func(handler, trusted_source=True)),
         ('format', get_format),
         ('updated', get_datetime),
         ('tags', get_tags),
         ('html', get_html, 'body', 'format')])

    if property_hash:
        if 'tags' in property_hash:
            property_hash['tag_keys'] = [get_tag_key(name) 
                                         for name in property_hash['tags']]
        article = db.Query(models.blog.Article).filter('permalink =', permalink).get()
        before_tags = set(article.tag_keys)
        for key,value in property_hash.iteritems():
            setattr(article, key, value)
        after_tags = set(article.tag_keys)
        for removed_tag in before_tags - after_tags:
            db.get(removed_tag).counter.decrement()
        for added_tag in after_tags - before_tags:
            db.get(added_tag).counter.increment()
        process_embedded_code(article)
        article.put()
        restful.send_successful_response(handler, '/' + article.permalink)
        view.invalidate_cache()
    else:
        handler.error(400)
Exemplo n.º 7
0
Arquivo: blog.py Projeto: mloar/bloog
def process_article_submission(handler, article_type):
    property_hash = restful.get_sent_properties(handler.request.get, 
        ['title',
         ('body', get_sanitizer_func(handler, trusted_source=True)),
         'legacy_id',
         ('format', get_format),
         ('published', get_datetime),
         ('updated', get_datetime),
         ('tags', get_tags),
         ('html', get_html, 'body', 'format'),
         ('permalink', permalink_funcs[article_type], 'title', 'published')])

    if property_hash:
        if 'tags' in property_hash:
            property_hash['tag_keys'] = [get_tag_key(name) 
                                         for name in property_hash['tags']]
        property_hash['format'] = 'html'   # For now, convert all to HTML
        property_hash['article_type'] = article_type
        article = models.blog.Article(**property_hash)
        article.set_associated_data(
            {'relevant_links': handler.request.get('relevant_links'),
             'amazon_items': handler.request.get('amazon_items')})
        process_embedded_code(article)
        article.put()
        for key in article.tag_keys:
            db.get(key).counter.increment()
        do_sitemap_ping()
        restful.send_successful_response(handler, '/' + article.permalink)
        view.invalidate_cache()
    else:
        handler.error(400)
Exemplo n.º 8
0
    def post(self, post_id):
        # code to retrieve selected blog for comment
        blogKey = db.Key.from_path('Blog', int(post_id))
        SelectedBlog = db.get(blogKey)
        # get Userid
        currentUser = self.checkCurrentUser()
        if currentUser:
            # get username and user key
            userKey = currentUser.key()
            username = currentUser.name
            # code to retrieve user input comment
            userComment = self.request.get('user_comment')
            #Save userComment data in db
            savedComment = CommentDB(comment = str(userComment) , blogkey= str(blogKey), userkey= str(userKey), username = str(username))
            commentKey = savedComment.put()

            # search commented blog based on blog Key in Blog dB
            refblog = db.get(blogKey)
            refblogCommentCount = refblog.commentCount
            if refblogCommentCount:
                refblogCommentCount= int(refblogCommentCount) + 1
            else:
                refblogCommentCount= 1

            refblog.commentCount = refblogCommentCount
            key = refblog.put()
            self.redirect("/blog/%s" % key.id())            
            self.redirect("/blog/%s" % key.id())            

        else:
            #if user not logged in ask user to Login
            self.render('login.html', alert="Please login First.")
Exemplo n.º 9
0
 def get(self, post_id):
     currentUser = self.checkCurrentUser()
     if currentUser:
         username = currentUser.name
         # code to retrieve selected comment for edit
         key = db.Key.from_path('CommentDB', int(post_id))
         SelectedComment = db.get(key)
         commentor = SelectedComment.username
         blogKey = SelectedComment.blogkey
         selectedBlog = db.get(blogKey)
         if username == commentor :
             # code to delete comment
             db.delete(key)
             #Code to update comment count
             refblogCommentCount = selectedBlog.commentCount
             if refblogCommentCount > 0:
                 refblogCommentCount= int(refblogCommentCount) - 1
             else:
                 refblogCommentCount= 0
             selectedBlog.commentCount = refblogCommentCount
             blogKey = selectedBlog.put()
             # code to redirect to selected blog
             self.redirect("/blog/%s" % blogKey.id())
             self.redirect("/blog/%s" % blogKey.id())
         else:
             self.render("alert.html",currentUser=currentUser.name, message = "Warning! You are not authorized to Delete this comment. Thanks.")
     else:
         #if user not logged in ask user to Login
         self.render('login.html', alert="Please login First.")  
Exemplo n.º 10
0
    def get(self, post_id):
        age = memcache.get('age%s' % post_id)
        if age is None:
            logging.error('AGE INITIALIZED')
            age = 0
            memcache.set('age%s' % post_id, time.time())
        else:
            age = int(time.time() - age)

        key = db.Key.from_path('Post', int(post_id), parent=blog_key())
        post = db.get(key)
        
        logged_in = False
        username = ''
        user_id = self.request.cookies.get('user_id')
        if user_id:
            logged_in = True
            key = db.Key.from_path('User', int(user_id), parent=blog_key())
            u = db.get(key)
            username = u.username

        if not post:
            self.error(404)
            return

        self.render("permalink.html", logged_in = logged_in, 
                                      username = username, 
                                      post = post, 
                                      age = age)
Exemplo n.º 11
0
    def get(self, post_k):

        # Will be returning JSON.
        self.response.headers['Content-Type'] = 'application/json'

        # Find entry for current user.
        user_k = get_user_entity_from_username(
            get_current_username(self.request.cookies)).key()

        query = db.GqlQuery(
            ("SELECT * FROM BlogPostReaction "
                "WHERE blog_post = :1 AND site_user = :2"),
            db.get(post_k),
            db.get(user_k))

        old_reaction = query.get()

        current_value = ""
        if old_reaction is not None:
            current_value = old_reaction.reaction_type

        # Generate data to return and return it.
        obj = {'success': True,
               'value': current_value}

        self.response.out.write(json.dumps(obj))
Exemplo n.º 12
0
 def post(self):
     c_id = self.request.get('id')
     # Authentication
     if not self.user:
         self.redirect('/login')
     else:
         ckey = db.Key.from_path('Comment', int(c_id), parent=comment_key())
         comm = db.get(ckey)
         params = dict(id = int(c_id), comment = comm)
         # Check if the user is the author of the comment
         # Authorization
         if self.user.name == comm.author:
             #Obtain id from textbox
             self.id = self.request.get('id')
             ckey = db.Key.from_path('Comment', int(self.id), parent=comment_key())
             comm = db.get(ckey)
             # Obtain new comment from textarea
             self.comm = self.request.get('comment')
             #Edit the comment and push it to the datastore
             comm.comment = self.comm
             comm.put()
             self.redirect('/blog/%s' % comm.post)
         elif not comm:
             self.error(404)
             return
         else:
             self.redirect('/blog/%s' % post.key().id())
Exemplo n.º 13
0
 def post(self):
    c_id = self.request.get('id')
    # Authetnication
    if not self.user:
        self.redirect('/login')
    else:
        ckey = db.Key.from_path('Comment', int(c_id), parent=comment_key())
        comm = db.get(ckey)
        params = dict(id = int(c_id), comment = comm)
        # Check if user is the author of the comment
        # Authorization
        if self.user.name == comm.author:
            # Obtain id from textbox
            self.id = self.request.get('id')
            ckey = db.Key.from_path('Comment', int(self.id), parent=comment_key())
            comm = db.get(ckey)
            pkey = db.Key.from_path('Post', int(comm.post), parent=blog_key())
            post = db.get(pkey)
            #Delete comment and decrement from datastore
            comm.delete()
            post.comments = post.comments - 1
            post.put()
            self.redirect('/blog/%s' % post.key().id())
        elif not comm:
            self.error(404)
            return
        else:
            self.redirect('/blog/%s' % post.key().id())
Exemplo n.º 14
0
		def append_websites (document, person_element, faculty):
			website_query = db.get(faculty.websites)
			#website_query = db.GqlQuery("SELECT * FROM Website WHERE person = :this_person", this_person=faculty.key())			
			for website in website_query:
				# main tag
				website_element = document.createElement('website')
				person_element.appendChild(website_element)

				#one url, one group, one or more author

				append_text_node(document, website_element, 'url', website.address)
				append_text_node(document, website_element, 'group', website.group.name)
				
				for author in website.authors:
					author_element = document.createElement('author')
					website_element.appendChild(author_element)

					append_text_node(document, author_element, 'first_name', db.get(author).first)
					append_text_node(document, author_element, 'last_name', db.get(author).last)

					middle = db.get(author).middle
					if middle is not None:
						append_text_node(document, author_element, 'middle_name', middle)
										
					suffix = db.get(author).suffix
					if suffix is not None:
						append_text_node(document, author_element, 'suffix', suffix)
Exemplo n.º 15
0
 def post(self):
     post_id = self.request.get('id')
     # Authentication
     if not self.user:
         self.redirect('/login')
     else:
         pkey = db.Key.from_path('Post', int(post_id), parent=blog_key())
         post = db.get(pkey)
         # Check if the user is the author of the post
         # Authorization
         if self.user.name != post.author:
             self.redirect('/blog/%s' % post_id)
         else:
             # Obtain id from the textbox
             self.id = self.request.get('id')
             pkey = db.Key.from_path('Post', int(self.id), parent=blog_key())
             post = db.get(pkey)
             # Get subject and content
             self.subject = self.request.get('subject')
             self.content = self.request.get('content')
             params = dict(subject = self.subject, content = self.content, id = self.id)
             # Sets the subject the content and pushes the edited post to the datastore
             post.subject = self.subject
             post.content = self.content
             post.put()
             self.redirect('/blog/%s' %self.id)
Exemplo n.º 16
0
 def get(self, one_num):
     if 'Referer' in self.request.headers:
         go = self.request.headers['Referer']
     else:
         go = '/'
     member = CheckAuth(self)
     t = self.request.get('t')
     if member:
         if str(member.created_ts) == str(t):
             one = GetKindByNum('Member', int(one_num))
             if one is not False:
                 if one.num != member.num:
                     q = db.GqlQuery("SELECT * FROM MemberBookmark WHERE one = :1 AND member_num = :2", one, member.num)
                     if q.count() > 0:
                         bookmark = q[0]
                         bookmark.delete()
                         member = db.get(member.key())
                         member.favorited_members = member.favorited_members - 1
                         member.put()
                         memcache.set('Member_' + str(member.num), member, 86400)
                         n = 'r/m' + str(one.num) + '/m' + str(member.num)
                         memcache.delete(n)
                         one = db.get(one.key())
                         one.followers_count = one.followers_count - 1
                         one.put()
                         memcache.set('Member_' + str(one.num), one, 86400)
                         memcache.set('Member::' + str(one.username_lower), one, 86400)
     self.redirect(go)
Exemplo n.º 17
0
    def post(self):
            import time
            date = time.strftime('%Y-%m-%d', time.gmtime())
            k = db.Key(self.request.get('clientkey'))
            invoice = Invoices()
            invoice.client = k
            invoice.date = date
            invoice.status = 'draft'
            invoice.total = 0.00
            invoice.put()
            iid = invoice.key()
            invoiceid = str(invoice.inum)
            billedtotal = 0.00
            billedtime = 0.00
            # there is a form that lists the projects, allowing the user to 
            # check one or more the field data is the key for that project
            # get_all gives an array of keys returned which we want to pull 
            # time from
            projects = self.request.get_all('projects')
            # start looping through the keys
            for projectkey in projects:
                # make the string key an actual key object
                pkey = db.Key(projectkey)
                # get everything out of the time store with that project 
                # associated and which has a status of logged.
                times_query = Time.all()
                times_query.filter('project =', pkey)
                times_query.filter('status =', 'logged')
                times = times_query.fetch(100)
                
                for time in times:


                    time.invoice = iid
                    time.status = "invoiced"
                    db.put(time)

                    billedtime = float(billedtime) + float(time.hours)
                    billedtotal = float(billedtotal) + float(time.total)
                    
                project_update = db.get(pkey)
                project_update.status = "empty"
                project_update.put()
            
            

            totalhoursbilled = "%2.f" % billedtime
            totalbill = "%.2f" % billedtotal
            totalbill = float(totalbill)

            invoice_update = db.get(iid)
            invoice_update.totalhours = float(totalhoursbilled)
            invoice_update.totalbill = totalbill
            invoice_update.put()

            project_update = db.get(pkey)
            project_update.billed = billedtime
            project_update.put()
            action = '/invoice?iid=' + str(iid)            
            self.redirect(action)
Exemplo n.º 18
0
    def get(self, mode, id):

        articles = models.Article.all()
        def mode_mapper(m):
            if m=='page': 
                return 'slug'
            elif m=='tag':
                return 'tags'
            else:
                return m
        articles.filter("%s =" % mode_mapper(mode), id)
        article = articles.fetch(1)
        for art in article:
            rimages = []
            rheights = []
            for image in art.images:
                rimages.append(db.get(image))
                img = images.Image(blob_key=str(db.get(image).file.key()))
                img.im_feeling_lucky()
                img.execute_transforms(output_encoding=images.JPEG,quality=1)
                rheights.append(float(img.height) / float(img.width))
            art.rimages = rimages

        articles = models.Article.all().order('-pubdate').fetch(20)
        for sarticle in articles:
            sarticle.rimages = [db.get(image) for image in sarticle.images]

        self.generate('page.html',
            template_values = {
                'articles' : articles,
                'article' : article,
                'rheights' : rheights,
                '%sid' % mode: id,
                })
Exemplo n.º 19
0
 def put(self, key):
     '''Adds a Character to a Party.
     '''
     _trace = TRACE+'APIParties:: put() '
     logging.info(_trace)        
     _party = db.get(key)        
     if _party is not None and _party.class_name() == 'Party':
         try:
             character_key = self.request.get('character_key')
         except AttributeError:
             r = API400
             r[MSG] = 'Missing \'character_key\' parameter.'
         
         character = db.get(character_key)
         if character is not None and character.class_name() == 'Character':
             _party = party.updateJSONParty(party, character)
             r = API200
             r[MSG] = 'The party grows stronger!'
             r[_party.class_name()] = _party
         else:
               r = API404
               r[MSG] = 'Character not found for key '+character_key+' .'                  
         
         r = API200
         r[MSG] = 'Adventure a party makes.'
         r[_party.class_name()] = party.getJSONParty(party)
     else:
         r = API404
         r[MSG] = 'Party not found for key '+key+' .'  
           
     return self.response.out.write(simplejson.dumps(r))
Exemplo n.º 20
0
  def post(self):
    group_k = self.request.get("key")
    group = db.get(group_k)
    member_k = db.Key.from_path('Person', self.request.get('email'))
    member = db.get(member_k)
    if member == None:
      response = {
        "success": False,
        "error": "No such user found"
      }

    elif self.request.get('action') == "add":
      if group.key() not in member.groups:
        member.groups.append(group.key())
        member.put()
        response = {
        "success": True,
        "member": member.name
        }
      else:
        response = {
          "success": False,
          "error": "User is already in group"
        }
    elif self.request.get('action') == "remove":
      member.groups.remove(group.key())
      member.put()
      response = {"success": True}

    self.response.content_type = 'application/json'
    self.response.write(json.dumps(response))
Exemplo n.º 21
0
 def assertEqualTree(self, tree, parent='0', ancestors=[u'root'], cls=Menu):
     pos = 0
     expected_sibs = [unicode(k[0]) for k in tree]
     for k in tree:
         node = find_node(k[0],cls)
         # path
         self.assertEqual(node.path,u'%s%s%s' % (parent, u'.', pos))
         # siblings
         node_sibs = [db.get(n).name for n in node.siblings]
         node_sibs.sort()
         expected_sibs.remove(k[0])
         expected_sibs.sort()
         self.assertEqual(expected_sibs, node_sibs)
         expected_sibs.append(k[0])
         # ancestors
         node_ancestors = [db.get(n).name for n in node.ancestors]
         self.assertEqual(ancestors, node_ancestors)
         if len(k) > 1:
             # children
             expected_children = [unicode(l[0]) for l in k[1]]
             expected_children.sort()
             node_children = [db.get(n).name for n in node.children]
             node_children.sort()
             self.assertEqual(expected_children, node_children)
             # recurse
             self.assertEqualTree(k[1], u'%s%s%s' % (parent, u'.', pos), ancestors+[node.name])
         pos += 1
Exemplo n.º 22
0
def get_user(user=None, user_id=None):
    '''Get a user from the DataStore using a User object or a user ID'''
    id = user.user_id() if user else user_id
    if id == 'us':
        return False
    else:
        e = mget(key=id, namespace='profile_data')
        if e is None:
            try:
                q = User.all().filter('id', id).fetch(1)
                e = q[0]
            except:
                u = User(
                    id=id,
                    user=user,
                    nickname=user.nickname(),
                )
                e = db.get(u.put())
        if e.bio is None or e.shared is None:
            if e.bio is None:
                m = User_Bio().put()
                e.bio = m
            if e.shared is None:
                p = User_Permissions().put()
                e.shared = p
            e = db.get(db.put(e))
        mset(key=id, value=e, time=10, namespace='profile_data')
        return e
Exemplo n.º 23
0
    def test_put_expiry_flags(self):
        # Try put_expiry_flags when the record has not expired yet.
        assert not self.p1.is_expired
        self.p1.put_expiry_flags()

        # Both entities should be unexpired.
        p1 = db.get(self.p1.key())
        assert p1.expiry_date
        assert not p1.is_expired
        assert p1.given_name == 'John'
        n1_1 = db.get(self.n1_1.key())
        assert not n1_1.is_expired

        # Advance past the expiry date and try again.
        set_utcnow_for_test(datetime(2010, 2, 3))
        p1.put_expiry_flags()

        # Both entities should be expired.
        p1 = db.get(self.p1.key())
        assert p1.is_expired
        assert p1.given_name == 'John'
        assert p1.source_date == datetime(2010, 2, 3)
        assert p1.entry_date == datetime(2010, 2, 3)
        assert p1.expiry_date == datetime(2010, 2, 1)
        n1_1 = db.get(self.n1_1.key())
        assert n1_1.is_expired
Exemplo n.º 24
0
	def post(self):
		question = db.get(self.request.get('question'))
		quiz = db.get(self.request.get('quiz'))
		# cannot copy a question into another user's quiz
		if quiz.owner == users.get_current_user():
			question.copyto(quiz)
		self.redirect('/questions?quiz=%s' % (quiz.key(),))
Exemplo n.º 25
0
	def post(self):
		quiz = db.get(self.request.get('quiz'))
		category = db.get(self.request.get('category'))
		if quiz.owner == users.get_current_user() or users.is_current_user_admin():
			quiz.category = category
			quiz.put()
		self.redirect('/')
Exemplo n.º 26
0
	def get(self):
		bbs = db.get(self.request.get("bbs_key"))
		user = users.get_current_user()
		entry = db.get(self.request.get("entry_key"))

		entry_owner=False
		if(user and user.user_id()==entry.user_id):
			entry_owner=True
		
		bbs_owner=not OwnerCheck.check(bbs,user)

		if(not bbs_owner and not OwnerCheck.is_admin(user) and not entry_owner):
			self.response.out.write(Alert.alert_msg("削除する権限がありません。",self.request.host))
			return
		entry.del_flag = 0
		entry.put()

		thread = db.get(self.request.get("thread_key"))
		thread.comment_cnt=thread.comment_cnt-1
		thread.cached_entry_key=[]
		thread.cached_entry_key_enable=None
		thread.put()

		url=MappingThreadId.get_thread_url("./",bbs,thread)+"?comment_edit=1"
		self.redirect(str(url))

		RecentCommentCache.invalidate(bbs)
    def _un_subscribe(self, app_user, si_user, broadcast_type):
        user_profile, si, fsic = db.get([UserProfile.createKey(app_user),
                                         ServiceIdentity.keyFromUser(add_slash_default(si_user)),
                                         FriendServiceIdentityConnection.createKey(app_user, si_user)])

        logging.info('%s is unsubscribing from notifications of "%s" with type "%s".',
                     user_profile.name if user_profile else app_user.email(),
                     si.name if si else si_user.email(),
                     broadcast_type)

        updated = False
        if fsic:
            if broadcast_type in fsic.enabled_broadcast_types:
                fsic.enabled_broadcast_types.remove(broadcast_type)
                updated = True
            if broadcast_type not in fsic.disabled_broadcast_types:
                fsic.disabled_broadcast_types.append(broadcast_type)
                updated = True

        if updated:
            fsic.put()
            models = db.get([UserData.createKey(fsic.friend, fsic.service_identity_user)] +
                             [get_mobile_key_by_account(mobile.account) for mobile in user_profile.mobiles])
            user_data_model, mobiles = models[0], models[1:]
            create_send_user_data_requests(mobiles, user_data_model, fsic, fsic.friend, fsic.service_identity_user)
            schedule_update_a_friend_of_a_service_identity_user(fsic.service_identity_user, fsic.friend, force=True,
                                                                clear_broadcast_settings_cache=True)
        else:
            logging.info('%s was already unsubscribed from notifications of "%s" with type "%s".',
                         user_profile.name if user_profile else app_user.email(),
                         si.name if si else si_user.email(),
                         broadcast_type)

        return updated, user_profile, si, fsic
Exemplo n.º 28
0
Arquivo: main.py Projeto: hassy/Mowars
 def post(self):
     
     loser = db.get(str(self.request.get('loser')))
     winner = db.get(str(self.request.get('winner')))
     vote = Vote(winner=winner, loser=loser)
     
     #To store username if the user is logged in
     client = OAuthClient("twitter", self)
     logged_in = client.get_cookie()
     
     if logged_in:
         info = client.get("/account/verify_credentials")
         vote.name = info["screen_name"]
     else:
         vote.name = 'Guest'
     
     #TODO: Stop spam-voting for same key from same ip repeatedly
     vote.ip = self.request.remote_addr
     
     if check_vote_spam(vote.ip, winner):
         self.redirect('/msg=You+appear+to+be+votespamming,+please+stop+or+try+again+later')
     else:
         vote.put()
     
         #Wins and losses
         loser.losses = loser.losses+1
         loser.win_percentage = loser.calc_win_percentage()
         loser.put()
     
         winner.wins = winner.wins+1
         winner.win_percentage = winner.calc_win_percentage()
         winner.put()
     
         #Redirect so that people cannot repost twice
         self.redirect('/?w=%s&l=%s' % (winner.key().id(), loser.key().id()))
Exemplo n.º 29
0
    def getall(self,catname=None,tagname=None):
        data=[]
 
        if catname:
            catkey=[categoryobj.key() for categoryobj in self.categories if categoryobj.category==catname][0]
            posts=[]
            [posts.append(post) for post in self.posts if catkey ==post.category.key()]
        #logging.info([catkey,len(categories)])
        elif tagname:
            logging.info(tagname)
            tagkey=[tagobj.key() for tagobj in self.tags if tagobj.tag==tagname]
            if tagkey:tagkey=tagkey[0]
            posts=[]
            [posts.append(post) for post in self.posts if tagkey in post.tags]
           
        else:posts=self.posts
        

        
        for post in posts:
           
            tags=[]
            [tags.append({"tag":db.get(key).tag,"tagid":db.get(key).key().id()}) for key in post.tags]
      
            updated=str(post.updated.day)+" "+str(months[post.updated.month])+" "+str(post.updated.year)
            dateposted=str(post.timestamp.day)+" "+str(months[post.timestamp.month])+" "+str(post.timestamp.year)
            data.append({"title":post.title,"body":post.body,"category":db.get(post.category.key()).category,
                         "catid": db.get(post.category.key()).key().id(),"id":str(post.key().id()),\
                        "tags":tags,"date":dateposted,"updated":updated})
        return(data)
Exemplo n.º 30
0
  def post(self):
    group_k = self.request.get("key")
    if group_k == "":
      parent_key = db.Key.from_path('Person', users.get_current_user().email())
      group = Group(parent=parent_key)
      group.name = self.request.get('groupname')
      group.put()
      
      group_k = group.key()
      person = db.get(parent_key)
      person.groups.append(group_k)
      person.put()

      response = {
        "success": True,
        "key": str(group.key()),
        "email": parent_key.name(),
        "name": person.name
      }
    else:
      group = db.get(group_k)
      for member in group.members:
        member.groups.remove(group.key())
        member.put()
      db.delete(group_k)

      response = {"success": True}

    self.response.content_type = 'application/json'
    self.response.write(json.dumps(response))
 def isFileExists(self, fileName):
     key = db.Key.from_path('FileInfo', fileName)
     file_info = db.get(key)
     return file_info
Exemplo n.º 32
0
 def do_update():
     e = db.get(key)
     if self.update(e):
         e.put()
     if datetime.datetime.now() > deadline:
         raise DeadlineExceededError
Exemplo n.º 33
0
    def by_id(cls, id):
        key = db.Key.from_path('Comment', int(id))
        comment = db.get(key)

        if comment:
            return comment
Exemplo n.º 34
0
    def by_id(cls, id):
        key = db.Key.from_path('Post', int(id), parent=blog_key())
        post = db.get(key)

        if post:
            return post
Exemplo n.º 35
0
def increment_counter(key):
	obj = db.get(key)
	obj.counter += 1
	obj.put()
	
	return obj.counter
Exemplo n.º 36
0
    def post(self):

        LoggedIn = False
        User = Team = ""
        path = os.environ['PATH_INFO']

        if (self.session.get('user')):  # (NEWSESSION)
            LoggedIn = True
            User = self.session['fname'] + " " + self.session['lname']
            Team = " (" + self.session['team'] + ")"

        if (LoggedIn == False):
            self.Writeln("<p><br><p><br>")
            self.Writeln("<body bgcolor=d2d2ff>")
            self.Writeln("<center>")
            self.Writeln("<h1>Please Login </h1>")
            self.Writeln("</center>")
            return

        courts = self.request.get('courts')
        if (courts == ""):
            self.Writeln("<p><br><p><br>")
            self.Writeln("<body bgcolor=d2d2ff>")
            self.Writeln("<center>")
            self.Writeln("<h1>Please Select Courts </h1>")
            self.Writeln("</center>")
            return

        e = db.get(courts)
        #       This shouldn't ever happen since reserved court can't be selected
        if (type(e.owner) is not types.NoneType):
            self.Writeln("<p><br><p><br>")
            self.Writeln("<body bgcolor=d2d2ff>")
            self.Writeln("<center>")
            self.Writeln("<h1>Courts already Selected </h1>")
            self.Writeln("</center>")
            return

# -------------------------------------------------------------------
#       By now, use has logged in and selected courts

# (NEWSESSION)
        captain_keyname = self.session['keyname']
        Captain = datastore.Captain.get_by_key_name(captain_keyname)
        CourtTime = ""

        g = datastore.OpenDate.get_by_key_name("key_opendate")
        if (g == None):
            self.Writeln("<p><br><p><br>")
            self.Writeln("<center>")
            self.Writeln("<body bgcolor=d2d2ff>")
            self.Writeln("WARNING! Set Opening Date For Reservations")
            return

        start = g.openingdate

        StartDate = library.getDate(g.openingdate)
        StartTime = library.ctime(g.openingdate)
        Today = library.getDate(datetime.datetime.utcnow() -
                                datetime.timedelta(hours=8))
        Today = Today + " @" + library.ctime(datetime.datetime.utcnow() -
                                             datetime.timedelta(hours=7))

        delta = datetime.datetime.utcnow() - datetime.timedelta(
            hours=8) - start

        if (datetime.datetime.utcnow() - datetime.timedelta(hours=8) < start):
            self.Writeln("<p><br><p><br>")
            self.Writeln("<center>")
            self.Writeln("<body bgcolor=d2d2ff>")
            self.Writeln("<h1>Can't Reserve yet </h1>")
            self.Writeln("<h1>Today is " + Today + "</h1>")
            self.Writeln("<h1>Reservations start  " + StartDate + "@" +
                         StartTime + "</h1>")
            self.Writeln("</center>")
            return

        Allowed = 2 + 2 * delta.days

        sofar = Captain.count

        if (sofar >= Allowed):
            Exceeded = True
            template_values = {
                'Host': library.Host(),
                'StartDate': StartDate,
                'StartTime': StartTime,
                'Today': Today,
                'Allowed': Allowed,
                'Captain': Captain,
                'Exceeded': True
            }

            path = os.path.join(os.path.dirname(__file__),
                                'display_request.html')
            self.response.out.write(template.render(path, template_values))
            return


# ----------------------------------------------------------------------------------
#       Reserve the courts

#       courts is the Key to the court selected
        courts = cgi.escape(self.request.get('courts'))
        e = db.get(courts)

        CourtDate = library.getDate(e.date)
        CourtDay = e.weekday
        CourtStart = library.ctime(e.start)
        CourtEnd = library.ctime(e.end)
        Courts = library.listconv(e.courts)
        CourtDesc = e.desc

        Month = e.date.month
        Year = e.date.year

        e.owner = captain_keyname  # either use session value or from Captain db
        db.put(e)

        Captain.count = Captain.count + 1
        db.put(Captain)

        template_values = {
            'Host': library.Host(),
            'Month': Month,
            'Year': Year,
            'StartDate': StartDate,
            'StartTime': StartTime,
            'Today': Today,
            'Allowed': Allowed,
            'Captain': Captain,
            'CourtDate': CourtDate,
            'CourtDay': CourtDay,
            'CourtStart': CourtStart,
            'CourtEnd': CourtEnd,
            'Courts': Courts,
            #           'CourtDesc'  : CourtEnd,
            'Exceeded': False
        }

        path = os.path.join(os.path.dirname(__file__), 'templates',
                            'reserve.html')
        self.response.out.write(template.render(path, template_values))
Exemplo n.º 37
0
  xml_content = forms["xml"].value
  xml_hash = hash(xml_content)
  lookup_query = db.Query(Xml)
  lookup_query.filter("xml_hash =", xml_hash)
  lookup_result = lookup_query.get()
  if lookup_result:
    xml_key = lookup_result.key().name()
  else:
    trials = 0
    result = True
    while result:
      trials += 1
      if trials == 100:
        raise Exception("Sorry, the generator failed to get a key for you.")
      xml_key = keyGen()
      result = db.get(db.Key.from_path("Xml", xml_key))
    xml = db.Text(xml_content, encoding="utf_8")
    row = Xml(key_name = xml_key, xml_hash = xml_hash, xml_content = xml)
    row.put()
  print xml_key

if "key" in forms:
  # Retrieve stored XML based on the provided key.
  key_provided = forms["key"].value
  # Normalize the string.
  key_provided = key_provided.lower().strip()
  # Check memcache for a quick match.
  xml = memcache.get("XML_" + key_provided)
  if xml is None:
    # Check datastore for a definitive match.
    result = db.get(db.Key.from_path("Xml", key_provided))
Exemplo n.º 38
0
  def get(self):
    self.session = Session()
    userkey=self.session['userkey']
    user=db.get(userkey)

    doRender(self, 'changeDetails.html', {'name' : user.name, 'surname' : user.surname})
Exemplo n.º 39
0
    def get(self):
        """Get handler for comparing test suites."""
        # Let's preprocess all request parameters.
        params = self.ProcessRequestParams()
        suite_key = params['suite_key']
        if not suite_key:
            self.response.out.write('No suite key specified.')
            return
        if suite_key.lower() == 'latest':
            suite = test_suite.TestSuite.all().order('-date')[0]
        else:
            suite = db.get(db.Key(suite_key))
        if not suite:
            self.response.out.write('No Matching suites found.')
            return

        processed_results = self.FetchAndProcessPageDelta(params, suite)
        delta_count = suite.results.count()
        data_count = suite.page_data_set.count()

        # Let's create URLs/links for template.
        start = params['offset']
        end = min(params['offset'] + params['limit'] - 1, delta_count)
        next_ = params['offset'] + params['limit']
        if next_ >= delta_count:
            next_ = ''
        else:
            next_ = str(next_)

        prev = params['offset'] - params['limit']
        if prev < 0:
            prev = ''
        else:
            prev = str(prev)
        prev_url = self.InsertQueryParams(params['url'], {'offset': prev})
        next_url = self.InsertQueryParams(params['url'], {'offset': next_})
        latest_url = self.InsertQueryParams(params['url'], {'order': '-date'})
        scores_hl_url = self.InsertQueryParams(params['url'],
                                               {'order': '-score'})
        scores_lh_url = self.InsertQueryParams(params['url'],
                                               {'order': 'score'})
        template_values = {
            'data_count': data_count,
            'delta_count': delta_count,
            'deltas': processed_results['deltas'],
            'end': end,
            'latest_url': latest_url,
            'limit': params['limit'],
            'next': next_,
            'next_url': next_url,
            'prev': prev,
            'prev_url': prev_url,
            'record_count': processed_results['record_count'],
            'scores_hl_url': scores_hl_url,
            'scores_lh_url': scores_lh_url,
            'start': start,
            'suite': suite,
            'url': params['url']
        }
        if params['display_stripdown'] and params[
                'display_stripdown'] == 'true':
            self.RenderTemplate('suite_compare_stripdown.html',
                                template_values)
            return
        else:
            self.RenderTemplate('suite_compare.html', template_values)
            return
Exemplo n.º 40
0
    def post(self, blog_id):
        # get all the necessary parameters
        key = db.Key.from_path("Blog", int(blog_id), parent=blog_key())
        post = db.get(key)
        user_id = User.by_name(self.user.name)
        comments_count = Comment.count_by_blog_id(post)
        post_comments = Comment.all_by_blog_id(post)
        likes = Like.by_blog_id(post)
        unlikes = Unlike.by_blog_id(post)
        previously_liked = Like.check_like(post, user_id)
        previously_unliked = Unlike.check_unlike(post, user_id)

        # check if the user is logged in
        if self.user:
            # if the user clicks on like
            if self.request.get("like"):
                # check if the user try like his own post
                if post.user.key().id() != User.by_name(
                        self.user.name).key().id():
                    if previously_liked == 0:
                        # add like to the likes database and refresh the page
                        l = Like(post=post, user=User.by_name(self.user.name))
                        l.put()
                        time.sleep(0.1)
                        self.redirect('/post/%s' % str(post.key().id()))
                    # otherwise if the user has liked this post before throw
                    # and error
                    else:
                        error = "You have already liked this post"
                        self.render("post.html",
                                    post=post,
                                    likes=likes,
                                    unlikes=unlikes,
                                    error=error,
                                    comments_count=comments_count,
                                    post_comments=post_comments)
                # otherwise if the user is trying to like his own post throw an
                # error
                else:
                    error = "You cannot like your own posts"
                    self.render("post.html",
                                post=post,
                                likes=likes,
                                unlikes=unlikes,
                                error=error,
                                comments_count=comments_count,
                                post_comments=post_comments)
            # if the user clicks on unlike
            if self.request.get("unlike"):
                # first check if the user is trying to unlike his own post
                if post.user.key().id() != User.by_name(
                        self.user.name).key().id():
                    # then check if the user has unliked this post before
                    if previously_unliked == 0:
                        # add unlike to the unlikes database and refresh the
                        # page
                        ul = Unlike(post=post,
                                    user=User.by_name(self.user.name))
                        ul.put()
                        time.sleep(0.1)
                        self.redirect('/post/%s' % str(post.key().id()))
                    # otherwise if the user has unliked this post before throw
                    # and error
                    else:
                        error = "You have already unliked this post"
                        self.render("post.html",
                                    post=post,
                                    likes=likes,
                                    unlikes=unlikes,
                                    error=error,
                                    comments_count=comments_count,
                                    post_comments=post_comments)
                # otherwise if the user is trying to unlike his own post throw
                # an error
                else:
                    error = "You cannot unlike your own posts"
                    self.render("post.html",
                                post=post,
                                likes=likes,
                                unlikes=unlikes,
                                error=error,
                                comments_count=comments_count,
                                post_comments=post_comments)
            # if the user clicks on add comment get the comment text first
            if self.request.get("add_comment"):
                comment_text = self.request.get("comment_text")
                # check if there is anything entered in the comment text area
                if comment_text:
                    # add comment to the comments database and refresh page
                    c = Comment(post=post,
                                user=User.by_name(self.user.name),
                                text=comment_text)
                    c.put()
                    time.sleep(0.1)
                    self.redirect('/post/%s' % str(post.key().id()))
                # otherwise if nothing has been entered in the text area throw
                # an error
                else:
                    comment_error = "Please enter a comment in the text area"
                    self.render("post.html",
                                post=post,
                                likes=likes,
                                unlikes=unlikes,
                                comments_count=comments_count,
                                post_comments=post_comments,
                                comment_error=comment_error)
            # if the user clicks on edit post
            if self.request.get("edit"):
                # check if the user is the author of this post
                if post.user.key().id() == User.by_name(
                        self.user.name).key().id():
                    # take the user to edit post page
                    self.redirect('/edit/%s' % str(post.key().id()))
                # otherwise if the user is not the author of this post throw an
                # error
                else:
                    error = "You cannot edit other user's posts"
                    self.render("post.html",
                                post=post,
                                likes=likes,
                                unlikes=unlikes,
                                comments_count=comments_count,
                                post_comments=post_comments,
                                error=error)
            # if the user clicks on delete
            if self.request.get("delete"):
                # check if the user is the author of this post
                if post.user.key().id() == User.by_name(
                        self.user.name).key().id():
                    # delete the post and redirect to the main page
                    db.delete(key)
                    time.sleep(0.1)
                    self.redirect('/')
                # otherwise if the user is not the author of this post throw an
                # error
                else:
                    error = "You cannot delete other user's posts"
                    self.render("post.html",
                                post=post,
                                likes=likes,
                                unlikes=unlikes,
                                comments_count=comments_count,
                                post_comments=post_comments,
                                error=error)
        # otherwise if the user is not logged in take them to the login page
        else:
            self.redirect("/login")
Exemplo n.º 41
0
    def get(self):
        """
      Called when adding a passenger to a ride
      
      Arguments:
      - 'self'
      
      Web Arguments:
      - user_name
      - ride_key
      """
        # The current user can add himself to the ride.  No need for this in the form.
        user_name = self.current_user.id

        ride_key = self.request.get('ride_key')
        contact = self.request.get('contact')
        address = self.request.get('address')
        lat = float(self.request.get('lat'))
        lng = float(self.request.get('lng'))
        ride = db.get(db.Key(ride_key))
        if ride == None:  # Check if the ride was found
            temp = os.path.join(os.path.dirname(__file__),
                                'templates/error.html')
            outstr = template.render(
                temp, {'error_message': 'Error in ride matching'})
            self.response.out.write(outstr)
        # Check if the current user is the driver
        elif ride.max_passengers == ride.num_passengers:
            doRender(self, 'error.html',
                     {'error_message': 'This ride is full'})
        # Check if the current user is already on the ride
        already = False
        for p in ride.passengers:
            if db.get(p).name == user_name:
                already = True
        if already:
            temp = os.path.join(os.path.dirname(__file__),
                                'templates/error.html')
            outstr = template.render(
                temp,
                {'error_message': 'You are already registered for this ride!'})
            self.response.out.write(outstr)
        # Check if the current user is already the driver for the ride
        elif user_name == ride.driver:
            doRender(
                self, 'error.html', {
                    'error_message':
                    'You can\'t be a passenger for a ride which you a driving.'
                })
        else:
            passenger = Passenger()
            passenger.name = user_name
            passenger.fullname = FBUser.get_by_key_name(user_name).nickname()
            passenger.contact = contact
            passenger.location = address
            passenger.lat = lat
            passenger.lng = lng
            passenger.ride = db.Key(ride_key)
            pass_key = passenger.put()
            ride.num_passengers = ride.num_passengers + 1
            ride.passengers.append(pass_key)
            ride.put()

            if ride.num_passengers == ride.max_passengers:
                capacity_message = 'is now full.'
            else:
                num_left = ride.max_passengers - ride.num_passengers
                capacity_message = 'can hold ' + str(
                    num_left) + ' more passengers.'
            query = db.Query(Ride)
            query.filter("ToD > ", datetime.datetime.now())
            ride_list = query.fetch(limit=100)
            user = self.current_user
            greeting = ''
            if user:
                greeting = (
                    "Welcome, %s! (<a href=\"%s\">sign out</a>) Go to your <a href='/home'>Home Page</a>"
                    % (user.nickname(), users.create_logout_url("/")))
            message = 'You have been added to %s ride.' % (ride.driver)
            self.sendDriverEmail(ride)
            path = os.path.join(os.path.dirname(__file__),
                                'templates/index.html')
            self.response.out.write(
                template.render(
                    path, {
                        'ride_list': ride_list,
                        'greeting': greeting,
                        'message': message,
                        'mapkey': MAP_APIKEY,
                    }))
Exemplo n.º 42
0
    def get(self):
        """Get handler for test suite details page."""
        user = users.get_current_user()
        if not user:
            self.redirect(users.create_login_url(self.request.uri))
            return

        suite_key = self.request.get('suite')
        if not suite_key:
            self.response.out.write('No suite key specified.')
            return
        if suite_key.lower() == 'latest':
            suite = test_suite.TestSuite.all().order('-date')[0]
        else:
            suite = db.get(db.Key(suite_key))
        if not suite:
            self.response.out.write('No Matching suites found.')
            return

        offset = int(
            self.GetOptionalParameter(parameter_name='offset',
                                      default_value=0))
        # Limit is used for pagination. Indicates number of entities/records
        # to display on a single page.
        limit = int(
            self.GetOptionalParameter(parameter_name='limit',
                                      default_value=20))
        order = self.GetOptionalParameter(parameter_name='order',
                                          default_value='-date')
        deltas = suite.results.order(order).fetch(limit=limit, offset=offset)
        delta_count = suite.results.count()
        data_count = suite.page_data_set.count()
        start = offset
        end = min(offset + limit - 1, delta_count)
        next_ = offset + limit
        if next_ >= delta_count:
            next_ = ''
        else:
            next_ = '%d' % next_

        prev = offset - limit
        if prev < 0:
            prev = ''
        else:
            prev = '%d' % prev

        ref_browser = suite.ref_browser.GetBrowserStringWithFlag()
        test_browsers = suite.GetTestBrowsersStringWithFlag()
        template_values = {
            'data_count': data_count,
            'deltas': deltas,
            'delta_count': delta_count,
            'end': end,
            'is_admin': users.is_current_user_admin(),
            'limit': limit,
            'next': next_,
            'order': order,
            'prerendered_string': page_delta.PRERENDERED_STRING,
            'prev': prev,
            'ref_browser': ref_browser,
            'start': start,
            'suite': suite,
            'test_browsers': test_browsers,
            'url': '/suite/details?suite=%s' % suite_key
        }
        self.RenderTemplate('suite_details.html', template_values)
        return
    def __Dynamic_Read(self, request, response):
        """Implements _Dynamic_Read if called by _run_in_namespace."""

        response.clear_offset()

        if request.version_id_size() != 1:
            raise apiproxy_errors.ApplicationError(
                log_service_pb.LogServiceError.INVALID_REQUEST)

        if (request.request_id_size()
                and (request.has_start_time() or request.has_end_time()
                     or request.has_offset())):
            raise apiproxy_errors.ApplicationError(
                log_service_pb.LogServiceError.INVALID_REQUEST)

        if request.request_id_size():
            results = []
            try:
                results = db.get(request.request_id_list())
            except datastore_errors.BadKeyError:

                for request_id in request.request_id_list():
                    try:
                        results.append(db.get(request_id))
                    except datastore_errors.BadKeyError:
                        pass
            for result in results:
                if result.version_id != request.version_id(0):
                    continue
                log = response.add_log()
                app_logs = db.get(result.app_logs)
                result.fill_in_log(request, log, app_logs)
            return

        query = db.Query(_LogRecord)

        if request.has_offset():
            query.filter('__key__ > ', db.Key(request.offset().request_id()))

        if request.has_count():
            limit = request.count()
        else:
            limit = LogServiceStub.__DEFAULT_READ_COUNT

        versions = request.version_id_list()

        index = 0
        for result in query.run(limit=limit):
            index += 1
            start = result.start_time

            if request.has_start_time():
                if request.start_time() > start:
                    continue

            if request.has_end_time():
                if request.end_time() <= start:
                    continue

            if not request.include_incomplete() and not result.finished:
                continue

            if result.version_id not in versions:
                continue

            app_logs = db.get(result.app_logs)
            if request.has_minimum_log_level():
                for app_log in app_logs:
                    if app_log.level >= request.minimum_log_level():
                        break
                else:
                    continue

            log = response.add_log()
            result.fill_in_log(request, log, app_logs)
            log.mutable_offset().set_request_id(str(result.key()))

        if index == limit:
            response.mutable_offset().set_request_id(str(result.key()))
Exemplo n.º 44
0
 def get_blogpost(self, post_id):
     key = db.Key.from_path('Blogpost', int(post_id))
     blogpost = db.get(key)
     return blogpost
Exemplo n.º 45
0
 def latest_comments(limit):
     comments = Comment.all().order('-time').fetch(limit)
     articles = db.get([comment.parent_key() for comment in comments])
     users = db.get(
         [db.Key.from_path('User', comment.email) for comment in comments])
     return comments, articles, users
Exemplo n.º 46
0
from google.appengine.ext import db
import scarlett.model as model

def printEntity(entity):
  print "%s = %s, %i" % (entity.key().name(), entity.level, entity.value)

totalKey = db.Key.from_path("Matrix", "KEY/----------/T/-/a")
totalEntity = db.get(totalKey)

printEntity(totalEntity)

query = model.Matrix.gql(
                         "where level = :level and ancestor is :parent",
                         level=u"Y",
                         parent=totalKey)
for yearEntity in query:
  printEntity(yearEntity)

print "totalViewer!"
Exemplo n.º 47
0
 def retrieve(self,key):
     cd = db.get(db.Key.from_path('Cache', key))
     return cd.value.decode('utf-8') if cd else None
Exemplo n.º 48
0
def _broadcast(sln_settings_key, sandwich_settings_key):
    sln_settings, sandwich_settings = db.get(
        [sln_settings_key, sandwich_settings_key])
    if not sln_settings:
        logging.info("Service has been deleted in the meantime")
        return
    solution_datetime = datetime.now(pytz.timezone(sln_settings.timezone))
    if not sandwich_settings.can_order_sandwiches_on(solution_datetime):
        logging.info("No email_reminders anymore today for %s",
                     sln_settings.service_user.email())
        return
    broadcast_type = get_sandwich_reminder_broadcast_type(
        sln_settings.main_language or DEFAULT_LANGUAGE,
        SandwichSettings.DAYS[solution_datetime.weekday()])
    message = sandwich_settings.reminder_broadcast_message
    order_sandwich_answer = AnswerTO()
    order_sandwich_answer.action = None
    order_sandwich_answer.caption = translate(sln_settings.main_language,
                                              SOLUTION_COMMON, u'order')
    order_sandwich_answer.type = u'button'
    order_sandwich_answer.id = u'order'
    order_sandwich_answer.ui_flags = 1
    no_sandwich_today_answer = AnswerTO()
    no_sandwich_today_answer.action = None
    no_sandwich_today_answer.caption = translate(
        sln_settings.main_language, SOLUTION_COMMON,
        u'order-sandwiches-not-today')
    no_sandwich_today_answer.type = u'button'
    no_sandwich_today_answer.id = u'Not now'
    no_sandwich_today_answer.ui_flags = 0
    answers = list()
    answers.append(order_sandwich_answer)
    answers.append(no_sandwich_today_answer)
    flags = 0
    branding = db.get(
        SolutionMainBranding.create_key(
            sln_settings.service_user)).branding_key
    tag = MESSAGE_TAG_SANDWICH_ORDER_NOW
    alert_flags = 0
    timeout = sandwich_settings.get_reminder_broadcast_timeout(
        solution_datetime)

    users.set_user(sln_settings.service_user)
    try:
        identities = [None]
        if sln_settings.identities:
            identities.extend(sln_settings.identities)
        for service_identity in identities:
            sln_i_settings = get_solution_settings_or_identity_settings(
                sln_settings, service_identity)
            if sln_i_settings.is_in_holiday_for_date(now()):
                logging.info(
                    "Not sending out sandwich broadcast '%s'. %s is in holiday.",
                    broadcast_type, sln_i_settings.service_user)
            else:
                logging.info(
                    "Sending broadcast to users of %s with broadcast type %s",
                    sln_i_settings.service_user, broadcast_type)
                messaging.broadcast(broadcast_type,
                                    message,
                                    answers,
                                    flags,
                                    branding,
                                    tag,
                                    service_identity,
                                    alert_flags,
                                    timeout=timeout)
    finally:
        users.clear_user()
Exemplo n.º 49
0
    def handle(self):
        """Handle request."""
        tstate = model.TransientShardState.from_request(self.request)
        spec = tstate.mapreduce_spec
        self._start_time = self._time()
        shard_id = tstate.shard_id

        shard_state, control = db.get([
            model.ShardState.get_key_by_shard_id(shard_id),
            model.MapreduceControl.get_key_by_job_id(spec.mapreduce_id),
        ])
        if not shard_state:
            # We're letting this task to die. It's up to controller code to
            # reinitialize and restart the task.
            logging.error("State not found for shard ID %r; shutting down",
                          shard_id)
            return

        if control and control.command == model.MapreduceControl.ABORT:
            logging.info("Abort command received by shard %d of job '%s'",
                         shard_state.shard_number, shard_state.mapreduce_id)
            if tstate.output_writer:
                tstate.output_writer.finalize(None, shard_state.shard_number)
            shard_state.active = False
            shard_state.result_status = model.ShardState.RESULT_ABORTED
            shard_state.put(config=util.create_datastore_write_config(spec))
            model.MapreduceControl.abort(spec.mapreduce_id)
            return

        input_reader = tstate.input_reader

        if spec.mapper.params.get("enable_quota", True):
            quota_consumer = quota.QuotaConsumer(
                quota.QuotaManager(memcache.Client()), shard_id,
                _QUOTA_BATCH_SIZE)
        else:
            quota_consumer = None

        ctx = context.Context(spec,
                              shard_state,
                              task_retry_count=self.task_retry_count())
        context.Context._set(ctx)

        try:
            # consume quota ahead, because we do not want to run a datastore
            # query if there's not enough quota for the shard.
            if not quota_consumer or quota_consumer.check():
                scan_aborted = False
                entity = None

                # We shouldn't fetch an entity from the reader if there's not enough
                # quota to process it. Perform all quota checks proactively.
                if not quota_consumer or quota_consumer.consume():
                    for entity in input_reader:
                        if isinstance(entity, db.Model):
                            shard_state.last_work_item = repr(entity.key())
                        else:
                            shard_state.last_work_item = repr(entity)[:100]

                        scan_aborted = not self.process_entity(
                            entity, ctx, tstate)

                        # Check if we've got enough quota for the next entity.
                        if (quota_consumer and not scan_aborted
                                and not quota_consumer.consume()):
                            scan_aborted = True
                        if scan_aborted:
                            break
                else:
                    scan_aborted = True

                if not scan_aborted:
                    logging.info("Processing done for shard %d of job '%s'",
                                 shard_state.shard_number,
                                 shard_state.mapreduce_id)
                    # We consumed extra quota item at the end of for loop.
                    # Just be nice here and give it back :)
                    if quota_consumer:
                        quota_consumer.put(1)
                    shard_state.active = False
                    shard_state.result_status = model.ShardState.RESULT_SUCCESS

            if not shard_state.active:
                # shard is going to stop. Finalize output writer if any.
                if tstate.output_writer:
                    tstate.output_writer.finalize(ctx,
                                                  shard_state.shard_number)

            # TODO(user): Mike said we don't want this happen in case of
            # exception while scanning. Figure out when it's appropriate to skip.
            ctx.flush()
        finally:
            context.Context._set(None)
            if quota_consumer:
                quota_consumer.dispose()

        # Rescheduling work should always be the last statement. It shouldn't happen
        # if there were any exceptions in code before it.
        if shard_state.active:
            self.reschedule(tstate)
Exemplo n.º 50
0
 def no_tx_get(i):
     return db.get(keys[i:i + cls._MAX_STATES_IN_MEMORY])
def increment_counter(obj_key, counter_field_name, amount):
    obj = db.get(obj_key)
    setattr(obj, counter_field_name, getattr(obj, counter_field_name) + amount)
    obj.put()
Exemplo n.º 52
0
    def handle(self):
        """Handle request."""
        spec = model.MapreduceSpec.from_json_str(
            self.request.get("mapreduce_spec"))

        # TODO(user): Make this logging prettier.
        logging.debug("post: id=%s headers=%s", spec.mapreduce_id,
                      self.request.headers)

        state, control = db.get([
            model.MapreduceState.get_key_by_job_id(spec.mapreduce_id),
            model.MapreduceControl.get_key_by_job_id(spec.mapreduce_id),
        ])
        if not state:
            logging.error("State not found for mapreduce_id '%s'; skipping",
                          spec.mapreduce_id)
            return

        shard_states = model.ShardState.find_by_mapreduce_id(spec.mapreduce_id)
        if state.active and len(shard_states) != spec.mapper.shard_count:
            # Some shards were lost
            logging.error(
                "Incorrect number of shard states: %d vs %d; "
                "aborting job '%s'", len(shard_states),
                spec.mapper.shard_count, spec.mapreduce_id)
            state.active = False
            state.result_status = model.MapreduceState.RESULT_FAILED
            model.MapreduceControl.abort(spec.mapreduce_id)

        active_shards = [s for s in shard_states if s.active]
        failed_shards = [
            s for s in shard_states
            if s.result_status == model.ShardState.RESULT_FAILED
        ]
        aborted_shards = [
            s for s in shard_states
            if s.result_status == model.ShardState.RESULT_ABORTED
        ]
        if state.active:
            state.active = bool(active_shards)
            state.active_shards = len(active_shards)
            state.failed_shards = len(failed_shards)
            state.aborted_shards = len(aborted_shards)

        if (not state.active and control
                and control.command == model.MapreduceControl.ABORT):
            # User-initiated abort *after* all shards have completed.
            logging.info("Abort signal received for job '%s'",
                         spec.mapreduce_id)
            state.result_status = model.MapreduceState.RESULT_ABORTED

        if not state.active:
            state.active_shards = 0
            if not state.result_status:
                # Set final result status derived from shard states.
                if [
                        s for s in shard_states
                        if s.result_status != model.ShardState.RESULT_SUCCESS
                ]:
                    state.result_status = model.MapreduceState.RESULT_FAILED
                else:
                    state.result_status = model.MapreduceState.RESULT_SUCCESS
                logging.info("Final result for job '%s' is '%s'",
                             spec.mapreduce_id, state.result_status)

        # We don't need a transaction here, since we change only statistics data,
        # and we don't care if it gets overwritten/slightly inconsistent.
        self.aggregate_state(state, shard_states)
        poll_time = state.last_poll_time
        state.last_poll_time = datetime.datetime.utcfromtimestamp(self._time())

        config = util.create_datastore_write_config(spec)

        if not state.active:
            # This is the last execution.
            # Enqueue done_callback if needed.
            if spec.mapper.output_writer_class():
                spec.mapper.output_writer_class().finalize_job(state)

            def put_state(state):
                state.put(config=config)
                done_callback = spec.params.get(
                    model.MapreduceSpec.PARAM_DONE_CALLBACK)
                if done_callback:
                    done_task = taskqueue.Task(
                        url=done_callback,
                        headers={"Mapreduce-Id": spec.mapreduce_id})
                    queue_name = spec.params.get(
                        model.MapreduceSpec.PARAM_DONE_CALLBACK_QUEUE,
                        "default")

                    if not _run_task_hook(spec.get_hooks(),
                                          "enqueue_done_task", done_task,
                                          queue_name):
                        done_task.add(queue_name, transactional=True)

            db.run_in_transaction(put_state, state)
            return
        else:
            state.put(config=config)

        processing_rate = int(
            spec.mapper.params.get("processing_rate")
            or model._DEFAULT_PROCESSING_RATE_PER_SEC)
        self.refill_quotas(poll_time, processing_rate, active_shards)
        ControllerCallbackHandler.reschedule(self.base_path(), spec,
                                             self.serial_id() + 1)
Exemplo n.º 53
0
def createNotificationMail(request, *args, **kwargs):
    """Appengine task that sends mail to the subscribed users.

  Expects the following to be present in the POST dict:
    comment_key: Specifies the comment id for which to send the notifications
    task_key: Specifies the task key name for which the comment belongs to

  Args:
    request: Django Request object
  """

    from soc.modules.gci.logic.helper import notifications as gci_notifications

    from soc.modules.gci.logic.models import comment as gci_comment_logic
    from soc.modules.gci.logic.models import task_subscription as \
        gci_task_subscription_logic

    # set default batch size
    batch_size = 10

    post_dict = request.POST

    comment_key = post_dict.get('comment_key')
    task_key = post_dict.get('task_key')

    if not (comment_key and task_key):
        # invalid task data, log and return OK
        return error_handler.logErrorAndReturnOK(
            'Invalid createNotificationMail data: %s' % post_dict)

    comment_key = long(comment_key)

    # get the task entity under which the specified comment was made
    task_entity = gci_task_logic.logic.getFromKeyName(task_key)

    # get the comment for the given id
    comment_entity = gci_comment_logic.logic.getFromID(comment_key,
                                                       task_entity)

    if not comment_entity:
        # invalid comment specified, log and return OK
        return error_handler.logErrorAndReturnOK(
            'Invalid comment specified: %s/%s' % (comment_key, task_key))

    # check and retrieve the subscriber_start_key that has been done last
    idx = post_dict.get('subscriber_start_index', '')
    subscriber_start_index = int(idx) if idx.isdigit() else 0

    # get all subscribers to GCI task
    fields = {
        'task': task_entity,
    }

    ts_entity = gci_task_subscription_logic.logic.getForFields(fields,
                                                               unique=True)

    subscribers = db.get(
        ts_entity.subscribers[subscriber_start_index:subscriber_start_index +
                              batch_size])

    task_url = "http://%(host)s%(task)s" % {
        'host': system.getHostname(),
        'task': redirects.getPublicRedirect(task_entity,
                                            {'url_name': 'gci/task'}),
    }

    # create the data for the mail to be sent
    message_properties = {
        'task_url': task_url,
        'redirect_url': "%(task_url)s#c%(cid)d" % {
            'task_url': task_url,
            'cid': comment_entity.key().id_or_name()
        },
        'comment_entity': comment_entity,
        'task_entity': task_entity,
    }

    subject = DEF_TASK_UPDATE_SUBJECT_FMT % {
        'program_name': task_entity.program.short_name,
        'title': task_entity.title,
    }

    for subscriber in subscribers:
        gci_notifications.sendTaskUpdateMail(subscriber, subject,
                                             message_properties)

    if len(subscribers) == batch_size:
        # spawn task for sending out notifications to next set of subscribers
        next_start = subscriber_start_index + batch_size

        task_params = {
            'comment_key': comment_key,
            'task_key': task_key,
            'subscriber_start_index': next_start
        }
        task_url = '/tasks/gci/task/mail/create'

        new_task = taskqueue.Task(params=task_params, url=task_url)
        new_task.add('mail')

    # return OK
    return http.HttpResponse()
Exemplo n.º 54
0
    def post(self, blog_id):
        key = db.Key.from_path("Blog", int(blog_id), parent=blog_key())
        post = db.get(key)
        user_id = User.by_name(self.user.name)
        comments_count = Comment.count_by_blog_id(post)
        post_comments = Comment.all_by_blog_id(post)
        likes = Like.by_blog_id(post)
        unlikes = Unlike.by_blog_id(post)
        previously_liked = Like.check_like(post, user_id)
        previously_unliked = Unlike.check_unlike(post, user_id)
        username = self.user.name

        if self.user:
            if self.request.get("like"):
                puk = post.user.key().id()
                ubn = User.by_name(self.user.name).key().id()
                if puk != ubn:  # These variables were only creatd to shorten the lenght of the line of code here since it was more than 79 chars.
                    if previously_liked == 0:
                        l = Like(post=post, user=User.by_name(self.user.name))
                        l.put()
                        time.sleep(0.1)
                        self.redirect('/post/%s' % str(post.key().id()))
                    else:
                        error = "You have already liked this post"
                        self.render("post.html",
                                    post=post,
                                    likes=likes,
                                    unlikes=unlikes,
                                    error=error,
                                    comments_count=comments_count,
                                    post_comments=post_comments,
                                    username=username)
                else:
                    error = "It's not allowed to like your own posts"
                    self.render("post.html",
                                post=post,
                                likes=likes,
                                unlikes=unlikes,
                                error=error,
                                comments_count=comments_count,
                                post_comments=post_comments,
                                username=username)

            if self.request.get("unlike"):
                puk = post.user.key().id()
                ubn = User.by_name(self.user.name).key().id()
                if puk != ubn:
                    if previously_unliked == 0:
                        ul = Unlike(post=post,
                                    user=User.by_name(self.user.name))
                        ul.put()
                        time.sleep(0.1)
                        self.redirect('/post/%s' % str(post.key().id()))
                    else:
                        error = "You have already unliked this post"
                        self.render("post.html",
                                    post=post,
                                    likes=likes,
                                    unlikes=unlikes,
                                    error=error,
                                    comments_count=comments_count,
                                    post_comments=post_comments,
                                    username=username)
                else:
                    error = "It's not allowed to unlike your own posts"
                    self.render("post.html",
                                post=post,
                                likes=likes,
                                unlikes=unlikes,
                                error=error,
                                comments_count=comments_count,
                                post_comments=post_comments,
                                username=username)

            if self.request.get("add_comment"):
                comment_text = self.request.get("comment_text")
                if comment_text:
                    c = Comment(post=post,
                                user=User.by_name(self.user.name),
                                text=comment_text)
                    c.put()
                    time.sleep(0.1)
                    self.redirect('/post/%s' % str(post.key().id()))
                else:
                    comment_error = "Use the textarea to enter a post, please"
                    self.render("post.html",
                                post=post,
                                likes=likes,
                                unlikes=unlikes,
                                comments_count=comments_count,
                                post_comments=post_comments,
                                comment_error=comment_error,
                                username=username)

            if self.request.get("edit"):
                puk = post.user.key().id()
                ubn = User.by_name(self.user.name).key().id()
                if puk == ubn:
                    self.redirect('/edit/%s' % str(post.key().id()))
                else:
                    error = "To edit a post you must be the author"
                    self.render("post.html",
                                post=post,
                                likes=likes,
                                unlikes=unlikes,
                                comments_count=comments_count,
                                post_comments=post_comments,
                                error=error,
                                username=username)

            if self.request.get("delete"):
                puk = post.user.key().id()
                ubn = User.by_name(self.user.name).key().id()
                if puk == ubn:
                    db.delete(key)
                    time.sleep(0.1)
                    self.redirect('/')
                else:
                    error = "To delete a post you must be the author"
                    self.render("post.html",
                                post=post,
                                likes=likes,
                                unlikes=unlikes,
                                comments_count=comments_count,
                                post_comments=post_comments,
                                error=error,
                                username=username)
        else:
            self.redirect("/login")
Exemplo n.º 55
0
 def post(self):
     key = self.request.get('key')
     query = db.get(key)
     db.delete(query)
     self.response.out.write('message_%s' % key)
Exemplo n.º 56
0
    def get(self):
        params_html = {}
        user_author = ''
        paper_key = self.request.get('i')
        key = 'paper_%s' % paper_key
        admin_flag = 0
        temp = self.request.cookies.get('user_id')
        if temp:
            userid = valid_hash_cookie(temp)
            if userid:
                params_html['userid'] = userid
                user = db.GqlQuery(
                    "SELECT * FROM UserPass_User WHERE user_id = '%s'" %
                    userid)
                user = user.get()

                if user:
                    admin_flag = user.isadmin
                    user_author = user.author_id

        temp_flag = 1
        try:
            paper = db.get(paper_key)
        except:
            logging.error("%s", 'There is no paper with such a key')
            temp_flag = 0

        if temp_flag:
            paper_desc = []
            paper_desc.append(unescape_html(paper.title))
            if (paper.publication_date):
                paper_desc.append((paper.publication_date))
            else:
                paper_desc.append(int(paper.publication_year))
            paper_desc.append(paper.publisher)
            paper_desc.append(paper.authors_str)
            paper_desc.append(paper.keywords)
            paper_desc.append(unescape_html(paper.abstract))
            paper_desc.append(paper.pdf_link)
            paper_desc.append(paper.data_link)
            paper_desc.append(paper.code_link)
            paper_desc.append(paper.demo_link)
            paper_desc.append(paper.biblio_str)
            paper_desc.append(paper.web_link)

            authors_links = []
            for item in paper.authors:
                authorID = item.replace(" ", "")
                authorID = authorID.replace(",", "")
                authors_links.append(authorID)

            params_html['key'] = paper_key
            params_html['admin_flag'] = admin_flag
            params_html['paper'] = paper_desc
            params_html['author_list'] = zip(paper.authors, authors_links)
            params_html['paper_abstract'] = unescape_html(paper.abstract)
            params_html['no_views'] = paper.no_views
            params_html['no_pdf_downloads'] = paper.no_pdf_downloads
            params_html['no_code_downloads'] = paper.no_code_downloads
            params_html['no_demo_downloads'] = paper.no_demo_downloads
            params_html['no_data_downloads'] = paper.no_data_downloads

            if paper.demo_link:
                if 'youtube' in paper.demo_link:
                    ind = paper.demo_link.find('watch?v=')
                    if ind:
                        youtube_key = paper.demo_link[ind +
                                                      8:len(paper.demo_link)]
                        params_html['youtube_demo'] = youtube_key
            if admin_flag:
                params_html['editpaper_link'] = "/_editpaper?i=" + paper_key
            elif user_author in authors_links:
                params_html['editpaper_link'] = "/_editpaper?i=" + paper_key
            else:
                params_html['editpaper_link'] = ''

            #----------------Increase the Click Counter in the Database--------------
            #pp = db.GqlQuery("SELECT * FROM Paper_Clicks_DB WHERE paperkey = '%s'" %paper_key)
            #pp = pp.get()
            pp = db.get(paper_key)
            if pp:
                pp.no_views = pp.no_views + 1
                pp.put()
            #------------------------------------------------------------------------

            self.response.out.write(
                template.render('./html/display_paper.html', params_html))
        else:
            self.redirect('/404?u=author')
Exemplo n.º 57
0
 def unassign_mentor_txn():
     proposal = db.get(proposal_key)
     proposal.mentor = None
     proposal.has_mentor = False
     db.put(proposal)
Exemplo n.º 58
0
 def post(self, topic_key):
     topic = db.get(db.Key(topic_key))
     if topic:
         topic.stars = topic.stars - 1
         topic.put()
         memcache.set('Topic_' + str(topic.num), topic, 86400)
Exemplo n.º 59
0
 def retrieve_comment(self, comment_id):
     key = db.Key.from_path('Comment', int(comment_id), parent=blog_key())
     comment = db.get(key)
     return comment
Exemplo n.º 60
0
 def get(self, post_id):
   key = db.Key.from_path('BlogPost', int(post_id))
   post_data = db.get(key)
   if not post_data:
     self.error(404)
   self.render("mainpage.html", posts = [post_data])