Ejemplo n.º 1
0
    def add_test_data_to_db():
        """Adds test data to the database, create_db must be called first.
        """
        ships = [
            {'name': 'Achilles', 'sh_class': 'Leander', 'type': 'Light Cruiser', 'country': 'NZL',
             'build_date': '1000-01-01'},
            {'name': 'Admiral Graf Spee', 'sh_class': 'Deutschland', 'type': 'Heavy Cruiser',
             'country': 'GER', 'build_date': '1000-01-01'},
            {'name': 'Cumberland', 'sh_class': 'County-Kent', 'type': 'Heavy Cruiser',
             'country': 'UK', 'build_date': '1000-01-01'},
            {'name': 'Exeter', 'sh_class': 'York', 'type': 'Heavy Cruiser', 'country': 'UK',
             'build_date': '1000-01-01'},
            {'name': 'Ajax', 'sh_class': 'Leander', 'type': 'Light Cruiser', 'country': 'UK',
             'build_date': '1000-01-01'}
        ]

        battles = [
            {'name': 'River Platte', 'conflict': 'WWII', 'date': '1939-12-13'},
            {'name': 'Battle of the Java Sea', 'conflict': 'WWII', 'date': '1942-02-27'}
        ]

        with models.database.atomic():
            Ship.insert_many(ships).execute()
            Battle.insert_many(battles).execute()

            river_platte = Battle.select().where(Battle.name == 'River Platte').get()
            java_sea = Battle.select().where(Battle.name == 'Battle of the Java Sea').get()

            for ship in Ship.select():
                if ship.name == 'Ajax':
                    ShipBattle.create(ship=ship, battle=java_sea)
                ShipBattle.create(ship=ship, battle=river_platte)
        models.database.close()
Ejemplo n.º 2
0
def post_comment(key):
	battle = Battle.get(key)
	now = datetime.now()
	if request.form.has_key('leftbox'):
		comment = request.form.get('leftbox')
		left_comment = Comment(parent=battle, comment=comment, author=users.get_current_user(), side="left", when=now)
		if request.files['left_image']:
			image_file = request.files['left_image']
			headers = image_file.headers['Content-Type']
			blob_key = parse_options_header(headers)[1]['blob-key']
			left_comment.blob_key = blob_key
			left_comment.image_url = images.get_serving_url(blob_key)
		left_comment.put()
		send_emails(key, comment)
	elif request.form.has_key('rightbox'):
		comment = request.form.get('rightbox')
		right_comment = Comment(parent=battle, comment=comment, author=users.get_current_user(), side="right", when=now)
		if request.files['right_image']:
			image_file = request.files['right_image']
			headers = image_file.headers['Content-Type']
			blob_key = parse_options_header(headers)[1]['blob-key']
			right_comment.blob_key = blob_key
			right_comment.image_url = images.get_serving_url(blob_key)
		right_comment.put()
		send_emails(key, comment)
	return left_right(key)
Ejemplo n.º 3
0
def add_a_battle():
	left = request.form.get('left')
	right = request.form.get('right')
	
	if check_existing_battle(left,right):
		return Response(status=400)
	battle = Battle(left=left,
					right=right,
					author = users.get_current_user())
	for tag in request.form.get('tags').split(','):
		if not tag.strip() == '' and not contains(battle,tag.strip()):
			battle.tags.append(tag.strip())
	if request.form.get('date') != '':
		battle.expirationDate = datetime.strptime(request.form.get('date'),"%m/%d/%Y")
	battle.put()
	return jsonify(key=str(battle.key()),left=left,right=right)
Ejemplo n.º 4
0
def send_emails(key,comment):
	battle = Battle.get(key)
	message = mail.EmailMessage(sender="*****@*****.**",
								subject="An update on Battle " + battle.left + " vs " + battle.right)
	for subscriber in battle.subscribers:
		message.to = subscriber
		message.body = comment + "\n by " + users.get_current_user().nickname()
		message.send()
Ejemplo n.º 5
0
def search_tag(tag):
	battle_with_tags = []
	battles = Battle.all()
	for battle in battles:
		for each_tag in battle.tags:
			if each_tag.lower() == tag.lower():
				battle_with_tags.append(battle)
	return render_template('all_battles.html',battles=battle_with_tags,title="Battles with tag " + tag,current_user=users.get_current_user().email()) 
Ejemplo n.º 6
0
def vote_battle(battle_key,up_or_down):
	vote = 1 if up_or_down == "up" else -1
	battle = Battle.get(battle_key)
	if check_for_user_vote_battle(battle, vote):
		battle.votes = battle.votes + vote
		battle.put()
	votes = battle.votes
	return jsonify(votes=votes)
Ejemplo n.º 7
0
def check_existing_battle(left,right,battle=None):
	existing_battles = Battle.all()
	for existing in existing_battles:
		if (existing.left.lower() == left.lower() and existing.right.lower() == right.lower()) or (existing.right.lower() == left.lower() and existing.left.lower() == right.lower()):
			if not battle:
				return True
			elif battle.key() != existing.key():
				return True
	return False
Ejemplo n.º 8
0
 def test_reset_db_message(self, create_db, clean_up):
     db_creator = DbCreatorActor.start(self._db_path)
     db_creator.tell(PopulateDbMessage(None).to_dict())
     db_creator.ask(ResetDbMessage(None).to_dict())
     assert len(Ship.select()) == 0
     assert len(Battle.select()) == 0
     assert len(ShipBattle.select()) == 0
     db_creator.stop()
     clean_up()
Ejemplo n.º 9
0
 def test_populate_db_message(self, create_db, clean_up):
     db_creator = DbCreatorActor.start(self._db_path)
     db_creator.ask(PopulateDbMessage(None).to_dict())
     assert self._db_path.exists()
     assert len(Ship.select()) == 5
     assert len(Battle.select()) == 2
     assert len(ShipBattle.select()) == 6
     db_creator.stop()
     clean_up()
Ejemplo n.º 10
0
def left_right(key):
	upload_url = blobstore.create_upload_url('/post_comment/'+key)
	battle = Battle.get(key)
	left_comments = Comment.all().ancestor(battle).order('-votes').filter('side =','left')
	right_comments = Comment.all().ancestor(battle).order('-votes').filter('side =','right')
	if battle.expirationDate and battle.expirationDate < datetime.now():
			return render_template('results.html',battle=battle,leftf=battle.left,rightf=battle.right,lc=left_comments,rc=right_comments)
	return render_template('battle.html',battle=battle,key=key,
		leftf=battle.left,rightf=battle.right,
		lc=left_comments,rc=right_comments,upload_url=upload_url,current_user=users.get_current_user().email())
Ejemplo n.º 11
0
def edit_battle(key):
	battle = Battle.get(key)
	if battle.author != users.get_current_user():
		return messages("You cannot edit this battle as you are not its owner")

	left = request.form.get('left')
	right = request.form.get('right')
	date = request.form.get('date')
	tags = ""
	
	if battle.tags:
		for t in battle.tags:
			tags = tags + t + ','
	
	if request.method == 'POST':
		if check_existing_battle(left,right,battle):
			return Response(status=400)
		if date != 'None' and date != '':
			battle.expirationDate = datetime.strptime(date,"%m/%d/%Y")
		battle.left = left
		battle.right = right
		form_tags = []
		for t in request.form.get('tags').split(','):
			form_tags.append(t.strip())
		battle.tags = []
		for tag in form_tags:
			if not tag == '':
				battle.tags.append(tag)
		battle.put()
		return Response(status=200)

	if battle.expirationDate:
		battle_date = battle.expirationDate.strftime("%m/%d/%Y")
	else:
		battle_date = 'None'
	return render_template('edit_battle.html',battle=battle,tags=tags,date=battle_date)
Ejemplo n.º 12
0
def all_battles():
	battles = Battle.all()
	return render_template('all_battles.html',battles=battles,title="Existing Battles",current_user=users.get_current_user().email())
Ejemplo n.º 13
0
def remove_battle(key):
	battle = Battle.get(key)
	if battle.author != users.get_current_user():
		return messages("You cannot delete this battle as you are not its owner")
	db.delete(battle)
	return redirect('/yourbattles')
Ejemplo n.º 14
0
def unsubscribe(key):
	battle = Battle.get(key)
	battle.subscribers.remove(str(users.get_current_user().email()))
	battle.put()
	return Response(status=200)
Ejemplo n.º 15
0
def index():
	count = Battle.all().count()
	recent_battles = Battle.all().order('-when').order('-votes').fetch(5)
	return render_template('new_index.html', count=count, recent_battles=recent_battles)
Ejemplo n.º 16
0
def yourbattles():
	battles = Battle.all().filter('author =',users.get_current_user())
	return render_template('all_battles.html',battles=battles,title="Your Battles",current_user=users.get_current_user().email())
Ejemplo n.º 17
0
def subscribe(key):
	battle = Battle.get(key)
	if not battle.subscribers.__contains__(users.get_current_user().email()):
		battle.subscribers.append(str(users.get_current_user().email()))
		battle.put()
	return Response(status=200)