add random url

pull/21/head
nathaniel smith 2017-04-20 21:18:13 -07:00
parent 568c153a32
commit cf650c14e6
3 changed files with 42 additions and 2 deletions

View File

@ -13,11 +13,22 @@ from django.template.loader import get_template
from common.mailing import send_email
from help.models import Ticket
logger = logging.getLogger()
SSH_TYPE_CHOICES = (
('ssh-rsa', 'ssh-rsa',),
('ssh-dss', 'ssh-dss',),
)
DEFAULT_INDEX_PATH = '/etc/skel/public_html/index.html'
if os.path.exists(DEFAULT_INDEX_PATH):
DEFAULT_INDEX_PAGE = open(DEFAULT_INDEX_PATH).read().rstrip()
else:
logger.warning('No default html page found in skel. using empty string.')
DEFAULT_INDEX_PAGE = ''
KEYFILE_HEADER = """########## GREETINGS! ##########
# Hi! This file is automatically managed by tilde.town. You
# probably shouldn't change it. If you want to add more public keys that's
@ -35,6 +46,10 @@ class Townie(User):
reasons = TextField(blank=True, null=False, default='')
displayname = CharField(max_length=100, blank=False, null=False)
@property
def home(self):
return os.path.join('/home', self.username)
def send_welcome_email(self, admin_name='vilmibm'):
welcome_tmpl = get_template('users/welcome_email.txt')
context = {
@ -53,6 +68,15 @@ class Townie(User):
self.email))
# managing concrete system state
def has_modified_page(self):
"""Returns whether or not the user has modified index.html. If they
don't have one, returns False."""
index_path = os.path.join(self.home, 'public_html/index.html')
if not os.path.exists(index_path):
return False
index_page = open(index_path).read().rstrip()
return index_page != DEFAULT_INDEX_PAGE
def create_on_disk(self):
"""A VERY NOT IDEMPOTENT create function. Originally, I had ambitions

View File

@ -1,9 +1,10 @@
from django.conf.urls import url
from .views import SignupView, ThanksView, KeyMachineView
from .views import SignupView, ThanksView, KeyMachineView, RandomView
app_name = 'users'
urlpatterns = [
url(r'^random/?$', RandomView.as_view(), name='random'),
url(r'^signup/?$', SignupView.as_view(), name='signup'),
url(r'^thanks/?$', ThanksView.as_view(), name='thanks'),
url(r'^keymachine/?$', KeyMachineView.as_view(), name='keymachine'),

View File

@ -1,11 +1,12 @@
import re
import random
from django.core.exceptions import ValidationError
from django.db import transaction
from django.forms import Form, CharField, EmailField, Textarea, ChoiceField, BooleanField
from django.http import HttpResponse
from django.shortcuts import redirect
from django.views.generic import TemplateView
from django.views.generic import TemplateView, View
from django.views.generic.edit import FormView
from .forms import TownieForm
@ -37,3 +38,17 @@ class ThanksView(TemplateView):
class KeyMachineView(TemplateView):
template_name = 'users/keymachine.html'
class RandomView(View):
def get(self):
url = None
users = list(Townie.objects.all())
random.shuffle(users)
for user in users:
if user.has_modified_page():
url = 'https://tilde.town/~{}'.format(user.username)
break
if url is None:
url = 'https://tilde.town'
return redirect(url)