def add_tags(tag): if tag is None: return existing_tag = Tag.query.filter(Tag.name == tag.lower()).one_or_none() """if it does return existing tag objec to list""" if existing_tag is not None: return existing_tag else: new_tag = Tag() new_tag.name = tag.lower().strip() return new_tag
def fake_tag(count=20): for i in range(count): tag = Tag(name=fake.word()) db.session.add(tag) try: db.session.commit() except IntegrityError: db.session.rollback()
def test_delete_tag(self): photo = Photo.query.get(2) tag = Tag(name='test') photo.tags.append(tag) db.session.commit() self.login() response = self.client.post(url_for('main.delete_tag', photo_id=2, tag_id=2), follow_redirects=True) data = response.get_data(as_text=True) self.assertIn('Tag deleted.', data) self.assertEqual(photo.tags, []) self.assertIsNone(Tag.query.get(2))
def new_tag(photo_id): photo = Photo.query.get_or_404(photo_id) if current_user != photo.author and not current_user.can('MODERATE'): abort(403) form = TagForm() if form.validate_on_submit(): for name in form.tag.data.split(): tag = Tag.query.filter_by(name=name).first() if tag is None: tag = Tag(name=name) db.session.add(tag) db.session.commit() if tag not in photo.tags: photo.tags.append(tag) db.session.commit() flash('Tag added.', 'success') flash_errors(form) return redirect(url_for('.show_photo', photo_id=photo_id))
def create_tag(): payload = request.json # validate payload assert payload is not None, 'missing json body' ValidationError.raise_assert('tag' in payload, 'tag required') tag_name = payload['tag'] # check duplicates duplicates = db.session.query(Tag).filter_by(tag=tag_name).first() ValidationError.raise_assert(duplicates is None, 'tag "{}" already exists'.format(tag_name)) tag = Tag(tag=tag_name) db.session.add(tag) db.session.commit() logger.debug('created tag "{}"'.format(tag.tag)) return jsonify({'tag': {'tag_id': tag.id, 'tag': tag.tag}}), 201
def mutate(self, info, name, description="", job="", note="", part=""): user = info.context.user if user.is_anonymous: return CreateTag(ok=False, status="Must be logged in.") else: new_tag = Tag(name=name, description=description, user=user) if job != "": # if job is not empty string, use from_global_id method # pass from_global_id the value provided in argument # from_global_id() returns the [type, ID] # search Job model for instance where primary key is equal to ID returned # from the from_global_id() method # assign that instance to new_note.job before saving new_tag.job = Job.objects.get(pk=from_global_id(job)[1]) if note != "": new_tag.note = Note.objects.get(pk=from_global_id(note)[1]) if part != "": new_tag.part = Part.objects.get(pk=from_global_id(part)[1]) new_tag.save() return CreateTag(tag=new_tag, ok=True, status="ok")
def create_post(): payload = request.json logger.debug('validating request body') # validate payload assert payload is not None, 'missing json body' for required_field in ['title', 'body', 'collaborators', 'explicit_tags']: ValidationError.raise_assert( bool=required_field in payload, msg='"{}" required'.format(required_field)) logger.debug('create Post object') # init Post object post = Post(title=payload['title'], body=encode_body(payload['body'])) logger.debug('process tags') # get implicit tags explicit_tag_names = map(clean_whitespace, payload['explicit_tags']) explicit_tag_names = map(lambda x: (x, True), explicit_tag_names) # compute implicit tags from title implicit_tag_names = map(clean_whitespace, title_tokenizer(post.title)) implicit_tag_names = map(lambda x: (x, False), implicit_tag_names) known_tags = set() logger.debug('get/create tag objects') for (tag_name, is_explicit) in chain(explicit_tag_names, implicit_tag_names): tag_key = tag_name.strip().lower() if tag_key in known_tags: continue known_tags.add(tag_key) tag = db.session.query(Tag).filter( db.func.lower(Tag.tag) == db.func.lower(tag_name)).first() # allow on-the-fly tag creation if tag is None: tag = Tag(tag=tag_name, has_explicit=is_explicit) db.session.add(tag) else: if is_explicit and tag.has_explicit == False: tag.has_explicit = True db.session.add(tag) post_tag = PostTag(is_explicit=is_explicit) post_tag.tag = tag post.post_tags.append(post_tag) # add collaborators # TODO: allow mixed types (users & teams) logger.debug('get collaborators') for user_id in payload['collaborators']: user = db.session.query(User).filter_by(id=user_id).first() QueryError.raise_assert(user is not None, 'user "{}" not found'.format(user_id)) post.collaborators.append(user) if g.current_user not in post.collaborators: post.collaborators.append(g.current_user) logger.debug('persist Post object to db') db.session.add(post) db.session.commit() logger.debug('created Post(id={})'.format(post.id)) output = { 'post_id': post.id, 'title': post.title, 'body': decode_body(post.body), 'created_date': post.created_date, 'modified_date': post.modified_date, 'explicit_tags': [], 'implicit_tags': [], 'collaborators': [user.id for user in post.collaborators], 'upvotes': [user.id for user in post.upvotes], } for post_tag in post.post_tags: if post_tag.is_explicit: output['explicit_tags'].append(post_tag.tag.tag) else: output['implicit_tags'].append(post_tag.tag.tag) return jsonify({'post': output}), 201
def inject_test_data(): if not Project.query.all(): # projects project1 = Project( published=True, demo=True, title="Online Picure Converter (React)", description=""" <h1>Project Description</h1> <p> We need a developer to create an online picture converter that will be able to convert images into different formats. </p> <p> This project needs to be built with React and needs to be a single page application (SPA). </p> """, link="https://letsconvert.app", created_at=get_unix(), modified_at=get_unix(), ) project2 = Project( published=True, demo=True, title="Babysitting Booking Platform", description=""" <h1>Project Description</h1> <p> We need a developer to migrate our existing buggy wordpress website to a new framework that is faster, more efficient, and can handle our increasing sales. </p> <p> Our company is an on-demand babysitter booking service that needs to allow users to book with real-time availabilities and babysitter schedules. </p> """, link="https://kiid.ca/en", created_at=get_unix() + 1, modified_at=get_unix() + 1, ) project3 = Project( published=True, demo=True, title="Persian Carpets Store CMS", description=""" <h1>Project Description</h1> <p> We need a developer to build a custom CMS system that integrates with our existing protocols and ways of doing things. </p> <p> Our existing website is built on a framwork from 1999 and is no longer performing adequately. We need something newer, better that can integrate with our existing inventory and procedures. </p> """, link="https://bashircarpets.com", created_at=get_unix() + 2, modified_at=get_unix() + 2, ) project4 = Project( published=True, demo=True, title="Online 3D Art Gallery", description=""" <h1>Project Description</h1> <p> We need a developer to build a 3D art gallery that will allow artists to upload their artworks and buyers to purchase them / view them in 3D. </p> <p> We also need a custom CMS solution to manage these artists and artworks, as well as a way to post events and have users be able to RSVP to the events they want to participate to. </p> """, restrictions="Europe Only", link="https://st-artamsterdam.com", created_at=get_unix() + 3, modified_at=get_unix() + 3, ) db.session.add(project1) db.session.add(project2) db.session.add(project3) db.session.add(project4) # company company1 = Company( name="Let's Convert", location="International", email="*****@*****.**", logo="/assets/img/companies/logo-placeholder.png", created_at=get_unix(), modified_at=get_unix(), ) company2 = Company( name="Kiid Babysitting", location="Montreal, Canada", email="*****@*****.**", logo="/assets/img/companies/logo-placeholder.png", created_at=get_unix() + 1, modified_at=get_unix() + 1, ) company3 = Company( name="Bashir Persian Rugs", location="Montreal, Canada", email="*****@*****.**", logo="/assets/img/companies/logo-placeholder.png", created_at=get_unix() + 2, modified_at=get_unix() + 2, ) company4 = Company( name="St-Art Amsterdam", location="Amsterdam, Netherlands", email="*****@*****.**", logo="/assets/img/companies/logo-placeholder.png", created_at=get_unix() + 3, modified_at=get_unix() + 3, ) db.session.add(company1) db.session.add(company2) db.session.add(company3) db.session.add(company4) company1.projects.append(project1) company2.projects.append(project2) company3.projects.append(project3) company4.projects.append(project4) # tags tag1 = Tag( name="react", created_at=get_unix(), modified_at=get_unix(), ) tag1_1 = Tag( name="reactjs", created_at=get_unix(), modified_at=get_unix(), ) tag1_2 = Tag( name="react-javascript", created_at=get_unix(), modified_at=get_unix(), ) tag2 = Tag( name="python", created_at=get_unix(), modified_at=get_unix(), ) tag3 = Tag( name="go", created_at=get_unix(), modified_at=get_unix(), ) db.session.add(tag1) db.session.add(tag1_1) db.session.add(tag1_2) db.session.add(tag2) db.session.add(tag3) project1.tags.append(tag1) project2.tags.append(tag1) project3.tags.append(tag1) project4.tags.append(tag1) project1.tags.append(tag2) project2.tags.append(tag2) project3.tags.append(tag2) project4.tags.append(tag2) db.session.commit() return
def project_preview(): # company variables try: name = request.form['company_name'] location = request.form['company_location'] email = request.form['company_email'] except KeyError as e: print(e) raise RequestError(general_error, 400) # checks for existing company company = Company.query.filter_by(email=email.lower()).first() # creates a company if not company: company = Company( name=name, location=location, email=email.lower(), logo="test-logo", created_at=get_unix(), modified_at=get_unix(), ) db.session.add(company) # project variables try: title = request.form['title'] description = request.form['description'] restrictions = request.form['restrictions'] link = request.form['link'] except KeyError as e: print(e) raise RequestError(general_error, 400) # checks for existing project if request.form.get('project_id'): project_id = request.form.get('project_id') project = Project.query.filter_by(id=project_id).first() project.title = title project.description = description project.restrictions = restrictions project.link = link # removes tags as they will be re-added later project.tags = [] else: # creates a project project = Project( published=False, demo=False, title=title, description=description, restrictions=restrictions, link=link, created_at=get_unix(), modified_at=get_unix(), ) db.session.add(project) # adds project to company company.projects.append(project) # tags variable try: tags = request.form['tags'] except KeyError as e: print(e) raise RequestError(general_error, 400) # converts to list tags = tags.split(',') # creates tags if tags: for t in tags: # ignores [''] if t: # looks for existing tag tag = Tag.query.filter_by(name=t.lower()).first() if not tag: tag = Tag( name=t.lower(), created_at=get_unix(), modified_at=get_unix(), ) db.session.add(tag) # adds tag to project project.tags.append(tag) # commits db.session.commit() return json.dumps([get_project(project)])