예제 #1
0
def index():
	user = g.user
	#print(tr_session.get_session)
	
	# recuperer les torrents de l'utilisateur et de lui uniquement !
	torrents_from_db = Torrent.query.filter_by(user = unicode(g.user)).all()
	# this listing will be used to fetch torrents, and after, to check the update of torrents
	listing =list()
	for x in torrents_from_db:
		listing.append(x.hashstring)
	
	# envoi d'un nouveau torrent
	form = IndexForm()
	torrents = client.get_torrents(listing)
	for torrent in torrents:
		torrent_x=TorrentIndex()
		torrent_x.bandwidthpriority=torrent.bandwidthPriority
		torrent_x.status = torrent.status
		torrent_x.torrentname = torrent.name
		torrent_x.progress = float(torrent.progress)
		torrent_x.tor_id = torrent.hashString
		form.torrents.append_entry(torrent_x)
	
	torrent_to_start = False
	if form.validate_on_submit():
		for torrent_un in form.torrents:
			x = client.get_torrent(unicode(torrent_un.tor_id.data))
			# if the torrent is in the listing...
			if torrent_un.bandwidthpriority.data != int(x.bandwidthPriority) and x.hashString in listing:
				app.logger.info('%s priorité intiale %s, finale %s.',x.name,x.bandwidthPriority,torrent_un.bandwidthpriority.data)
				# we update...
				updatebandwidthpriority(x.hashString,torrent_un.bandwidthpriority.data)
				# and we remove the id from the listing -> if we don't do so, torrents bandwidth priority is updated twice for some obscure reason
				# thus staying at the first priority instead of being really updated to the user wish.
				listing.remove(torrent_un.tor_id.data)
		
		if form.torrentseed_file.data.mimetype == 'application/x-bittorrent':
			filename = secure_filename(form.torrentseed_file.data.filename)
			form.torrentseed_file.data.save(os.path.join(basedir + '/tmp', filename))
			f = open(basedir + '/tmp/' + filename)
			torrent_to_start = base64.b64encode(f.read())
		else:
			if form.torrentseed_url.data != '':
				torrent_to_start = form.torrentseed_url.data
		if torrent_to_start:
			# ON ajoute le torrent à transmission
			new_tor = client.add_torrent(torrent_to_start)
			new_tor.start()
			
			app.logger.info('%s demarré et ajouté à la base de données par %s.',new_tor,user)
			
			# on ajoute le torrent à la base de données pour se souvenir à qui il appartient.
			torrent_to_add = Torrent(hashstring=new_tor.hashString,user=unicode(g.user))
			db.session.add(torrent_to_add)
			db.session.commit()
		#except tr.TransmissionError:
		#	app.logger.info(tr.TransmissionError)
		return redirect(redirect_url())
		
	return render_template("index.html", form = form, title = "Home", user = g.user)
예제 #2
0
def admin():
	user = g.user
	if user.is_admin():
		torrents = client.get_torrents()
		torrents_forms = IndexForm()
		for torrent in torrents:
			form = TorrentIndex()
			form.tor_id = torrent.hashString
			form.torrentname = torrent.name
			form.status = torrent.status
			
			t = Torrent.query.filter_by(hashstring = torrent.hashString).first()
			if t!=None and t.user!=None:
				form.owner = t.user
			
			form.bandwidthpriority=torrent.bandwidthPriority
			torrents_forms.torrents.append_entry(form)

	return render_template("admin.html", title = "Admin", user = g.user, torrents = torrents, torrents_forms = torrents_forms)