def post(self): dj_key = ndb.Key(urlsafe=self.request.get("dj_key")) dj = Dj.get(dj_key) if not dj: self.session.add_flash( "An error occurred processing your request. Please try again.") self.redirect("/dj/myself") return dj.fullname = self.request.get("fullname") email = self.request.get("email") if email[-1] == "@": email += "bowdoin.edu" if "@" not in email: email += "@bowdoin.edu" duplicate_dj_key = Dj.get_key_by_email(email) if duplicate_dj_key and duplicate_dj_key != dj.key: error = True self.session.add_flash( "The email specified is already in use by another DJ. " "Please enter a unique one.") dj.email = email username = self.request.get("username") duplicate_dj_key = Dj.get_key_by_username() if duplicate_dj_key and duplicate_dj_key != dj.key: error = True self.session.add_flash( "The username specified is already in use by another DJ. " "Please choose another.") dj.username = username if error: self.redirect("/dj/myself") return if self.request.get("password"): if not self.request.get("password") == self.request.get("confirm"): self.session.add_flash("New passwords do not match.") self.redirect("/dj/myself") return else: dj.password = self.request.get("password") dj.put() self.session.add_flash("You have successfully updated your profile.") self.redirect("/dj/")
def test_dj_management(self): # Add some Djs names = file("./names") name_pairs = [(name.strip(), (name[0] + name.split()[1]).lower().strip()) for name in names] seen_unames = set() for name, uname in name_pairs: req = Request.blank('/dj/djs/', POST={ 'username': uname, 'fullname': name, 'email': uname, 'password': "******", 'confirm': "wbor", 'submit': "Add DJ"}) req.headers['Cookie'] = self.cookies.output() req.method = 'POST' res, ses, flash = get_response(req, app=dj_app) if uname in seen_unames: self.assertNotEqual(u"success", flash[0][1]) else: self.assertEqual(u"success", flash[0][1]) seen_unames.add(uname) self.set_cookie(res) # Run some searches on Djs name, uname = name_pairs[1] fname = name.split()[0] djkey = Dj.get_key_by_username(uname) for i in range(len(fname)): req = Request.blank('/ajax/djcomplete?query=%s'%fname[:i+1]) res, ses, flash = get_response(req, app=main_app) found = json.loads(res.body) res_keys = [data['key'] for data in found['data']] self.assertIn(djkey.urlsafe(), res_keys) # Run some searches on Djs. Guy should NOT show up name = "Guy Fieri" fname = name.split()[0] for i in range(len(fname)): req = Request.blank('/ajax/djcomplete?query=%s'%fname[:i+1]) res, ses, flash = get_response(req, app=main_app) found = json.loads(res.body) res_keys = [data['key'] for data in found['data']] self.assertNotIn(djkey.urlsafe(), res_keys) # Modify this guy so that his name is different req = Request.blank('/dj/djs/%s'%djkey.urlsafe(), POST={ 'username': "******", 'fullname': "Guy Fieri", 'email': "guyguy", 'submit': "Edit DJ"}) req.headers['Cookie'] = self.cookies.output() req.method = 'POST' res, ses, flash = get_response(req, app=dj_app) print flash self.assertEqual(u"success", flash[0][1]) self.set_cookie(res) # Run some searches on Djs. Our changed guy shouldn't be here. name, uname = name_pairs[1] fname = name.split()[0] for i in range(len(fname)): req = Request.blank('/ajax/djcomplete?query=%s'%fname[:i+1]) res, ses, flash = get_response(req, app=main_app) found = json.loads(res.body) res_keys = [data['key'] for data in found['data']] self.assertNotIn(djkey.urlsafe(), res_keys) # Run some searches on Djs. Guy should show up name, uname = "Guy Fieri", "gfieri" fname = name.split()[0] for i in range(len(fname)): req = Request.blank('/ajax/djcomplete?query=%s'%fname[:i+1]) res, ses, flash = get_response(req, app=main_app) found = json.loads(res.body) res_keys = [data['key'] for data in found['data']] self.assertIn(djkey.urlsafe(), res_keys)