From cf650c14e6c319d290e693f39ea8ab6a88a17f7e Mon Sep 17 00:00:00 2001 From: nathaniel smith Date: Thu, 20 Apr 2017 21:18:13 -0700 Subject: [PATCH] add random url --- ttadmin/users/models.py | 24 ++++++++++++++++++++++++ ttadmin/users/urls.py | 3 ++- ttadmin/users/views.py | 17 ++++++++++++++++- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/ttadmin/users/models.py b/ttadmin/users/models.py index 308a9ce..ddbd545 100644 --- a/ttadmin/users/models.py +++ b/ttadmin/users/models.py @@ -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 diff --git a/ttadmin/users/urls.py b/ttadmin/users/urls.py index 5832263..342132c 100644 --- a/ttadmin/users/urls.py +++ b/ttadmin/users/urls.py @@ -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'), diff --git a/ttadmin/users/views.py b/ttadmin/users/views.py index f5cbe39..2e6124e 100644 --- a/ttadmin/users/views.py +++ b/ttadmin/users/views.py @@ -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)