def opts_design(request): # Design du site if request.method == 'POST': form = DesignForm(request.POST) if form.is_valid(): prof = request.user.get_profile() if form.cleaned_data['remote']: durl = form.cleaned_data['rurl'] else: design = get_object_or_404(Style, name=form.cleaned_data['design']) durl = design.url prof.style = durl prof.save() # Vider le cache de session request.session['style'] = durl messages.add_message(request, messages.INFO, _('Design changé')) return HttpResponseRedirect('user-5.html') else: form = DesignForm({ 'rurl': request.user.get_profile().style, 'remote': True }) return tpl('users/opts_design.html', {'form': form}, request)
def opts_profile(request): # Changer le profile prof = request.user.get_profile() if request.method == 'POST': form = ProfileForm(request.POST) if form.is_valid(): prof.website = form.cleaned_data['website'] prof.quote = form.cleaned_data['quote'] prof.pperso = request.POST['body'] prof.avatar = form.cleaned_data['avatar'] prof.sign = form.cleaned_data['sign'] prof.save() messages.add_message(request, messages.INFO, _('Profil modifié avec succès')) return HttpResponseRedirect('user-5.html') else: form = ProfileForm({ 'website': prof.website, 'quote': prof.quote, 'avatar': prof.avatar, 'sign': prof.sign }) return tpl('users/opts_profile.html', { 'form': form, 'body': prof.pperso }, request)
def viewfiles(request, package_id): # Afficher les fichiers d'un paquet package_id = int(package_id) # Récupérer le paquet package = get_object_or_404(Package, pk=package_id) # Nom du fichier qui contient la liste des fichiers contenus dans l'archive filename = settings.LOCAL_MIRROR + package.download_url + '.files' # Lire le fichier, et retirer tout ce qui ne doit pas être affiché f = open(filename) files = [] for line in f: if line.startswith('__LOGRAM'): continue files.append(line) # Rendre la template return tpl( 'packages/files.html', { 'files': files, 'pkg': package.name + '-' + package.version + ' (' + package.arch.name + ')' }, request)
def opts_pseudo(request): # Pseudo et adresse e-mail if request.method == 'POST': form = PseudoForm(request.POST) if form.is_valid(): usr = request.user prof = request.user.get_profile() usr.username = form.cleaned_data['pseudo'] usr.email = form.cleaned_data['email'] prof.show_email = form.cleaned_data['show_email'] prof.uname = usr.username prof.timezone = form.cleaned_data['timezone'] usr.save() prof.save() messages.add_message( request, messages.INFO, _('Pseudonyme et e-mail modifiés avec succès')) return HttpResponseRedirect('user-5.html') else: form = PseudoForm({ 'pseudo': request.user.username, 'email': request.user.email, 'show_email': request.user.get_profile().show_email, 'timezone': request.user.get_profile().timezone }) return tpl('users/opts_pseudo.html', {'form': form}, request)
def home(request): # Affichage des paquets des distributions et recherche de paquets # Récupérer la liste des distributions distros = Distribution.objects.all() # Rendre la template (c'est elle qui fait tout) return tpl('packages/home.html', {'distros': distros}, request)
def staff(request): # Même que list, mais seulement les membres du staff members = Profile.objects.select_related('user').exclude( main_group=1).order_by('main_group') return tpl('users/list.html', { 'profiles': members, 'is_staff': True }, request)
def search(request, page): # Recherche q, method, distro # Construire le début de la requete packages = Package.objects \ .select_related('arch', 'distribution') \ .order_by('-distribution', 'name') # Récupérer les variables q = request.GET['q'] method = request.GET['method'] distro = request.GET['distro'] # Filtrer pour la distro (dans ce cas le order_by est ignoré) di = None if distro != 'all': packages = packages.filter(distribution=int(distro)) di = get_object_or_404(Distribution, pk=distro) # Filtrer suivant le critère de recherche if method == 'match': packages = packages.filter(name__exact=q) elif method == 'contains': packages = packages.filter(name__contains=q) elif method == 'starts': packages = packages.filter(name__startswith=q) elif method == 'ends': packages = packages.filter(name__endswith=q) else: raise Http404 # Paginer paginator = Paginator(packages, 20) try: page = int(page) except ValueError: page = 1 try: packages = paginator.page(page).object_list except (EmptyPage, InvalidPage): packages = paginator.page(paginator.num_pages).object_list # Rendre la template return tpl( 'packages/list.html', { 'packages': packages, 'list_pages': get_list_page(page, paginator.num_pages, 4), 'q': q, 'method': method, 'udistro': distro, 'distro': di }, request)
def index(request): # Page d'accueil des téléchargements, doit être claire # Récupérer la liste des derniers paquets packages = Package.objects \ .select_related('distribution', 'arch') \ .order_by('-date')[:10] # Récupérer la liste des téléchargements dws = DwVariant.objects \ .select_related('download', 'download__cat') \ .order_by('download__cat__weight', 'download__weight') # Créer l'arbre des téléchargements, les templates ne sachant pas le faire downloads = {} for dw in dws: # Ajouter la catégorie if dw.download.cat.id in downloads: cat = downloads[dw.download.cat.id] else: cat = { 'name': dw.download.cat.name, 'description': dw.download.cat.description, 'downloads': {} } downloads[dw.download.cat.id] = cat dwls = cat['downloads'] # Ajouter le téléchargement if dw.download.id in dwls: dwl = dwls[dw.download.id] else: dwl = { 'name': dw.download.name, 'description': dw.download.description, 'screen': dw.download.screen, 'thb': dw.download.thb, 'variants': {} } dwls[dw.download.id] = dwl variants = dwl['variants'] # Ajouter la variante variants[dw.name] = dw # Afficher la template return tpl('packages/index.html', { 'packages': packages, 'downloads': downloads }, request)
def sections(request, distro_id): # Affichage des sections d'une distribution distro_id = int(distro_id) # Récupérer la distribution distro = get_object_or_404(Distribution, pk=distro_id) # Prendre la liste des sections sections = Section.objects.order_by('name') # Rendre la template return tpl('packages/sections.html', { 'distro': distro, 'sections': sections }, request)
def viewmirrors(request, package_id): # Afficher les mirroirs et proposer le téléchargement du paquet package_id = int(package_id) # Récupérer le paquet package = get_object_or_404(Package, pk=package_id) # Récupérer la liste des mirroirs mirrors = Mirror.objects.order_by('place') # Rendre la template return tpl('packages/mirrors.html', { 'package': package, 'mirrors': mirrors }, request)
def online(request, page): # Afficher les utilisateurs en ligne page = int(page) # Récupérer les informations du cache des connectés activities = Activity.objects \ .select_related('user') \ .order_by('-date') # Compter anon = 0 users = 0 connected_users = [] for act in activities: if act.user_id: users += 1 else: anon += 1 if act.template in actnames: act.activity = actnames[act.template] else: act.activity = _('Page privée') # Paginer paginator = Paginator(activities, 30) # 30 utilisateurs par page try: pg = paginator.page(page) except (EmptyPage, InvalidPage): pg = paginator.page(paginator.num_pages) # Rendre la template return tpl( 'users/list.html', { 'list_pages': get_list_page(page, paginator.num_pages, 4), 'profiles': pg.object_list, 'is_connected': True, 'anons': anon, 'pindex': 4, 'regs': users }, request)
def viewdir(request, dir_id): # Affichage du contenu d'un dossier de l'utilisateur. # Si dir_id = 0, alors c'est son dossier principal # Affiche aussi un formulaire d'upload si le quota du dossier n'est pas dépassé dir_id = int(dir_id) if dir_id == 0: mdir = request.user.get_profile().main_dir else: mdir = get_object_or_404(Directory, pk=dir_id) #Vérifier que c'est bien le dossier de l'utilisateur if mdir.user != request.user: raise Http404 # Vérifier le quota restant available = mdir.quota - mdir.used render_form = False form = 0 if (available != 0 or request.user.has_perm('upload.ignore_quota')) and dir_id != 0: # Créer le formulaire d'envoi form = UploadForm() render_form = True # Obtenir la liste des fichiers et dossiers files = File.objects.filter(directory=mdir) dirs = Directory.objects.filter(parent=mdir) # Rendre la template return tpl( 'upload/view.html', { 'dir': mdir, 'dir_id': dir_id, 'available': available, 'form': form, 'render_form': render_form, 'files': files, 'exts': EXTS, 'dirs': dirs }, request)
def list(request, page): # Lister les membres, par pseudo page = int(page) members = Profile.objects.select_related('user').order_by( '-main_group', 'user__username') paginator = Paginator(members, 30) # 30 utilisateurs par page try: pg = paginator.page(page) except (EmptyPage, InvalidPage): pg = paginator.page(paginator.num_pages) return tpl( 'users/list.html', { 'list_pages': get_list_page(page, paginator.num_pages, 4), 'profiles': pg.object_list, 'pindex': 2, 'is_staff': False }, request)
def changelog(request, package_id, page): # Afficher les changements d'un paquet package_id = int(package_id) # Récupérer le paquet package = get_object_or_404(Package, pk=package_id) # Récupérer les éléments de changelog de ce paquet entries = ChangeLog.objects \ .filter(package=package_id) \ .select_related('distribution') \ .order_by('-date') # Pour chaque entrée, récupérer la chaîne traduite strings = String.objects.filter(package=package) for entry in entries: entry.content = str_of_package(package, request.LANGUAGE_CODE.split('-')[0], 3, strings, entry).content # Paginer paginator = Paginator(entries, 20) try: page = int(page) except ValueError: page = 1 try: entries = paginator.page(page).object_list except (EmptyPage, InvalidPage): entries = paginator.page(paginator.num_pages).object_list # Afficher dans la template return tpl( 'packages/changelog.html', { 'package': package, 'list_pages': get_list_page(page, paginator.num_pages, 4), 'entries': entries }, request)
def opts_mdp(request): # Mot de passe if request.method == 'POST': form = PassForm(request.POST) if form.is_valid(): usr = request.user # On vérifie que l'ancien mot de passe est connu usr.set_password(form.cleaned_data['password']) usr.save() messages.add_message(request, messages.INFO, _('Mot de passe changé avec succès')) return HttpResponseRedirect('user-5.html') else: form = PassForm() return tpl('users/opts_pass.html', {'form': form}, request)
def view(request, user_id): # Afficher les informations d'un utilisateur try: usr = Profile.objects.select_related('user').get(pk=user_id) except Profile.DoesNotExist: raise Http404 try: paste = Pastebin.objects.filter( author=user_id).order_by('-created')[:1] except Pastebin.DoesNotExist: raise Http404 if paste: ok = True temp = {'profile': usr, 'ok': ok, 'paste': paste[0]} else: ok = False temp = {'profile': usr, 'ok': ok} # Rendre la template return tpl('users/show.html', temp, request)
def listsection(request, distro_id, section_id, page): # Affichage des paquets d'une section distro_id = int(distro_id) section_id = int(section_id) # Récupérer la distribution distro = get_object_or_404(Distribution, pk=distro_id) # Récupérer la section section = get_object_or_404(Section, pk=section_id) # Récupérer les versions (et leurs paquets) de la distribution dans la bonne section packages = Package.objects \ .select_related('arch') \ .filter(distribution=distro, section=section) \ .order_by('name') # Paginer paginator = Paginator(packages, 20) try: page = int(page) except ValueError: page = 1 try: packages = paginator.page(page).object_list except (EmptyPage, InvalidPage): packages = paginator.page(paginator.num_pages).object_list # Rendre la template return tpl( 'packages/list.html', { 'distro': distro, 'section': section, 'list_pages': get_list_page(page, paginator.num_pages, 4), 'packages': packages }, request)
def viewsourcelog(request, log_id): # Afficher les informations sur une construction d'une source log_id = int(log_id) # Récupérer le log try: log = SourceLog.objects \ .select_related('source', 'distribution', 'arch') \ .get(pk=log_id) except SourceLog.DoesNotExist: raise Http404 # Gérer les flags log.flag_latest = ((log.flags & 1) != 0) log.flag_manual = ((log.flags & 2) != 0) log.flag_failed = ((log.flags & 4) != 0) log.flag_overwrite = ((log.flags & 8) != 0) log.flag_rebuild = ((log.flags & 16) != 0) log.flag_continuous = ((log.flags & 32) != 0) log.flag_warnings = ((log.flags & 64) != 0) log.flag_building = ((log.flags & 128) != 0) # Gérer les dépendances log.depends = log.depends.split(';') log.conflicts = log.conflicts.split(';') log.suggests = log.suggests.split(';') # Adresse des logs part = (log_id >> 10) << 10 filename = '/files/logs/%i-%i' % (part, part + 1024) # Afficher la template return tpl('packages/loginfo.html', { 'log': log, 'filename': filename }, request)
def showpackage(request, package_id): # Afficher un paquet. Pas oublier de gérer distro_id=0 package_id = int(package_id) # Récupérer le paquet try: package = Package.objects \ .select_related('arch', 'distribution', 'section', 'sourcepkg') \ .get(pk=package_id) except Package.DoesNotExist: raise Http404 # Splitter les chaînes package.depends = package.depends.split(';') package.conflicts = package.conflicts.split(';') package.suggests = package.suggests.split(';') package.provides = package.provides.split(';') package.replaces = package.replaces.split(';') # Prendre les paquets qui ont le même nom pour proposer un joli # menu à l'utilisateur pkgs = Package.objects \ .select_related('arch', 'distribution') \ .filter(name__exact=package.name) \ .order_by('-distribution') # Prendre les chaînes du paquet strings = String.objects.filter(package=package) package.title = str_of_package(package, request.LANGUAGE_CODE.split('-')[0], 0, strings, None).content package.short_desc = str_of_package(package, request.LANGUAGE_CODE.split('-')[0], 1, strings, None).content package.long_desc = str_of_package(package, request.LANGUAGE_CODE.split('-')[0], 2, strings, None).content # Gestion des votes if package.total_votes == 0: package.rating = 0.0 else: package.rating = float(package.votes) * 3.0 / float( package.total_votes) if request.user.is_anonymous(): can_vote = False else: votes = PackageVote.objects.filter(package=package, user=request.user.get_profile()) can_vote = (votes.count() == 0) # Prendre la dernière entrée de changelog changelog = ChangeLog.objects \ .filter(package=package) \ .order_by('-date')[0] # On a besoin du premier mirroir pour afficher l'icône mirror = Mirror.objects.get(pk=1) # Rendre la template return tpl( 'packages/view.html', { 'package': package, 'changelog': changelog, 'mirror': mirror, 'can_vote': can_vote, 'pkgs': pkgs }, request)
def opts_index(request): # Page statique, juste afficher la template return tpl('users/opts_index.html', {}, request)