def post(self): if self.logged_in: try: if len(self.request.params.multi.dicts) > 1 and 'file' in self.request.params.multi.dicts[1]: file_info = self.request.POST['file'] file_content = file_info.file.read() if file_info.filename.endswith('.json'): events = json_to_events(file_content) if file_info.filename.endswith('.xlsx'): events = excel_to_events(file_content) if file_info.filename.endswith('.xls'): events = excel_to_events(file_content) if file_info.filename.endswith('.csv'): events = csv_to_events(file_content) if file_info.filename.endswith('.ics'): events = ics_to_events(file_content) new_attacks = [] for event in events: new_attack = attack.Attack(parent=self.current_user.key) new_attack.start_time = event['Start'].replace(tzinfo=None) new_attack.duration = event['Duration'] new_attack.comment = event['Comment'] new_attack.start_text = create_start_text(event['Start']) new_attack.duration_text = create_duration_text(event['Duration']) new_attacks.append(new_attack) ndb.put_multi(new_attacks) self.response.out.write(simplejson.dumps({'message': str(len(new_attacks)) + ' attacks uploaded.'})) support_email("List uploaded", str(len(new_attacks)) + " attacks uploaded.") except Exception as e: trace = traceback.format_exc() logging.error(e.message) logging.error(trace) self.response.set_status(500) self.response.out.write(simplejson.dumps({'message': "Upload failed.<br/>Sorry! We're looking at it."})) support_email("Upload failed", "Couldn't upload: " + file_info.filename) else: self.response.out.write(simplejson.dumps({'message': 'Please login before uploading attacks.'}))
def _on_signin(self, data, auth_info, provider, extra=None): """Callback whenever a new or existing user is logging in. data is a user info dictionary. auth_info contains access token or oauth token and secret. extra is a dict with additional params passed to the auth init handler. See what's in it with e.g. logging.info(auth_info) """ logging.info(data) auth_id = '%s:%s' % (provider, data['id']) user = self.auth.store.user_model.get_by_auth_id(auth_id) _attrs = self._to_user_model_attrs(data, self.USER_ATTRS[provider]) if user: logging.debug('Found existing user to log in') # Existing users might've changed their profile data so we update our # local model anyway. This might result in quite inefficient usage # of the Datastore, but we do this anyway for demo purposes. # # In a real app you could compare _attrs with user's properties fetched # from the datastore and update local user in case something's changed. user.populate(**_attrs) user.provider = provider if not hasattr(user, 'admin'): user.admin = False user.put() self.auth.set_session(self.auth.store.user_to_dict(user)) else: # check whether there's a user currently logged in # then, create a new user if nobody's signed in, # otherwise add this auth_id to currently logged in user. if self.auth.get_user_by_session() is not None: logging.debug('Updating currently logged in user') u = self.current_user u.populate(**_attrs) # The following will also do u.put(). Though, in a real app # you might want to check the result, which is # (boolean, info) tuple where boolean == True indicates success # See webapp2_extras.appengine.auth.models.User for details. u.add_auth_id(auth_id) else: logging.debug('Creating a brand new user') _attrs['share_report_key'] = generate_string(8) _attrs['share_report_and_list_key'] = generate_string(7) _attrs['provider'] = provider _attrs['admin'] = False ok, user = self.auth.store.user_model.create_user(auth_id, **_attrs) if ok: self.auth.set_session(self.auth.store.user_to_dict(user)) support_email("New User", "A new account has been created: " + str(_attrs)) destination_url = '/report' if extra is not None: params = webob.multidict.MultiDict(extra) destination_url = str(params.get('destination_url', '/report')) return self.redirect(destination_url)
def post(self): if self.logged_in: try: if len(self.request.params.multi.dicts ) > 1 and 'file' in self.request.params.multi.dicts[1]: file_info = self.request.POST['file'] file_content = file_info.file.read() if file_info.filename.endswith('.json'): events = json_to_events(file_content) if file_info.filename.endswith('.xlsx'): events = excel_to_events(file_content) if file_info.filename.endswith('.xls'): events = excel_to_events(file_content) if file_info.filename.endswith('.csv'): events = csv_to_events(file_content) if file_info.filename.endswith('.ics'): events = ics_to_events(file_content) new_attacks = [] for event in events: new_attack = attack.Attack( parent=self.current_user.key) new_attack.start_time = event['Start'].replace( tzinfo=None) new_attack.duration = event['Duration'] new_attack.comment = event['Comment'] new_attack.start_text = create_start_text( event['Start']) new_attack.duration_text = create_duration_text( event['Duration']) new_attacks.append(new_attack) ndb.put_multi(new_attacks) self.response.out.write( simplejson.dumps({ 'message': str(len(new_attacks)) + ' attacks uploaded.' })) support_email("List uploaded", str(len(new_attacks)) + " attacks uploaded.") except Exception as e: trace = traceback.format_exc() logging.error(e.message) logging.error(trace) self.response.set_status(500) self.response.out.write( simplejson.dumps({ 'message': "Upload failed.<br/>Sorry! We're looking at it." })) support_email("Upload failed", "Couldn't upload: " + file_info.filename) else: self.response.out.write( simplejson.dumps( {'message': 'Please login before uploading attacks.'}))