
def frange(start, end, step):
	tmp = start
	while(tmp <= end):
		yield tmp
		tmp += step

from cineapp import db
from cineapp.models import Movie, Mark
from sqlalchemy.sql.expression import literal, desc

def frange(start, end, step):
	tmp = start
	while(tmp <= end):
		yield tmp
		tmp += step

def get_activity_list(start, length):

	"""
		Returns an array containing activity records ordered by descending date
		Params are a range of records we want to have in the returned array
	"""

	# Object_items
	object_dict={"count": 0, "list": []}
	object_list=[]
	
	# Movie Query
	movies_query=db.session.query(Movie.id,literal("user_id").label("user_id"),Movie.added_when.label("entry_date"),literal("movies").label("entry_type"))

	# Marks Query
	marks_query=db.session.query(Mark.movie_id,Mark.user_id.label("user_id"),Mark.updated_when.label("entry_date"),literal("marks").label("entry_type")).filter(Mark.mark != None)

	# Homework Query
	homework_query=db.session.query(Mark.movie_id,Mark.user_id.label("user_id"),Mark.homework_when.label("entry_date"),literal("homeworks").label("entry_type")).filter(Mark.homework_when != None)

	# Build the union request
	activity_list = movies_query.union(marks_query,homework_query).order_by(desc("entry_date")).slice(int(start),int(start) + int(length))
	
	for cur_item in activity_list:
		if cur_item.entry_type == "movies":
			object_list.append({"entry_type": "movies", "object" : Movie.query.get(cur_item.id)})
		elif cur_item.entry_type == "marks":
			object_list.append({"entry_type": "marks", "object" : Mark.query.get((cur_item.user_id,cur_item.id))})
		elif cur_item.entry_type == "homeworks":
			object_list.append({"entry_type" : "homeworks", "object" : Mark.query.get((cur_item.user_id,cur_item.id))}) 

	# Count activity number (Will be used for the datatable pagination)
	object_dict["count"]=movies_query.union(marks_query,homework_query).order_by(desc("entry_date")).count()
	object_dict["list"]=object_list

	# Return the filled object
	return object_dict

Move activity SQL query into a dedicated function
Move activity SQL query into a dedicated function

The SQL query based on the UNION predicate is now into a function which takes as
parameters the number of record we want to have in the result. This function
will be used for the activity dashboard and also for the new global activity
page using the datatable plugin.

The subqueries have been also updated in order to exclude some records which
shouldn't be fetched :
  - For the homework don't fetch records without an homework date
  - For the mark, don't fetch record without a mark

+ from cineapp import db
+ from cineapp.models import Movie, Mark
+ from sqlalchemy.sql.expression import literal, desc
  
  def frange(start, end, step):
  	tmp = start
  	while(tmp <= end):
  		yield tmp
  		tmp += step
  
+ def get_activity_list(start, length):
+ 
+ 	"""
+ 		Returns an array containing activity records ordered by descending date
+ 		Params are a range of records we want to have in the returned array
+ 	"""
+ 
+ 	# Object_items
+ 	object_dict={"count": 0, "list": []}
+ 	object_list=[]
+ 	
+ 	# Movie Query
+ 	movies_query=db.session.query(Movie.id,literal("user_id").label("user_id"),Movie.added_when.label("entry_date"),literal("movies").label("entry_type"))
+ 
+ 	# Marks Query
+ 	marks_query=db.session.query(Mark.movie_id,Mark.user_id.label("user_id"),Mark.updated_when.label("entry_date"),literal("marks").label("entry_type")).filter(Mark.mark != None)
+ 
+ 	# Homework Query
+ 	homework_query=db.session.query(Mark.movie_id,Mark.user_id.label("user_id"),Mark.homework_when.label("entry_date"),literal("homeworks").label("entry_type")).filter(Mark.homework_when != None)
+ 
+ 	# Build the union request
+ 	activity_list = movies_query.union(marks_query,homework_query).order_by(desc("entry_date")).slice(int(start),int(start) + int(length))
+ 	
+ 	for cur_item in activity_list:
+ 		if cur_item.entry_type == "movies":
+ 			object_list.append({"entry_type": "movies", "object" : Movie.query.get(cur_item.id)})
+ 		elif cur_item.entry_type == "marks":
+ 			object_list.append({"entry_type": "marks", "object" : Mark.query.get((cur_item.user_id,cur_item.id))})
+ 		elif cur_item.entry_type == "homeworks":
+ 			object_list.append({"entry_type" : "homeworks", "object" : Mark.query.get((cur_item.user_id,cur_item.id))}) 
+ 
+ 	# Count activity number (Will be used for the datatable pagination)
+ 	object_dict["count"]=movies_query.union(marks_query,homework_query).order_by(desc("entry_date")).count()
+ 	object_dict["list"]=object_list
+ 
+ 	# Return the filled object
+ 	return object_dict
+ 
Move activity SQL query into a dedicated function
## Code Before:

def frange(start, end, step):
	tmp = start
	while(tmp <= end):
		yield tmp
		tmp += step

## Instruction:
Move activity SQL query into a dedicated function
## Code After:
from cineapp import db
from cineapp.models import Movie, Mark
from sqlalchemy.sql.expression import literal, desc

def frange(start, end, step):
	tmp = start
	while(tmp <= end):
		yield tmp
		tmp += step

def get_activity_list(start, length):

	"""
		Returns an array containing activity records ordered by descending date
		Params are a range of records we want to have in the returned array
	"""

	# Object_items
	object_dict={"count": 0, "list": []}
	object_list=[]
	
	# Movie Query
	movies_query=db.session.query(Movie.id,literal("user_id").label("user_id"),Movie.added_when.label("entry_date"),literal("movies").label("entry_type"))

	# Marks Query
	marks_query=db.session.query(Mark.movie_id,Mark.user_id.label("user_id"),Mark.updated_when.label("entry_date"),literal("marks").label("entry_type")).filter(Mark.mark != None)

	# Homework Query
	homework_query=db.session.query(Mark.movie_id,Mark.user_id.label("user_id"),Mark.homework_when.label("entry_date"),literal("homeworks").label("entry_type")).filter(Mark.homework_when != None)

	# Build the union request
	activity_list = movies_query.union(marks_query,homework_query).order_by(desc("entry_date")).slice(int(start),int(start) + int(length))
	
	for cur_item in activity_list:
		if cur_item.entry_type == "movies":
			object_list.append({"entry_type": "movies", "object" : Movie.query.get(cur_item.id)})
		elif cur_item.entry_type == "marks":
			object_list.append({"entry_type": "marks", "object" : Mark.query.get((cur_item.user_id,cur_item.id))})
		elif cur_item.entry_type == "homeworks":
			object_list.append({"entry_type" : "homeworks", "object" : Mark.query.get((cur_item.user_id,cur_item.id))}) 

	# Count activity number (Will be used for the datatable pagination)
	object_dict["count"]=movies_query.union(marks_query,homework_query).order_by(desc("entry_date")).count()
	object_dict["list"]=object_list

	# Return the filled object
	return object_dict
