def supply(request): """ If the HTTP Verb is GET: Provide a form for adding a new spam email message. If the HTTP Verb is POST: Save and process a new email message, and view the resulting message. :param HttpRequest request: A web request. :rtype: An HttpResponse object. """ user = users.get_current_user() if user is None: return redirect(users.create_login_url('/supply')) usetting = UserSetting.gql('WHERE userid = :1', user.user_id()) if usetting.count() != 1 or not usetting.get().is_contrib: return HttpResponseForbidden('<h1>Authorization Required</h1>') if request.method == 'GET': ctx = RequestContext(request, {}) return render_to_response('input_form.html', context_instance=ctx) title = request.POST['title'] input = request.POST['input'].lstrip('\t\n\r ') date = datetime.now() email = Email(title=title, body=input, date=date, views=0, rating=0) email.put() _process_new(email) return redirect('/view/%s' % email.key())
def user(request): user = users.get_current_user() if user: usetting = UserSetting.gql('WHERE userid = :1', user.user_id()) if usetting.count() == 0: usetting = UserSetting(userid=user.user_id(), email=db.Email(user.email()), is_contrib=False) usetting.put() userurl = users.create_logout_url(request.get_full_path()) else: userurl = users.create_login_url(request.get_full_path()) return { "user": user, "userurl": userurl }
def parseRequest(self, urlpart): self.customRules = self.request.get_all('c') self.customRules += (urlsafe_b64decode(r.encode('ascii')) for r in self.request.get_all('e')) match = self.userPacRegxp.match(unquote(urlpart).strip()) if match: setting = UserSetting.gql('WHERE pacName=:1', match.group(1).lower()).get() if setting is None: return urlpart = match.group(2) or setting.defaultProxy self.customRules += setting.customRules self.settingTime = setting.lastModified else: self.settingTime = datetime.min match = self.proxyRegxp.match(urlpart.lower()) if match is None: return self.proxyDict = match.groupdict() if self.proxyDict['name']: if self.proxyDict['name'] not in PRESET_PROXIES: return self.proxyString = PRESET_PROXIES[self.proxyDict['name']][1] elif self.proxyDict['type']: self.proxyDict['type'] = 'SOCKS' if self.proxyDict[ 'type'] == 'socks' else 'PROXY' self.proxyString = '%(type)s %(host)s:%(port)s' % self.proxyDict # Chrome expects 'SOCKS5' instead of 'SOCKS', see http://j.mp/pac-test if useragent.family() == 'Chrome': self.proxyString = self.proxyString.replace('SOCKS ', 'SOCKS5 ') return True
def parseRequest(self, urlpart): self.customRules = self.request.get_all('c') self.customRules += (urlsafe_b64decode(r.encode('ascii')) for r in self.request.get_all('e')) match = self.userPacRegxp.match(unquote(urlpart).strip()) if match: setting = UserSetting.gql('WHERE pacName=:1', match.group(1).lower()).get() if setting is None: return urlpart = match.group(2) or setting.defaultProxy self.customRules += setting.customRules self.settingTime = setting.lastModified else: self.settingTime = datetime.min match = self.proxyRegxp.match(urlpart.lower()) if match is None: return self.proxyDict = match.groupdict() if self.proxyDict['name']: if self.proxyDict['name'] not in PRESET_PROXIES: return self.proxyString = PRESET_PROXIES[self.proxyDict['name']][1] elif self.proxyDict['type']: self.proxyDict['type'] = 'SOCKS' if self.proxyDict['type'] == 'socks' else 'PROXY' self.proxyString = '%(type)s %(host)s:%(port)s' % self.proxyDict # Chrome expects 'SOCKS5' instead of 'SOCKS', see http://j.mp/pac-test if useragent.family() == 'Chrome': self.proxyString = self.proxyString.replace('SOCKS ', 'SOCKS5 ') return True
def incoming(request): """ Accept a new email message directly via the AppEngine email facility. The entire email message is contained in the POST body of *email*. :param HttpRequest request: A web request. :rtype: An HttpResponse object. """ logging.info('Incoming email received.') try: msg = InboundEmailMessage(request.raw_post_data) usetting = UserSetting.gql('WHERE email = :1', msg.sender) if usetting.count() == 0: logging.warn('Received email from an unrecognized sender: ' + msg.sender) return render_to_response('msg_receipt.email', mimetype='text/plain') if not usetting.get().is_contrib: logging.warn('Received email from an unauthorized contributor: ' + msg.sender) return render_to_response('msg_receipt.email', mimetype='text/plain') content = '' for content_type, body in msg.bodies('text/plain'): headers = True date = False for line in str(body).split('\n'): if not date: parts = line.split(' ') line = ' '.join(parts[len(parts)-5:]) date = datetime.strptime(line, '%a %b %d %H:%M:%S %Y') logging.debug(str(date)) if headers and line == '': headers = False elif not headers: content += '%s\n' % line if content == '': logging.warn('Received an email, but no text/plain bodies.') else: logging.info('Compiled plain-text email: body length=%d' % len(content)) newtitle = msg.subject.replace('\n','').replace('\r','') content = content.lstrip('\t\n\r ') email = Email(title=newtitle, body=content, date=date, views=0, rating=0) email.put() logging.info('Processing new data for tokens & tags') _process_new(email) except Exception, ex: logging.error('Error processing new email. %s' % ex)
def get(self): user = users.get_current_user() self.lastModified(template.mtime('index.html')) self.response.out.write(template.render('index.html', presetProxies=((k, v[0]) for k, v in PRESET_PROXIES.items()), pacUrlPrefix=PAC_URL_PREFIX, pacUserUrlPrefix=PAC_USER_URL_PREFIX, userSetting=UserSetting.get_by_key_name(user.user_id()) if user else None, ))
def get(self): user = users.get_current_user() self.lastModified(template.mtime('index.html')) self.response.out.write( template.render( 'index.html', presetProxies=((k, v[0]) for k, v in PRESET_PROXIES.items()), pacUrlPrefix=PAC_URL_PREFIX, pacUserUrlPrefix=PAC_USER_URL_PREFIX, userSetting=UserSetting.get_by_key_name(user.user_id()) if user else None, ))
def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() self.user_a = User() self.user_a.put() self.user_b = User() self.user_b.name = 'Foo Bar' self.user_b.clientID = 'C123' self.user_b.put() self.setting = UserSettingKey() self.setting.name = 'SEND_EMAIL' self.setting.description = 'Acceptance to receiv E-mail' self.setting.values = ['0', '1'] self.setting.put() self.user_setting = UserSetting() self.user_setting.setting_key = self.setting.key self.user_setting.user = self.user_b.key self.user_setting.value = '0' self.user_setting.put() self.email_a = UserEmail() self.email_a.email = '*****@*****.**' self.email_a.is_activated = False self.email_a.is_default = False self.email_a.user = self.user_b.key self.email_a.put() self.email_b = UserEmail() self.email_b.email = '*****@*****.**' self.email_b.is_activated = True self.email_b.is_default = True self.email_b.user = self.user_b.key self.email_b.put()
def post(self): user = users.get_current_user() if not user or not self.request.get('customize'): return pacName = self.request.get('pacname', '').lower() if pacName != user.nickname().lower(): self.error(400) return UserSetting( key_name=user.user_id(), defaultProxy=self.request.get('proxy'), pacName=pacName, customRules=self.request.get('addrules').splitlines(), ).put() if self.request.get('usage') != 'online': self.redirect('/%s%s%s?download' % (PAC_URL_PREFIX, PAC_USER_URL_PREFIX, pacName), permanent=False)
def setsetting(user, name, value): # this makes sure it exists setting = register.get(name) encoded = setting.encode(value) if value == setting.default(): # setting is default # using filter to avoid problems when setting does not exist # TODO: check if 'filter' instead of 'get' is faster UserSetting.objects.filter(name=name, user=user).delete() return True # succesfully saved (by deleting) try: # TODO: use update here instead obj = UserSetting.objects.get(name=name, user=user) except UserSetting.DoesNotExist: obj = UserSetting(name=name, user=user, value=encoded) obj.save() else: obj.value = encoded obj.save() return True # succesfully saved new value
def incoming(request): """ Accept a new email message directly via the AppEngine email facility. The entire email message is contained in the POST body of *email*. :param HttpRequest request: A web request. :rtype: An HttpResponse object. """ logging.info('Incoming email received.') try: msg = InboundEmailMessage(request.raw_post_data) usetting = UserSetting.gql('WHERE email = :1', msg.sender) if usetting.count() == 0: logging.warn('Received email from an unrecognized sender: ' + msg.sender) return render_to_response('msg_receipt.email', mimetype='text/plain') if not usetting.get().is_contrib: logging.warn('Received email from an unauthorized contributor: ' + msg.sender) return render_to_response('msg_receipt.email', mimetype='text/plain') content = '' for content_type, body in msg.bodies('text/plain'): headers = True date = False for line in str(body).split('\n'): if not date: parts = line.split(' ') line = ' '.join(parts[len(parts) - 5:]) date = datetime.strptime(line, '%a %b %d %H:%M:%S %Y') logging.debug(str(date)) if headers and line == '': headers = False elif not headers: content += '%s\n' % line if content == '': logging.warn('Received an email, but no text/plain bodies.') else: logging.info('Compiled plain-text email: body length=%d' % len(content)) newtitle = msg.subject.replace('\n', '').replace('\r', '') content = content.lstrip('\t\n\r ') email = Email(title=newtitle, body=content, date=date, views=0, rating=0) email.put() logging.info('Processing new data for tokens & tags') _process_new(email) except Exception, ex: logging.error('Error processing new email. %s' % ex)
class Test(unittest.TestCase): def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() self.user_a = User() self.user_a.put() self.user_b = User() self.user_b.name = 'Foo Bar' self.user_b.clientID = 'C123' self.user_b.put() self.setting = UserSettingKey() self.setting.name = 'SEND_EMAIL' self.setting.description = 'Acceptance to receiv E-mail' self.setting.values = ['0', '1'] self.setting.put() self.user_setting = UserSetting() self.user_setting.setting_key = self.setting.key self.user_setting.user = self.user_b.key self.user_setting.value = '0' self.user_setting.put() self.email_a = UserEmail() self.email_a.email = '*****@*****.**' self.email_a.is_activated = False self.email_a.is_default = False self.email_a.user = self.user_b.key self.email_a.put() self.email_b = UserEmail() self.email_b.email = '*****@*****.**' self.email_b.is_activated = True self.email_b.is_default = True self.email_b.user = self.user_b.key self.email_b.put() def tearDown(self): self.testbed.deactivate() def testGetSettings(self): self.assertEqual(0, len(self.user_a.get_settings())) settings = self.user_b.get_settings() self.assertEqual(1, len(settings)) self.assertEqual('SEND_EMAIL', settings[0].setting_key.get().name) def testUpdateSettings(self): self.user_a.update_setting(self.setting, '1') self.assertEqual('0', self.user_setting.value) self.user_b.update_setting(self.setting, '1') self.assertEqual('1', self.user_setting.value) def testUpdate(self): self.user_a.upate('*****@*****.**', 'Bar Foy') self.assertEqual('Bar Foy', self.user_a.name) self.assertTrue(self.user_a.has_email()) self.assertEqual(self.user_a, UserEmail.find_user('*****@*****.**')) self.user_b.upate('*****@*****.**', 'Foo Bar') self.assertEqual('Foo Bar', self.user_b.name) self.assertEqual('*****@*****.**', self.user_b.get_default_email()) self.assertEqual(self.user_b, UserEmail.find_user('*****@*****.**')) def testHasEmail(self): self.assertFalse(self.user_a.has_email()) self.assertTrue(self.user_b.has_email()) def testDefaultEmailConsistancy(self): self.assertEqual(None, self.user_a.get_default_email()) self.assertEqual('*****@*****.**', self.user_b.get_default_email()) UserEmail.create_or_update(self.user_a, '*****@*****.**', True) UserEmail.create_or_update(self.user_b, '*****@*****.**', True) user_a_emails = UserEmail.gql("WHERE user = :1", self.user_a.key).fetch() user_b_emails = UserEmail.gql("WHERE user = :1", self.user_b.key).fetch() self.assertEqual(1, len(user_a_emails)) self.assertTrue(user_a_emails[0].is_default) self.assertEqual(3, len(user_b_emails)) for email in user_b_emails: if '*****@*****.**' == email.email: self.assertTrue(email.is_default) else: self.assertFalse(email.is_default) def testFindByEmail(self): user = User.find_by_email('*****@*****.**') self.assertEqual(self.user_b, user) user = User.find_by_email('*****@*****.**') self.assertEqual(self.user_b, user) user = User.find_by_email('*****@*****.**') self.assertEqual(None, user) def testClinet(self): self.assertEqual(self.user_b, User.client('C123')) user = User.client('C001') self.assertNotEqual(None, user) self.assertEqual('C001', user.clientID)