Beispiel #1
0
    def current_user(self):
        if not hasattr(self, '_current_user'):
            sess = sessions.Session()
            if 'id' in sess:
                id = sess['id']

                # Try to find user info in memcache
                cache = mc.get(id, namespace='users')
                if cache:
                    logging.info('Found user %s in cache' % (id))
                    u = User(id=id, email=cache['email'])
                else:
                    logging.info('Looking for user %s in store' % (id))
                    u = User.get_by_key_name(id)

                    if u:
                        # Memcache already existing user
                        logging.info('Setting user %s in cache' % (id))
                        mc.set(id,
                               dict(id=u.id, email=u.email),
                               namespace='users')
                    else:
                        # We have a new user
                        logging.info('Creating new user %s' % (id))

                        # Make sure it's the correct class year
                        d = DNDRemoteLookup()
                        dndnames = d.lookup([id], CLASS_YEAR)
                        if id not in dndnames or len(dndnames[id]) == 0:
                            logging.info('Reject new user %s' % (id))
                            self.response.out.write(
                                "Sorry, only the senior class can enter last chances.  If you think there's been a mistake, please contact people running this."
                            )
                            self._current_user = None
                            sess.delete()
                            return None

                        # Add new user
                        email = id.replace(' ', '.').replace(
                            '..', '.') + '@dartmouth.edu'
                        u = User(key_name=id, id=id, email=email)
                        u.save()

                        # memcache the user
                        mc.set(id, dict(id=id, email=email), namespace='users')

                self._current_user = u
            else:
                self._current_user = None
        return self._current_user
    def current_user(self):
        if not hasattr(self, '_current_user'):
            sess = sessions.Session()
            if 'id' in sess:
                id = sess['id']

                # Try to find user info in memcache
                cache = mc.get(id, namespace='users')
                if cache:
                    logging.info('Found user %s in cache' % (id))
                    u = User(id=id,email=cache['email'])
                else:
                    logging.info('Looking for user %s in store' % (id))
                    u = User.get_by_key_name(id)

                    if u:
                        # Memcache already existing user
                        logging.info('Setting user %s in cache' % (id))
                        mc.set(id, dict(id=u.id, email=u.email), namespace='users')
                    else:
                        # We have a new user
                        logging.info('Creating new user %s' % (id))

                        # Make sure it's the correct class year
                        d = DNDRemoteLookup()
                        dndnames = d.lookup([id], CLASS_YEAR)
                        if id not in dndnames or len(dndnames[id])==0:
                            logging.info('Reject new user %s' % (id))
                            self.response.out.write("Sorry, only the senior class can enter last chances.  If you think there's been a mistake, please contact people running this.")
                            self._current_user = None
                            sess.delete()
                            return None

                        # Add new user
                        email = id.replace(' ','.').replace('..', '.') + '@dartmouth.edu'
                        u = User(key_name=id, id=id, email=email)
                        u.save()

                        # memcache the user
                        mc.set(id, dict(id=id, email=email), namespace='users')

                self._current_user = u
            else:
                self._current_user = None
        return self._current_user
Beispiel #3
0
    def post(self):
        if not self.current_user:
            args = dict(user=self.current_user, logout_url=LOGOUT_URL)
            self.response.out.write(
                template.render('templates/index.html', args))
            return

        try:
            query = db.Query(Crush)
            query.filter('id =', self.current_user.id)
            query.order('created')

            results = query.fetch(11)
            orig_crushes = [x.crush for x in results]

            names = self.request.POST.getall('c')
            orig = self.request.POST.getall('o')

            # First handle deletion
            for crush in orig_crushes:
                if crush not in names:
                    crushkey = self.current_user.id + ':' + crush
                    c = Crush.get_by_key_name(crushkey)
                    if c:
                        logging.info('deleting crush %s from cache and store' %
                                     (crushkey))
                        c.delete()
                        mc.delete(crushkey, namespace='crushes')

            # Now add anything new
            d = DNDRemoteLookup()
            # TODO not necessary to lookup names that were already in there (even though we memcache lookups)
            dndnames = d.lookup(names, CLASS_YEAR)
            new_crushes = []
            comments = []
            i = 0
            for name in names:
                if name == '':
                    i += 1
                    continue

                # Check if it's already there
                crushkeyname = self.current_user.id + ':' + name
                c = mc.get(crushkeyname, namespace='crushes')

                if c != None or Crush.get_by_key_name(crushkeyname):
                    # We also checked that it's in db in case it got evicted from cache
                    if c:
                        logging.info('Found preexisting crush in cache')
                    else:
                        logging.info('Found preexisting crush in store')

                    # Was already validated, so no need to check in dndnames
                    comments.append('')
                    new_crushes.append(name)
                else:
                    # Crush doesn't already exist
                    if len(dndnames[name]) == 0:
                        # No good
                        comments.append(
                            'DND couldn\'t find anyone named "%s" in your year'
                            % (cgi.escape(name)))
                        new_crushes.append('')
                    elif len(dndnames[name]) == 1:
                        # New crush
                        resolved_name = dndnames[name][0]
                        crushkeyname = self.current_user.id + ':' + resolved_name
                        c = Crush(key_name=crushkeyname,
                                  id=self.current_user.id,
                                  crush=resolved_name)
                        c.put()
                        mc.set(crushkeyname, True, namespace='crushes')
                        comments.append('Saved')
                        new_crushes.append(resolved_name)
                    else:
                        # Unspecific - let them choose
                        links = ['<a href="#" onClick="document.getElementById(\'c%d\').value=\'%s\';return false;">%s</a>' \
                                 % (i,x,x) for x in dndnames[name]]
                        comments.append('Did you mean: ' + ', '.join(links))
                        new_crushes.append('')
                i += 1

            self.render_main(crushes=new_crushes, comments=comments)

        except DeadlineExceededError:
            self.response.clear()
            self.response.set_status(500)
            self.response.out.write(
                'The operation could not be completed in time.  Try again or contact technical assistance.'
            )
    def post(self):
        if not self.current_user:
            args = dict(user=self.current_user, logout_url=LOGOUT_URL)
            self.response.out.write(template.render('templates/index.html', args))
            return

        try:
            query = db.Query(Crush)
            query.filter('id =', self.current_user.id)
            query.order('created')

            results = query.fetch(11)
            orig_crushes = [x.crush for x in results]

            names = self.request.POST.getall('c')
            orig = self.request.POST.getall('o')

            # First handle deletion
            for crush in orig_crushes:
                if crush not in names:
                    crushkey = self.current_user.id+':'+crush
                    c = Crush.get_by_key_name(crushkey)
                    if c:
                        logging.info('deleting crush %s from cache and store' % (crushkey))
                        c.delete()
                        mc.delete(crushkey, namespace='crushes')

            # Now add anything new
            d = DNDRemoteLookup()
            # TODO not necessary to lookup names that were already in there (even though we memcache lookups)
            dndnames = d.lookup(names, CLASS_YEAR)
            new_crushes = []
            comments = []
            i = 0
            for name in names:
                if name == '':
                    i+=1
                    continue

                # Check if it's already there
                crushkeyname = self.current_user.id+':'+name
                c = mc.get(crushkeyname, namespace='crushes')

                if c != None or Crush.get_by_key_name(crushkeyname):
                    # We also checked that it's in db in case it got evicted from cache
                    if c:
                        logging.info('Found preexisting crush in cache')
                    else:
                        logging.info('Found preexisting crush in store')

                    # Was already validated, so no need to check in dndnames
                    comments.append('')
                    new_crushes.append(name)
                else:
                    # Crush doesn't already exist
                    if len(dndnames[name]) == 0:
                        # No good
                        comments.append('DND couldn\'t find anyone named "%s" in your year' % (cgi.escape(name)))
                        new_crushes.append('')
                    elif len(dndnames[name]) == 1:
                        # New crush
                        resolved_name = dndnames[name][0]
                        crushkeyname = self.current_user.id+':'+resolved_name
                        c = Crush(key_name=crushkeyname, id=self.current_user.id, crush=resolved_name)
                        c.put()
                        mc.set(crushkeyname, True, namespace='crushes')
                        comments.append('Saved')
                        new_crushes.append(resolved_name)
                    else:
                        # Unspecific - let them choose
                        links = ['<a href="#" onClick="document.getElementById(\'c%d\').value=\'%s\';return false;">%s</a>' \
                                 % (i,x,x) for x in dndnames[name]]
                        comments.append('Did you mean: ' + ', '.join(links))
                        new_crushes.append('')
                i += 1

            self.render_main(crushes=new_crushes, comments=comments)

        except DeadlineExceededError:
            self.response.clear()
            self.response.set_status(500)
            self.response.out.write('The operation could not be completed in time.  Try again or contact technical assistance.')