def test_degree_range(self): """Make sure all the ra and decs in the db are in degree range.""" ra_outofrange_count = Star.query.filter(db.not_(db.between(Star.ra, 0, 360))).count() dec_outofrange_count = Star.query.filter(db.not_(db.between(Star.dec, -90, 90))).count() self.assertEqual(ra_outofrange_count, 0) self.assertEqual(dec_outofrange_count, 0)
def insert_post(): """ Inserts post into databases. """ # File upload section file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) pdf_url = '{}{}'.format(UPLOAD_URL_PREFIX, filename) else: pdf_url = None # Post creation section title = request.form.get('title-input') cat_id = request.form.get('category') text = request.form.get('post-content') date = datetime.now() # Make post ID the datetime to ensure uniqueness post_id = '{:0>4}{:0>2}{:0>2}{:0>2}{:0>2}{:0>2}'.format(date.year, date.month, date.day, date.hour, date.minute, date.second) emp_id = session['emp_id'] post = Post(post_id=post_id, title=title, cat_id=cat_id, date=date, text=text, emp_id=emp_id, pdf_url=pdf_url) db.session.add(post) # Determine who sees this post. This is a list of stores. audience = request.form.getlist('audience') # Add each employee from audience to assigned_post table: for store in audience: # Sales associates (03-SS) are excluded from seeing posts unless # it is assigned to them. employees = Employee.query.filter(Employee.store_id == store, db.not_(Employee.pos_id.in_(['03-SS']))).all() # consolidate this, refactor to make more flexible for employee in employees: emp_assigned_post = AssignedPost(post_id=post_id, emp_id=employee.emp_id, was_read=False) db.session.add(emp_assigned_post) # Task section: if request.form.get('has_task'): for store in audience: desc = request.form.get('task_item') deadline = request.form.get('deadline') task = Task(post_id=post_id, store_id=store, desc=desc, assigned_date=date, deadline=deadline, is_complete=False) db.session.add(task) db.session.commit() flash("Post created.") return redirect('/')
def get_random_district(): """ Helper function to generate a random district. Will be assigned to random store in make_store function. """ # Need to exclude Corporate all_districts = District.query.filter(db.not_(District.district_id.in_(['D99']))).all() random_district = choice(all_districts).district_id return random_district
def queue(): """Opens the queue and retrieves items for review. Assures reviewer doesn't review anything they've reviewed previously, that a given item is reviewed 3 times tops, and that images are not surfaced in comments queue and vice versa. """ #Create a list of items reviewed by this reviewer previously - these are not fair game item_id_list = [ a.item_id for a in Action.query.filter( Action.reviewer_id == session["reviewer id"]) ] #Create a list of items reviewed less than four times- these are fair game less_than_4 = [] sql = """ select item_id from (select item_id, count(item_id) as count from actions group by 1 having count(item_id) = 3) as a;""" cursor = db.session.execute(sql) s = cursor.fetchall() for i in range(len(s)): numb = int(s[i][0]) less_than_4.append(numb) #Once you're in the queue, display the correct content for the route the reviewer came from if "pickermode" in session: comments = Item.query.filter( func.lower(Item.subreddit) == session["pickermode"].lower(), db.not_(Item.item_id.in_(item_id_list)), db.not_(Item.item_id.in_(less_than_4))).limit(5).all() elif "imagemode" in session: comments = Item.query.filter( Item.parent == "image", db.not_(Item.item_id.in_(item_id_list)), db.not_(Item.item_id.in_(less_than_4))).limit(5).all() else: comments = Item.query.filter( Item.parent == None, db.not_(Item.item_id.in_(item_id_list)), db.not_(Item.item_id.in_(less_than_4))).limit(5).all() #Selecting badwords from db, limiting due to regex throwing errors with non latin characters badwords_list = [ w.word for w in BadWord.query.filter(BadWord.language == 'en') ] matches = {} #Make a string of badwords separated by pipes adding \b around to indicate whole word checking badwords_pattern = r'\b(' + '|'.join(badwords_list) + r')\b' list_of_comment_bodies = [word.body.lower() for word in comments] #Go through all the badwords and look for them in the comment bodies for item in comments: res = re.findall(badwords_pattern, item.body.lower(), re.IGNORECASE) if len(res) > 0: matches[item.link_id] = ', '.join(res) return render_template("queue.html", comments=comments, batchsize=len(comments), matches=matches)
def show_profile(user_id): """ Renders user profile page """ user = User.query.get(user_id) if user: fav_series_id = [ s.series_id for s in Fav_Series.query.filter_by(user_id=user_id).all() ] series = Series.query.filter( db.not_(Series.series_id.in_(fav_series_id))).all() return render_template("user_info.html", user=user, series=series) else: flash("User does not exist", "danger") if "user_id" in session: return redirect("/user/{}".format(session["user_id"])) else: return redirect("/")
def work_inprogress(sender_id): """Pull all projects that are not complete""" complete = db.session.query(Project.project_id).join(Proj_Stat).filter(Project.user_id == sender_id, Proj_Stat.status_id == 6).all() if not complete: inprogress = db.session.query(Project.name, Project.project_id, Project.due_at).filter(Project.user_id == sender_id).all() else: inprogress = db.session.query(Project.name, Project.project_id, Project.due_at).filter(db.not_(Project.project_id.in_(complete)) & (Project.user_id == sender_id)).all() return inprogress
def q7(): """Return all humans whose email addresses do NOT contain 'gmail'.""" return Human.query.filter(db.not_(Human.email.like('%gmail%'))).all()
# Get all animals born in a year greater than (but not equal to) 2015. query_4 = Animal.query.filter(Animal.birth_year > 2015).all() # Get all the humans with first names that start with "J" query_5 = Human.query.filter(Human.fname.like('J%')).all() # Get all the animals who don't have a birth year query_6 = Animal.query.filter(Animal.birth_year == None).all() # Get all the animals with species "fish" OR "rabbit" query_7 = Animal.query.filter((Animal.animal_species == "fish") | (Animal.animal_species == "rabbit")).all() # Get all the humans who don't have an email address that # contains "gmail" query_8 = Human.query.filter(db.not_((Human.email.like('%gmail%')))).all() # # Continue reading the instructions before you move on! # def print_humans_and_animals(): """Print a directory of humans and their animals""" humans = db.session.query(Human.fname, Human.lname, Animal.name, Animal.animal_species).join(Animal).all() for human in (Human.query.options(db.joinedload("animals")).order_by( Human.fname).all()): #create a dictionary here that will append new pet on key of human