def post(self) -> ({str: str}, HTTPStatus): """ Delete an existing Theme. :param name: name of Theme to delete. :param id: id of Theme to delete. :type name: str :type id: str :returns: A no content with a http status code of 204, otherwise a JSON of the error details and the appropriate http status code """ if not get_jwt_claims()['admin']: return {"error": "administration privileges required"}, HTTPStatus.FORBIDDEN # Get arguments args = self.reqparser.parse_args() # does the theme exist? theme = Theme.get_by_name(args["name"]) if "name" in args else Theme.get_by_id(args["id"]) if not theme: # cannot delete a theme that does not exist. return {'error': 'Theme does not exists.', 'id': " ", 'name': args["name"]}, HTTPStatus.BAD_REQUEST sub_themes = SubTheme.get_by_theme_id(theme.id) for sub_theme in sub_themes: sub_theme.delete() sub_theme.commit() # delete the theme theme.delete() theme.commit() return "", HTTPStatus.NO_CONTENT
def post(self) -> ({str: str}, HTTPStatus): """ Rename an existing Theme :post_argument current_name: name of SubTheme :post_argument new_name: new name of SubTheme :post_type current_name: str :post_type new_name: str :returns: A JSON of the changes made to the theme with a http status code of 200, otherwise a JSON of the error details and the appropriate http status code """ if not get_jwt_claims()['admin']: return { "error": "administration privileges required" }, HTTPStatus.FORBIDDEN # Get arguments args = self.reqparser.parse_args() # Check the current theme name and the new theme name is not empty, abort if it is empty if not args["current_name"] or not args["new_name"]: return ({ 'error': 'Theme name cannot be empty', 'name': args["current_name"], 'new_name': args["new_name"] }, HTTPStatus.BAD_REQUEST) theme = Theme.get_by_name(args["current_name"]) if not theme: # cannot rename a theme that does not exist. return { 'error': 'Theme does not exists.', 'id': " ", 'name': args["current_name"] }, HTTPStatus.BAD_REQUEST # Does the new name for theme exist? if Theme.get_by_name(args["new_name"]): return { 'error': 'Cannot rename theme to {} ; Theme {} already exists.'.format( args["new_name"], args["new_name"]), 'id': "", 'name': args["current_name"] }, HTTPStatus.BAD_REQUEST # rename the new theme theme.name = args["new_name"] theme.save() theme.commit() return ({ "message": "Theme renamed", "id": theme.id, "old_name": args["current_name"], "new_name": theme.name }, HTTPStatus.OK)
def create_theme(self, name: str) -> db.Model: """ Create Theme :param name: Name of theme :return: Theme """ theme = Theme(name=name) theme.save() return theme
def delete(): id = int(request.args.get('id')) token = request.args.get('token') u = current_user() t = get_token(u.id) if t.content == token: flash_token(t) b = Theme.find(id) log('删除 theme 用户是', u, b) delete_file(local_image_director, b.banner_img) Theme.delete(id) return redirect(url_for('board.edit')) else: abort(403)
def edit(request, theme_id): """ This is ripped out of Google's example. This is ugly and I hate it. """ user = users.GetCurrentUser() theme = None editing = False if theme_id: editing = True theme = Theme.theme(theme_id=theme_id) if theme is None: return http.HttpResponseNotFound('No theme exists with that key (%r)' % theme_id) form = ThemeForm(data=request.POST or None, instance=theme) if not request.POST: return respond(request, user, 'themes/new', { 'form': form, 'theme': theme, 'language': 'python', 'code': SNIPPETS['python'] }) errors = form.errors if not errors: try: theme = form.save(commit=False) except ValueError, err: errors['__all__'] = unicode(err)
def index(): bs = Board.all() te_top = [] te_centers = [] te_top.append(bs[0]) te_top.append(Theme.find_all(board_id=bs[0].id)) for i, b in enumerate(bs): if i > 0: temp = [] themes = Theme.find_all(board_id=b.id) temp.append(b) temp.append(themes) te_centers.append(temp) return render_template("theme/index.html", te_top=te_top, te_centers=te_centers)
def test_return_empty_params(self): """ Test sending empty param's return themes """ themes = Theme.get_all() response = self.testing_client.get('/data') self.assertEqual([a.json() for a in themes], response.get_json())
def rate(request, theme_id): user = users.GetCurrentUser() theme = Theme.theme(theme_id=theme_id) rating = request.POST.get('rating', 0.0) theme.rate(rating) theme.save() return HttpResponse()
def edit(): u = current_user() token = get_token(u.id) bs = Board.all() ts = Theme.all() return render_template("/board/forum_edit.html", bs=bs, ts=ts, token=token.content)
def create_theme(self) -> None: """ Create a Theme """ self.theme = Theme.get_by_name("_test_theme_") if self.theme: return self.theme = Theme("_test_theme_") self.theme.save() try: self.theme.commit() except Exception as exp: logger.error(exp) self.tearDown() if not self.theme: self.fail()
def get(self) -> ([Theme], HTTPStatus): """ Fetch Themes Parameters can be passed using a POST request that contains a JSON with the following fields: :param limit: the maximum number of entries to return :param name: themes name :param id: the themes identification number :type limit: int :type name: str :type id: str :return: a list of Theme/s and an HTTPStatus code of 200 on success otherwise an JSON with an error message and appropriate http status """ # Get arguments passed in POST request args = self.reqparser.parse_args() # fetch themes using arguments passed in post request themes = [] if "id" in args: theme = Theme.get_by_id(args["id"]) if theme: themes.append(theme.json()) elif "name" in args: theme = Theme.get_by_name(args["name"]) if theme: themes.append(theme.json()) else: [themes.append(theme.json()) for theme in Theme.get_all()] # were any themes found in the database? if len(themes) < 1: # no themes were found return [], HTTPStatus.OK if "limit" in args: try: themes = themes[:int(args["limit"])] except ValueError: return {"error": "Limit parsed is not an int"}, HTTPStatus.BAD_REQUEST # themes were found return themes, HTTPStatus.OK
def get(request, theme_id): user = users.GetCurrentUser() theme = Theme.theme(theme_id=theme_id) lang = 'python' return respond(request, user, 'themes/theme', { 'user': user, 'theme': theme, 'code': tokenize_to_html(SNIPPETS[lang]) })
def delete(): # TODO token与用户验证 topic_id = int(request.args.get('id')) tp = Topic.find(topic_id) print('删除 的 topic 是', tp) t = Theme.find(tp.theme_id) t.topic_num -= 1 t.save() delete_file(local_image_director, tp.banner_img) tp.delete() return redirect(url_for('theme.detail'))
def get(self): contents = Content.all().fetch(1000) theme_packages = ThemePackage.all().fetch(1000) themes = Theme.all().fetch(1000) pages = Page.all().fetch(1000) images = Image.all().fetch(1000) roles = Role.all().fetch(1000) sections = Section.all().fetch(1000) _users = User.all().fetch(1000) actions = ACTIONS template_values = {'logout_url':self.ws.users.create_logout_url('/'),'theme_packages': theme_packages,'themes': themes, 'images': images, 'pages': pages, 'contents':contents, 'roles':roles, 'users':_users, 'actions': actions, 'sections': sections, 'site': self.ws.site} self.response.out.write(template.render('templates/manage.html',template_values))
def get_all_themes(self, user_id: int = None) -> None: """ Create Theme Tree containing all Themes, SubThemes, Attributes and Attribute Aliases if user_id exists :param user_id: User Id for Attribute Aliases """ theme_ids = {theme.id for theme in Theme.get_all()} self.response.extend([ resp for resp in [ self.create_theme_tree(theme_id, user_id) for theme_id in theme_ids ] if resp ])
def tearDown(self) -> None: """ Clean up all dependencies after tests""" for alias in self.aliases: try: if AttrAlias.get_by_user_id(self.user.id): alias.delete() alias.commit() except Exception: pass for attr in self.attributes: try: if Attributes.get_by_id(attr.id): attr.delete() attr.commit() except Exception: pass for sub_theme in self.sub_themes: try: if SubTheme.get_by_id(sub_theme.id): sub_theme.delete() sub_theme.commit() except Exception: pass for unit in self.units: try: if Unit.get_by_id(unit.id): unit.delete() unit.commit() except Exception: pass try: if Theme.get_by_id(self.theme.id): self.theme.delete() self.theme.commit() except Exception: pass self.client.post('/logout', headers=self.auth_header) if self.user: if Users.find_by_id(self.user.id): try: self.user.delete() self.user.commit() except Exception: pass self.app_context.pop()
def add(): u = current_user() if file_exists(): return redirect(url_for('board.new', tip='no name/file')) filename = allow_save(local_image_director) if filename is not None: form = request.form m = Theme.new(form, user_id=u.id, user_name=u.username) m.banner_img = filename m.save() return redirect(url_for('board.new', tip='success'))
def test_return_subthemes_of_theme(self): """ Adding a theme and subtheme (linked to to that theme) to the database and then testing to see if it is retrieved correctly """ theme = Theme(name='Test_Theme') theme.save() theme.commit() sub_theme = SubTheme(theme.id, "Test_Sub_Theme") sub_theme.save() sub_theme.commit() response = self.testing_client.get('/data', data=dict(theme=theme.id)) self.assertEqual(sub_theme.json(), response.get_json()) sub_theme.delete() sub_theme.commit() theme.delete() theme.commit()
def create_dummy_theme(self) -> Theme: """ Create a Theme :return: a Theme """ theme = Theme.get_by_name("_test_add_theme_") if not theme: theme = Theme("_test_add_theme_") theme.save() theme.commit() return theme return theme
def detail(id_page): topic_num = 5 (id, page) = check_idpage(id_page) (startn, endn) = ((page - 1) * topic_num, page * topic_num) m = Theme.find(id) pages = math.ceil(m.topic_num / topic_num) if m.topic_num < startn or startn < 0: abort(404) elif startn < m.topic_num < endn: tps = Topic.find_all(theme_id=m.id)[startn:] # tps = Topic.cache_all() else: tps = Topic.find_all(theme_id=m.id)[startn:endn] return render_template("theme/detail.html", theme=m, tps=tps, pgs=pages)
def create_dummy_theme(self) -> Theme: """ Create a Theme :return: a Theme instance """ theme = Theme.get_by_name("_test_add_theme_") if not theme: theme = Theme("_test_add_theme_") theme.save() theme.commit() self.dummy_ids.append(theme.id) return theme return theme
def tearDown(self): """ Handle the cleanup after the tests""" for theme_id in self.dummy_ids: theme = Theme.get_by_id(theme_id) if theme: theme.delete() theme.commit() self.client.post('/logout', headers=self.auth_header) if self.user: self.user.delete() self.user.commit() self.app_context.pop()
def update(): token = request.args.get('token') form = request.form id = int(form.get('id', -1)) name = form.get('name', '') u = current_user() t = get_token(u.id) if t.content == token and '' != name: flash_token(t) b = Theme.find(id) log('update board 用户是', u.id, u.username, b) b.name = name b.save() return redirect(url_for('board.edit')) else: abort(403)
def generate_admin_html(self, page, user): contents = Content.all().fetch(1000) roles = Role.all().fetch(1000) emaildata = {"contents": contents, "roles": roles} emailcontext = template.Context(emaildata) email_template = template.Template(open("templates/email.html").read()) email_html = email_template.render(emailcontext) admindata = { "page_edit": Page.to_form(self.request.path, "edit", page.key()), "theme_edit": Theme.to_form(self.request.path, "edit", page.theme.key(), page.key()), "page_key": page.key(), "path": self.request.path, "permission_table": Permission.get_table(page.key()), "sections": ( { "name": section.name.replace("section_", "").capitalize(), "theme_key": section.theme.key(), "theme_html": section.theme.html, "theme_css": section.theme.css, "theme_js": section.theme.js, "content": ( { "title": content.title, "content_edit": Content.to_edit_list("title", self.request.path), "content_form": Content.to_form( self.request.path, "edit", content.key(), section.key() ), "content_deepform": Content.to_form(self.request.path, rel_key=section.key()), } for content in section.get_contents() ), } for section in page.get_sections() ), "page_form": Page.to_form(self.request.path, rel_key=self.ws.site.key()), "user_form": User.to_form(self.request.path), "user_list": User.to_edit_list("email", self.request.path, True), "user_edit_form": User.to_form(self.request.path, "edit", user.key()), "user_import": open("defaults/admin/user_import.html").read(), "images": self.ws.site.images_for_use(), "email_blast": email_html, } context = template.Context(admindata) admin_template = template.Template(open("defaults/admin/tabs.html").read()) admin_html = admin_template.render(context) return admin_html
def args_db_checker(self, args) -> None: """ Check parsed Arguments and dependencies and Create appropriate RFC-7807 JSON Error Response :param args: Parsed Arguments in GET request """ invalid_params = [] if "user_id" in args: # Create JSON Error message if User does not exist if not Users.find_by_id(args["user_id"]) and args["user_id"] >= 0: invalid_params.append({ "name": "user_id", "value": args["user_id"], "reason": "User Not Found", "code": 404, "rollback": "Theme Tree will not contain Attribute Aliases" }) if "theme_id" in args: # Create JSON Error message if Theme does not exist if not Theme.get_by_id(args["theme_id"]): invalid_params.append({ "name": "theme_id", "value": args["theme_id"], "reason": "Theme Not Found", "code": 404, "rollback": "A Themes will be return in the Theme Tree" }) if invalid_params: # Put Error message in response self.response = [{ "error": dict(type='Request entries not found', title="Unable to fetch dB entries for parsed arguments", invalid_params=invalid_params) }]
def test_submit(): u = Unit('kg', 'Kilogram') u2 = Unit('g', 'Grams') u3 = Unit('km', 'KiloMeter') u.save() u2.save() u3.save() t = Theme('Environment') t2 = Theme('Transport') t.save() t2.save() st = SubTheme(t.id, 'Airquality') st2 = SubTheme(t2.id, 'Traffic') st.save() st2.save() db.session.commit()
def create_theme(name: str = 'Environment') -> Theme: """ Create a Theme :param name: Themes name :raises ValueError: If the new Theme is not persisted to the dB :return: A new Theme """ theme = Theme.get_by_name(name) if not theme: theme = Theme(name) theme.save() theme.commit() if not theme: logger.critical('ValueError raised while creating Theme') raise ValueError return theme
def get(self): args = self.parser.parse_args() theme, subtheme = None, None if "subtheme" in args: subtheme = args['subtheme'] if subtheme is not None and subtheme != '': attributes = Attributes.get_by_sub_theme_id(subtheme) return [a.json() for a in attributes], 200 elif "theme" in args: theme = args['theme'] if theme != "": subthemes = SubTheme.get_by_theme_id(theme) return [a.json() for a in subthemes], 200 if theme is None and subtheme is None: themes = Theme.get_all() return [a.json() for a in themes], 200 return {"error": "error occured while processing request"}, 400
def list(request, page=1): limit = 6 page = int(page) count = Theme.count() offset = (page-1) * limit num_pages = ceil(float(count) / float(limit)) + 1 current_page = int((float(offset) / float(count)) * num_pages) + 1 user = users.GetCurrentUser() themes = db.GqlQuery('SELECT * FROM Theme ORDER BY created DESC LIMIT %i OFFSET %i'%(limit, offset)) return respond(request, user, 'themes/list', { 'themes': themes, 'user': user, 'num_themes': offset, 'num_pages': num_pages, 'page_range': range(1, num_pages), 'page': page, })
def add(): u = current_user() user_info = { 'user_id': u.id, 'user_name': u.username, } if file_exists(): print('file=', request.files) return redirect(url_for('topic.new', tip='no name/theme img')) filename = allow_save(local_image_director) if filename is not None: form = request.form m = Topic.new(form, **user_info) m.banner_img = filename m.content = form.get('content', '') m.save() t = Theme.find(m.theme_id) t.topic_num += 1 t.save() return redirect(url_for('theme.index'))
def post(self) -> ({str: str}, HTTPStatus): """ Creates a new theme :post_argument name: the name of the new theme :post_type name: str :returns: A JSON with a message, theme id, and new themes name with a http status of 200 (OK) otherwise, A JSON with an appropriate error message and http status applicable to the error """ if not get_jwt_claims()['admin']: return { "error": "administration privileges required" }, HTTPStatus.FORBIDDEN # Get arguments args = self.reqparser.parse_args() # Check the theme name is not empty, abort if it is empty if not args["name"]: return { 'error': 'Theme cannot be empty', 'name': "''" }, HTTPStatus.BAD_REQUEST # Check theme does not exist (avoid duplicates) if Theme.get_by_name(args["name"]): return { 'error': 'Theme already exists.', 'id': " ", 'name': args["name"] }, HTTPStatus.BAD_REQUEST # Create the new theme theme = Theme(args["name"]) theme.save() theme.commit() return { "message": "New theme created", "id": theme.id, "name": theme.name }, HTTPStatus.OK
def create_theme_tree(self, theme_id: int, user_id: int) -> None: """ Create Theme Tree :param theme_id: Theme Id :param user_id: User Id """ theme = Theme.get_by_id(theme_id) if not theme: # Theme does not exist self.get_all_themes(user_id) return # Create Theme Trunk theme_tree = theme.serializable sub_themes = SubTheme.get_by_theme_id(theme_id) if not sub_themes: # No SubThemes in Theme return Trunk return theme_tree sub_theme_ids = {sub.id for sub in sub_themes} sub_list = [] for sub in sub_themes: sub_list.append(sub.serializable) attribute_by_sub_id = self.get_attributes(user_id, sub_theme_ids, theme_id) for sub in sub_list: # Add Attribute branches attr = attribute_by_sub_id.get(sub["id"]) if attr: sub["attributes"] = attr # Add SubTheme branches theme_tree["sub_themes"] = sub_list self.response.append(theme_tree)
def get(self): args = self.parser.parse_args() theme, subtheme, attribute_data, sensor, sensor_name, sensor_attribute, attributes, sensorid, n_predictions, predictions, grouped, harmonising_method, per_sensor, freq = None, None, None, None, None, None, [], None, 100, None, None, None, None, '1H' if 'theme' in args: theme = args['theme'] if 'subtheme' in args: subtheme = args['subtheme'] if 'attributedata' in args: attribute_data = args['attributedata'] if 'attribute' in args and args['attribute'] is not None: _attributes = args['attribute'] if _attributes != '': attributes = _attributes.split(',') if 'sensor' in args and args['sensor'] is not None: sensor = args['sensor'] if sensor != '': if sensor == 'all': sensors = Sensor.get_all() return [a.json() for a in sensors], 200 else: return (Sensor.get_by_id(sensor)).json(), 200 if 'sensorname' in args and args['sensorname'] is not None: sensor_name = args['sensorname'] if sensor_name != '': _sensors = sensor_name.split(',') _by_name = Sensor.get_by_name_in(_sensors) return [a.json() for a in _by_name], 200 if 'sensorattribute' in args and args['sensorattribute'] is not None: sensor_attribute = args['sensorattribute'] if sensor_attribute != '': _sen_attrs_ids = sensor_attribute.split(',') _sen_attrs = SensorAttribute.get_by_id_in(_sen_attrs_ids) attrs_ids = [_id.a_id for _id in _sen_attrs] _attributes = Attributes.get_by_id_in(attrs_ids) return [a.json() for a in _attributes], 200 if 'grouped' in args: grouped = args['grouped'] if 'harmonising_method' in args: harmonising_method = args['harmonising_method'] if 'per_sensor' in args: per_sensor = args['per_sensor'] if 'freq' in args: freq = args['freq'] if 'predictions' in args: predictions = args['predictions'] if predictions >=100: predictions = 100 if 'n_predictions' in args: n_predictions = args['n_predictions'] if 'sensorid' in args: sensorid = args['sensorid'] if theme is None and subtheme is None \ and len(attributes) == 0 and attribute_data is None \ and sensor is None and sensor_name is None and sensor_attribute is None: themes = Theme.get_all() return [a.json() for a in themes], 200 if attribute_data is not None: global LIMIT, OFFSET data = None operation = None if 'limit' in args and args['limit'] is not None: LIMIT = args['limit'] if 'offset' in args and args['offset'] is not None: OFFSET = args['offset'] if 'operation' in args and args['operation'] is not None: operation = args['operation'] if ('fromdate' in args and args['fromdate'] is not None and 'todate' in args and args['todate'] is not None): data = self.get_attribute_data(attribute_data, LIMIT, OFFSET, args['fromdate'], args['todate'], operation) if predictions: data.append(self.get_predictions(attribute_table = data[0]["Attribute_Table"], sensor_id = sensorid, n_pred = n_predictions)) else: if grouped: if harmonising_method: data = self.get_attribute_data(attribute_data, LIMIT, OFFSET, operation=operation) data = request_harmonised_data(data, harmonising_method=harmonising_method) else: data = self.get_attribute_data(attribute_data, LIMIT, OFFSET, operation=operation) data = request_grouped_data(data, per_sensor=per_sensor, freq=freq) else: data = self.get_attribute_data(attribute_data, LIMIT, OFFSET, operation=operation) if predictions: #### Ceck for data if data[0]["Total_Records"] != 0: #### Check for non numeric data if is_number(data[0]["Attribute_Values"][0]["Value"]): data.append(self.get_predictions(attribute_table = data[0]["Attribute_Table"], sensor_id = sensorid, n_pred = n_predictions)) else: print("Cannot predict non-numeric data") pass else: pass return data, 200 if attributes: _attrs = [] attr = Attributes.get_by_name_in(attributes) for a in attr: _attrs.append(a.json()) return _attrs, 200 if subtheme is not None and subtheme != '': attributes = Attributes.get_by_sub_theme_id(subtheme) return [a.json() for a in attributes], 200 if theme is not None and theme != '': subthemes = SubTheme.get_by_theme_id(theme) return [a.json() for a in subthemes], 200 return { "error": "error occured while processing request" }, 400
def post(self) -> (dict, HTTPStatus): """ Get dummy data from CSV. Store dummy data in database :param file_name: File name to extract data from. :return: A Status Report detailing the dB Entries created and an HTTP Status code 200 on success otherwise, a JSON error message is returned with the appropriate HTTPStatus code """ args = self.reqparser.parse_args() # Get size of tables before import self.loc_stats["before"] = db.session.query(func.count( LocationData.id)).scalar() self.tracker_stats["before"] = db.session.query(func.count( Tracker.id)).scalar() # Fetch Data from CSV try: df = pd.read_csv(args['file_name']) except IOError as ioe: logger.error("Unable to parse CSV data to dataframe", ioe.with_traceback(ioe.__traceback__)) return dict(error="Unable to parse CSV data to dataframe", trackback=ioe.with_traceback( ioe.__traceback__)), HTTPStatus.BAD_REQUEST moving_theme = Theme.get_by_name("Moving_Sensors") if moving_theme: moving_sensor_theme_id = moving_theme.id else: moving_sensor_theme = Theme("Moving_Sensors") moving_sensor_theme.save() moving_sensor_theme.commit() moving_sensor_theme_id = moving_sensor_theme.id moving_subtheme = SubTheme.get_by_name("Moving_Airquality") if moving_subtheme: moving_sensor_subtheme_id = moving_subtheme.id else: moving_sensor_subtheme = SubTheme(moving_sensor_theme_id, "Moving_Airquality") moving_sensor_subtheme.save() moving_sensor_subtheme.commit() moving_sensor_subtheme_id = moving_sensor_subtheme.id # Trackers must be unique, Fetch trackers and make dB entries for # each unique tracker unique_tracker_ids = df["tracker"].unique() for tracker_id in unique_tracker_ids: self.t_ids[self.create_trackers(str(tracker_id), moving_sensor_subtheme_id)] = 0 # Define Location data Pandas DataFrame Column names loc_df = df[[ 'tracker', 'datetime', 'latitude', 'longitude', 'speed', 'heading', 'elevation', 'charger', 'battery', 'signalquality', 'satcnt' ]] # Drop all entries that are incomplete have NaN or None/ Null values loc_df = loc_df.dropna() # Store Location Data in the dB for index, row in loc_df.iterrows(): self.add_location_data(row['tracker'], row['datetime'], row['latitude'], row['longitude'], row['speed'], row['heading'], row['elevation'], row['charger'], row['battery'], row['signalquality'], row['satcnt']) self.loc_stats["after"] = db.session.query(func.count( LocationData.id)).scalar() self.tracker_stats["after"] = db.session.query(func.count( Tracker.id)).scalar() return self.status_report(), 200
def index(request): user = users.GetCurrentUser() return respond(request, user, 'home/index', { 'most_downloaded': Theme.most_downloaded(), })
def get(self): if self.ws.site is None: self.redirect("/install") return False path = self.request.path # print path user_control = "" user_label = "" def kv_q(x): x = x.split("=") if len(x) < 2: return x[0] return {x[0]: x[1]} page = Page.get_by_name(path) if not page: self.error(404) return False if not self.permission_check(page): self.error(403) self.redirect("/") return False admin_html = "" if page: if not page.theme: page.theme = Theme.create( { "name": ["default" + page.name], "html": [open("defaults/template.html").read()], "css": [open("defaults/template.css").read()], "js": [""], } ) page.put() if not page.sections: page.get_or_make_sections() user = self.ws.users.get_current_user(self) if user: auth = [{"user_control": self.ws.users.create_account_url(path), "user_label": "Account"}] if self.ws.users.is_current_user_admin(self): admin_html = self.generate_admin_html(page, user) else: auth = [ {"user_control": self.ws.users.create_register_url(path), "user_label": "Register"}, {"user_control": self.ws.users.create_login_url(path), "user_label": "Login"}, ] page_theme = page.theme page_content_template = template.Template(page.build_template()) sections = page.get_sections() section_dict = {} site_users = User.all().fetch(1000) for section in sections: section_dict[section.name] = section user_control_link = "" for control in auth: user_control_link += "<a href='%s' class='account control'>%s</a>" % ( control["user_control"], control["user_label"], ) page_content_template_values = { "site_users": site_users, "ws": self.ws, "page": page, "sections": section_dict, "user_control_link": user_control_link, } page_content = self.render_string(page_content_template, page_content_template_values) page_template_html = open("defaults/outer.html").read() page_template = template.Template(page_template_html) template_values = { "title": page.title, "css": page_theme.css, "content": page_content, "js": page_theme.js, "admin_content": admin_html, } self.render_string_out(page_template, template_values) else: self.error(404)
from pymongo import MongoClient from urlparse import urlparse from models.theme import Theme logging.basicConfig() config = local_settings.env app = Flask(config.get('APPLICATION_NAME', 'email_render')) theme_logger = logging.getLogger('theme') ql = logger(logger=theme_logger, level=logging.INFO) uri = "localhost" client = MongoClient(uri) db = client['affiliate_website'] collection = db['themes'] theme = Theme(ql, collection) #--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- # ERROR HANDLING @app.errorhandler(500) def internal_500_error(exception): app.logger.exception(exception) return pprint.pformat(exception) @app.errorhandler(404) def internal_404_error(exception): app.logger.exception(exception) return 'awe<br/>\n%s<br/>\n%s' % (exception, request.url), 404
class TestGetThemeTree(TestCase): def setUp(self): """ Setup FlaskClient for tests, create an admin user and create the authorization header for requests to the FlaskClient """ self.client, self.app_context = self.create_client() self.user = self.create_admin_user() self.auth_header = self.get_auth_header() self.units = [] self.theme = None self.create_theme() self.sub_themes = [] self.create_sub_themes(3) self.attributes = [] self.create_attributes(4) self.aliases = [] self.create_attribute_alias() @staticmethod def create_client() -> (FlaskClient, AppContext): """ Create a FlaskClient :returns: A FlaskClient and a AppContext """ test_app = create_app(DATABASE_NAME='test_analysis', TESTING=True) testing_client = test_app.test_client() test_app_context = test_app.app_context() test_app_context.push() return testing_client, test_app_context def create_theme(self) -> None: """ Create a Theme """ self.theme = Theme.get_by_name("_test_theme_") if self.theme: return self.theme = Theme("_test_theme_") self.theme.save() try: self.theme.commit() except Exception as exp: logger.error(exp) self.tearDown() if not self.theme: self.fail() def create_sub_themes(self, count: int) -> None: """ Create SubThemes :param count: The number of SubThemes to create. """ if self.theme: theme_id = self.theme.id for num in range(0, count): sub_theme = SubTheme(theme_id, "_test_sub_theme_{}".format(num)) if sub_theme: try: sub_theme.save() sub_theme.commit() self.sub_themes.append(sub_theme) except Exception as exp: logger.error(exp) self.tearDown() def create_unit(self, number: int) -> Unit: """ Create unit/s :param number: a number to make the unit type unique """ unit = Unit.get_by_symbol("Unit_type_{}".format(number)) if unit: self.units.append(unit) return unit unit = Unit("Unit_type_{}".format(number), "Description_{}".format(number)) unit.save() unit.commit() self.units.append(unit) return unit def create_attributes(self, count: int) -> None: """ Create Attributes for SubThemes :param count: Number of Attributes per SubTheme """ number_attributes = len(self.sub_themes) * count try: index = 0 sub_theme = self.sub_themes[index] for num in range(0, number_attributes): unit = self.create_unit(num) attr = Attributes( "attribute_id_{}".format(num), "attribute_name_{}".format(num), "b3_heat_value_bffc4e56_20e2_41a5_84d8_de725a3f875b", sub_theme.id, unit.id, "_test_description_{}".format(num), "1") attr.save() attr.commit() self.attributes.append(attr) if num % count: index += 1 if index >= len(self.sub_themes): return sub_theme = self.sub_themes[index] except Exception as exp: logger.error(exp) self.fail() def create_attribute_alias(self) -> None: """ Create Attributes Aliases for Attributes :param count: The number of attributes to create per SubTheme """ try: for attr in self.attributes: alias = AttrAlias(attr.id, user_id=self.user.id, name="_alias_name {}".format( len(self.aliases)), table_name="_alias_table_name_{}".format( len(self.aliases))) alias.save() alias.commit() self.aliases.append(alias) except Exception as exp: logger.error(exp) self.tearDown() @staticmethod def create_admin_user() -> Users: """ Create an Admin user :return: an Admin user """ password_hash = bcrypt.hashpw("@p@22M0rd#@!".encode("utf-8"), bcrypt.gensalt()) user = Users.find_by_email("test_admin_user@no_an_email.cr") if not user: user = Users("test_admin_user", "test_admin_user@no_an_email.cr", password_hash.decode("utf8"), True, True) try: user.save() user.commit() except Exception as e: pass return user def get_auth_header(self) -> {str: str}: """ # Create an Authorization header :return: An Authorization header """ response_login = self.client.post('/login', data=dict(email=self.user.email, password="******", remember=True), follow_redirects=True) response_login_json = response_login.get_json() return { 'Authorization': 'Bearer {}'.format(response_login_json["access_token"]) } def test_get(self) -> None: """ Test Create Theme Tree endpoint """ resp = self.client.get('/admin/themes/get_tree', data=dict(user_id=self.user.id, theme_id=self.theme.id), headers=self.auth_header) data = resp.get_json() self.assertEqual(resp.status_code, HTTPStatus.OK) self.assertTrue('id' in data) self.assertTrue(data['id'] == self.theme.id) self.assertTrue(data['Name'] == self.theme.name) self.assertTrue('sub_themes' in data) self.assertTrue(len(data['sub_themes']) == len(self.sub_themes)) for sub in data['sub_themes']: self.assertTrue("attributes" in sub) for attr in sub["attributes"]: self.assertTrue("alias" in attr) def tearDown(self) -> None: """ Clean up all dependencies after tests""" for alias in self.aliases: try: if AttrAlias.get_by_user_id(self.user.id): alias.delete() alias.commit() except Exception: pass for attr in self.attributes: try: if Attributes.get_by_id(attr.id): attr.delete() attr.commit() except Exception: pass for sub_theme in self.sub_themes: try: if SubTheme.get_by_id(sub_theme.id): sub_theme.delete() sub_theme.commit() except Exception: pass for unit in self.units: try: if Unit.get_by_id(unit.id): unit.delete() unit.commit() except Exception: pass try: if Theme.get_by_id(self.theme.id): self.theme.delete() self.theme.commit() except Exception: pass self.client.post('/logout', headers=self.auth_header) if self.user: if Users.find_by_id(self.user.id): try: self.user.delete() self.user.commit() except Exception: pass self.app_context.pop()
def _get_theme_for_download(theme_id): theme = Theme.get(db.Key.from_path(Theme.kind(), int(theme_id))) theme.num_downloads += 1 theme.save() return theme