Splitting web stuff off into a separate apps directory. This stuff will end up being replaced/re-written, it's ancient and really badly done.

This commit is contained in:
Greg Taylor 2008-06-15 04:40:14 +00:00
parent 75b39ada13
commit ed1510c4a7
12 changed files with 23 additions and 23 deletions

0
webapps/__init__.py Normal file
View file

0
webapps/news/__init__.py Executable file
View file

44
webapps/news/models.py Executable file
View file

@ -0,0 +1,44 @@
from django.db import models
from django.contrib.auth.models import User
class NewsTopic(models.Model):
"""
Represents a news topic.
"""
name = models.CharField(max_length=75, unique=True)
description = models.TextField(blank=True)
icon = models.ImageField(upload_to='newstopic_icons', default='newstopic_icons/default.png', blank=True, help_text="Image for the news topic.")
def __str__(self):
try:
return self.name
except:
return "Invalid"
class Meta:
ordering = ['name']
class Admin:
list_display = ('name', 'icon')
class NewsEntry(models.Model):
"""
An individual news entry.
"""
author = models.ForeignKey(User, related_name='author')
title = models.CharField(max_length=255)
body = models.TextField()
topic = models.ForeignKey(NewsTopic, related_name='newstopic')
date_posted = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
class Meta:
ordering = ('-date_posted',)
verbose_name_plural = "News entries"
class Admin:
list_display = ('title', 'author', 'topic', 'date_posted')
list_filter = ('topic',)
search_fields = ['title']

8
webapps/news/urls.py Executable file
View file

@ -0,0 +1,8 @@
from django.conf.urls.defaults import *
urlpatterns = patterns('webapps.news.views',
(r'^show/(?P<entry_id>\d+)/$', 'show_news'),
(r'^archive/$', 'news_archive'),
(r'^search/$', 'search_form'),
(r'^search/results/$', 'search_results'),
)

126
webapps/news/views.py Executable file
View file

@ -0,0 +1,126 @@
"""
This is a very simple news application, with most of the expected features
like:
* News categories/topics
* Searchable archives
"""
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
import django.views.generic.list_detail as gv_list_detail
from django.http import HttpResponseRedirect
from django.contrib.auth.models import User
from django import newforms as forms
from django.db.models import Q
from webapps.news.models import NewsTopic, NewsEntry
# The sidebar text to be included as a variable on each page. There's got to
# be a better, cleaner way to include this on every page.
sidebar = """
<p class='doNotDisplay doNotPrint'>This page&rsquo;s menu:</p>
<ul id='side-bar'>
<li><a href='/news/archive'>News Archive</a></li>
<li><a href='/news/search'>Search News</a></li>
</ul>
"""
class SearchForm(forms.Form):
"""
Class to represent a news search form under Django's newforms. This is used
to validate the input on the search_form view, as well as the search_results
view when we're picking the query out of GET. This makes searching safe
via the search form or by directly inputing values via GET key pairs.
"""
search_terms = forms.CharField(max_length=100, min_length=3, required=True)
def show_news(request, entry_id):
"""
Show an individual news entry. Display some basic information along with
the title and content.
"""
news_entry = get_object_or_404(NewsEntry, id=entry_id)
pagevars = {
"page_title": "News Entry",
"news_entry": news_entry,
"sidebar": sidebar
}
context_instance = RequestContext(request)
return render_to_response('news/show_entry.html', pagevars, context_instance)
def news_archive(request):
"""
Shows an archive of news entries.
TODO: Expand this a bit to allow filtering by month/year.
"""
news_entries = NewsEntry.objects.all().order_by('-date_posted')
# TODO: Move this to either settings.py or the SQL configuration.
entries_per_page = 15
pagevars = {
"page_title": "News Archive",
"browse_url": "/news/archive",
"sidebar": sidebar
}
return gv_list_detail.object_list(request, news_entries, template_name='news/archive.html', extra_context=pagevars, paginate_by=entries_per_page)
def search_form(request):
"""
Render the news search form. Don't handle much validation at all. If the
user enters a search term that meets the minimum, send them on their way
to the results page.
"""
if request.method == 'GET':
# A GET request was sent to the search page, load the value and
# validate it.
search_form = SearchForm(request.GET)
if search_form.is_valid():
# If the input is good, send them to the results page with the
# query attached in GET variables.
return HttpResponseRedirect('/news/search/results/?search_terms='+ search_form.cleaned_data['search_terms'])
else:
# Brand new search, nothing has been sent just yet.
search_form = SearchForm()
pagevars = {
"page_title": "Search News",
"search_form": search_form,
"debug": debug,
"sidebar": sidebar
}
context_instance = RequestContext(request)
return render_to_response('news/search_form.html', pagevars, context_instance)
def search_results(request):
"""
Shows an archive of news entries. Use the generic news browsing template.
"""
# TODO: Move this to either settings.py or the SQL configuration.
entries_per_page = 15
# Load the form values from GET to validate against.
search_form = SearchForm(request.GET)
# You have to call is_valid() or cleaned_data won't be populated.
valid_search = search_form.is_valid()
# This is the safe data that we can pass to queries without huge worry of
# badStuff(tm).
cleaned_get = search_form.cleaned_data
# Perform searches that match the title and contents.
# TODO: Allow the user to specify what to match against and in what
# topics/categories.
news_entries = NewsEntry.objects.filter(Q(title__contains=cleaned_get['search_terms']) | Q(body__contains=cleaned_get['search_terms']))
pagevars = {
"page_title": "Search Results",
"searchtext": cleaned_get['search_terms'],
"browse_url": "/news/search/results",
"sidebar": sidebar
}
return gv_list_detail.object_list(request, news_entries, template_name='news/archive.html', extra_context=pagevars, paginate_by=entries_per_page)

View file

View file

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

5
webapps/website/urls.py Normal file
View file

@ -0,0 +1,5 @@
from django.conf.urls.defaults import *
urlpatterns = patterns('webapps.website.views',
(r'^$', 'page_index'),
)

57
webapps/website/views.py Normal file
View file

@ -0,0 +1,57 @@
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.contrib.auth.models import User
from webapps.news.models import NewsEntry
import functions_db
"""
This file contains the generic, assorted views that don't fall under one of
the other applications.
"""
def page_index(request):
"""
Main root page.
"""
# Some misc. configurable stuff.
# TODO: Move this to either SQL or settings.py based configuration.
fpage_player_limit = 4
fpage_news_entries = 2
# A QuerySet of recent news entries.
news_entries = NewsEntry.objects.all().order_by('-date_posted')[:fpage_news_entries]
# Dictionary containing database statistics.
objstats = functions_db.object_totals()
# A QuerySet of the most recently connected players.
recent_players = functions_db.get_recently_connected_players()[:fpage_player_limit]
pagevars = {
"page_title": "Front Page",
"news_entries": news_entries,
"players_connected_recent": recent_players,
"num_players_connected": functions_db.get_connected_players().count(),
"num_players_registered": functions_db.num_total_players(),
"num_players_connected_recent": functions_db.get_recently_connected_players().count(),
"num_players_registered_recent": functions_db.get_recently_created_players().count(),
"num_players": objstats["players"],
"num_rooms": objstats["rooms"],
"num_things": objstats["things"],
"num_exits": objstats["exits"],
}
context_instance = RequestContext(request)
return render_to_response('index.html', pagevars, context_instance)
def to_be_implemented(request):
"""
A notice letting the user know that this particular feature hasn't been
implemented yet.
"""
pagevars = {
"page_title": "To Be Implemented...",
}
context_instance = RequestContext(request)
return render_to_response('tbi.html', pagevars, context_instance)

View file

@ -0,0 +1,9 @@
import gameconf
def general_context(request):
"""
Returns common Evennia-related context stuff.
"""
return {
'game_name': gameconf.get_configvalue('site_name'),
}