diff --git a/create_quest/forms.py b/create_quest/forms.py index dbdaa9b..8cdc7e2 100644 --- a/create_quest/forms.py +++ b/create_quest/forms.py @@ -4,7 +4,7 @@ Form(s) for the create_quest page. """ from django import forms -from quest.models import Quest, Post, PageTitle +from quest.models import Quest, Post class QuestForm(forms.ModelForm): """ diff --git a/create_quest/views.py b/create_quest/views.py index 2881cf9..00e5445 100644 --- a/create_quest/views.py +++ b/create_quest/views.py @@ -6,7 +6,7 @@ from django.contrib import messages from django.shortcuts import redirect, render from .forms import QuestForm, PostForm -from quest.models import Quest, Post, PageTitle +from quest.models import Quest, Post, Page def index(request): """ @@ -16,18 +16,19 @@ def index(request): # TODO: clean the post body quest = Quest(owner=request.user) quest_form = QuestForm(request.POST, instance=quest) - post = Post(post_type='text', page_num=1) + post = Post(post_type='text') post_form = PostForm(request.POST, instance=post) if all((quest_form.is_valid(), post_form.is_valid())): quest.save() - post.quest = quest - post.save() - page = PageTitle( + page = Page( quest=quest, page_num=1, title="Page 1" ) page.save() + post.quest = quest + post.page = page + post.save() return redirect('quest:quest', quest_id=quest.id) else: quest_form = QuestForm() diff --git a/jinja2/base.html b/jinja2/base.html index 32d855f..bc6ac8c 100644 --- a/jinja2/base.html +++ b/jinja2/base.html @@ -1,3 +1,4 @@ +{% set vars = {} %} diff --git a/quest/events.py b/quest/events.py index 0e5db02..dfccded 100644 --- a/quest/events.py +++ b/quest/events.py @@ -154,9 +154,10 @@ def text_post(socket, data): # handle image + quest = Quest.objects.get(id=socket.quest_id) p = Post( - quest=Quest.objects.get(id=socket.quest_id), - page_num=page_num, + quest=quest, + page=Page.objects.get(quest=quest, page_num=page_num), post_type='text', post_text=post_text) p.save() @@ -196,10 +197,11 @@ def dice_post(socket, data): post_text = "Roll " + dice_roll if form['diceChal']: post_text += " vs DC" + str(form['diceChal']) - + + quest = Quest.objects.get(id=socket.quest_id) p = Post( - quest=Quest.objects.get(id=socket.quest_id), - page_num=page_num, + quest=quest, + page=Page.objects.get(quest=quest, page_num=page_num), post_type='dice', post_text=post_text ) @@ -232,9 +234,10 @@ def poll_post(socket, data): return # error message? form = form.cleaned_data + quest=Quest.objects.get(id=socket.quest_id) p = Post( - quest=Quest.objects.get(id=socket.quest_id), - page_num=page_num, + quest=quest, + page=Page.objects.get(quest=quest, page_num=page_num), post_type='poll', post_text="Poll" ) @@ -412,19 +415,39 @@ def new_page(socket, data): Called when the QM creates a new page. """ title = data.get('page_title') + appendix = bool(data.get('appendix')) quest = Quest.objects.get(id=socket.quest_id) - p = PageTitle( + if appendix: + page = Page.objects.filter( + quest=quest, + appendix=True + ).order_by('page_num').last() + if page: + page_num = chr(ord(page.page_num)+1) + else: + page_num = 'a' + else: + page_num = Page.objects.filter( + quest=quest, + appendix=False + ).order_by('page_num').last().page_num + 1 + p = Page( quest=quest, - page_num=PageTitle.objects.filter(quest=quest).count() + 1, + page_num=page_num, title=title, + appendix=appendix, ) p.save() data = {} data['page_num'] = p.page_num data['title'] = title - data['url'] = reverse('quest:quest', args=[socket.quest_id, p.page_num]) + if appendix: + data['appendix'] = True + else: + data['appendix'] = False + data['url'] = reverse('quest:quest', args=[socket.quest_id, p.page_num]) socket.send('new_page', data) diff --git a/quest/jinja2/quest/quest.html b/quest/jinja2/quest/quest.html index 764b365..b50d6f3 100644 --- a/quest/jinja2/quest/quest.html +++ b/quest/jinja2/quest/quest.html @@ -23,9 +23,18 @@ {% endif %}
  • {% if request.session.get("hide_chat") == True %}←{% else %}→{% endif %}
  • @@ -143,14 +152,15 @@ {% endif %} - {% if page_num != pages.reverse()[0].page_num %} + {% if vars['next_page'] %}
    - +
    {% endif %} @@ -158,7 +168,7 @@

    Chat

    {% autoescape false %} - {% for message in messages %} + {% for message in chat_messages %}
    {{ message.user.username or quest.anon_name }} diff --git a/quest/migrations/0001_initial.py b/quest/migrations/0001_initial.py index a047359..b35f359 100644 --- a/quest/migrations/0001_initial.py +++ b/quest/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 2.1.1 on 2018-09-05 17:44 +# Generated by Django 2.1.1 on 2018-09-25 13:36 import django.core.validators from django.db import migrations, models @@ -22,11 +22,12 @@ class Migration(migrations.Migration): ], ), migrations.CreateModel( - name='PageTitle', + name='Page', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('page_num', models.IntegerField()), + ('page_num', models.CharField(max_length=4)), ('title', models.CharField(max_length=200)), + ('appendix', models.BooleanField(default=False)), ], ), migrations.CreateModel( @@ -47,7 +48,6 @@ class Migration(migrations.Migration): name='Post', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('page_num', models.IntegerField(default=1)), ('post_type', models.CharField(choices=[('text', 'Text'), ('dice', 'Dice'), ('poll', 'Poll')], max_length=4)), ('post_text', models.TextField()), ('timestamp', models.DateTimeField(auto_now=True)), @@ -58,16 +58,17 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('title', models.CharField(max_length=200)), + ('anon_name', models.CharField(default='Anonymous', max_length=20)), ], ), migrations.CreateModel( name='DiceCall', fields=[ ('post', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to='quest.Post')), - ('dice_roll', models.CharField(max_length=9)), + ('dice_roll', models.CharField(max_length=16)), ('strict', models.BooleanField()), - ('dice_challenge', models.IntegerField(blank=True, null=True, validators=[django.core.validators.MaxValueValidator(999), django.core.validators.MinValueValidator(1)])), - ('rolls_taken', models.IntegerField(blank=True, null=True, validators=[django.core.validators.MaxValueValidator(99), django.core.validators.MinValueValidator(1)])), + ('dice_challenge', models.IntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(65536)])), + ('rolls_taken', models.IntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(256)])), ('open', models.BooleanField()), ], ), @@ -75,7 +76,7 @@ class Migration(migrations.Migration): name='DiceRoll', fields=[ ('message', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to='quest.Message')), - ('roll', models.CharField(max_length=9)), + ('roll', models.CharField(max_length=16)), ('results', models.TextField()), ('total', models.IntegerField()), ], diff --git a/quest/migrations/0002_auto_20180905_1344.py b/quest/migrations/0002_auto_20180925_0936.py similarity index 89% rename from quest/migrations/0002_auto_20180905_1344.py rename to quest/migrations/0002_auto_20180925_0936.py index 1673c54..51c6e19 100644 --- a/quest/migrations/0002_auto_20180905_1344.py +++ b/quest/migrations/0002_auto_20180925_0936.py @@ -1,4 +1,4 @@ -# Generated by Django 2.1.1 on 2018-09-05 17:44 +# Generated by Django 2.1.1 on 2018-09-25 13:36 from django.conf import settings from django.db import migrations, models @@ -10,8 +10,8 @@ class Migration(migrations.Migration): initial = True dependencies = [ - migrations.swappable_dependency(settings.AUTH_USER_MODEL), ('quest', '0001_initial'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), ] operations = [ @@ -20,6 +20,11 @@ class Migration(migrations.Migration): name='owner', field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), ), + migrations.AddField( + model_name='post', + name='page', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='quest.Page'), + ), migrations.AddField( model_name='post', name='quest', @@ -36,7 +41,7 @@ class Migration(migrations.Migration): field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), ), migrations.AddField( - model_name='pagetitle', + model_name='page', name='quest', field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='quest.Quest'), ), diff --git a/quest/migrations/0003_quest_anon_name.py b/quest/migrations/0003_quest_anon_name.py deleted file mode 100644 index afe5371..0000000 --- a/quest/migrations/0003_quest_anon_name.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 2.1.1 on 2018-09-21 16:50 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('quest', '0002_auto_20180905_1344'), - ] - - operations = [ - migrations.AddField( - model_name='quest', - name='anon_name', - field=models.CharField(default='Anonymous', max_length=20), - ), - ] diff --git a/quest/migrations/0004_auto_20180924_0931.py b/quest/migrations/0004_auto_20180924_0931.py deleted file mode 100644 index 92923bc..0000000 --- a/quest/migrations/0004_auto_20180924_0931.py +++ /dev/null @@ -1,34 +0,0 @@ -# Generated by Django 2.1.1 on 2018-09-24 13:31 - -import django.core.validators -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('quest', '0003_quest_anon_name'), - ] - - operations = [ - migrations.AlterField( - model_name='dicecall', - name='dice_challenge', - field=models.IntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(65536)]), - ), - migrations.AlterField( - model_name='dicecall', - name='dice_roll', - field=models.CharField(max_length=16), - ), - migrations.AlterField( - model_name='dicecall', - name='rolls_taken', - field=models.IntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(256)]), - ), - migrations.AlterField( - model_name='diceroll', - name='roll', - field=models.CharField(max_length=16), - ), - ] diff --git a/quest/models.py b/quest/models.py index 8519302..b43c856 100644 --- a/quest/models.py +++ b/quest/models.py @@ -31,12 +31,25 @@ class Message(models.Model): message = models.TextField(max_length=512) +class Page(models.Model): + """ + Represents the title of a quest page. + """ + quest = models.ForeignKey(Quest, on_delete=models.CASCADE) + page_num = models.CharField(max_length=4) + title = models.CharField(max_length=200) + appendix = models.BooleanField(default=False) + + def __str__(self): + return self.title + + class Post(models.Model): """ An object for arbituary posts. Contains all post data, type, etc. """ quest = models.ForeignKey(Quest, on_delete=models.CASCADE) - page_num = models.IntegerField(default=1) + page = models.ForeignKey(Page, on_delete=models.CASCADE) POST_TYPES = ( ('text', 'Text'), ('dice', 'Dice'), @@ -129,15 +142,3 @@ class PollVote(models.Model): class Meta: unique_together = ('option', 'ip_address') - - -class PageTitle(models.Model): - """ - Represents the title of a quest page. - """ - quest = models.ForeignKey(Quest, on_delete=models.CASCADE) - page_num = models.IntegerField() - title = models.CharField(max_length=200) - - def __str__(self): - return self.title diff --git a/quest/static/quest.js b/quest/static/quest.js index 0452027..1e29350 100644 --- a/quest/static/quest.js +++ b/quest/static/quest.js @@ -156,7 +156,7 @@ socket.events['set_option_box'] = function(data) { } socket.events['new_page'] = function(data) { if (page_num != data.page_num-1) { return; } - let html_str = '
    '; + let html_str = '
    '; document.getElementById('questPane').innerHTML = document.getElementById('questPane').innerHTML + html_str; } diff --git a/quest/urls.py b/quest/urls.py index 5acfcfa..b1f03be 100644 --- a/quest/urls.py +++ b/quest/urls.py @@ -10,7 +10,7 @@ app_name = 'quest' urlpatterns = [ path('', views.index, name='index'), path('', views.quest, name='quest'), - path('/', views.quest, name='quest'), + path('/', views.quest, name='quest'), path('/edit_quest', views.edit_quest, name='edit_quest'), - path('//edit_quest', views.edit_quest, name='edit_quest'), + path('//edit_quest', views.edit_quest, name='edit_quest'), ] diff --git a/quest/views.py b/quest/views.py index 8087e59..bcf2378 100644 --- a/quest/views.py +++ b/quest/views.py @@ -2,10 +2,11 @@ """ Quest and quest accessory views. """ -from django.shortcuts import render, redirect +from django.contrib import messages from django.http import HttpResponse +from django.shortcuts import render, redirect -from .models import Quest, DiceRoll, PollOption, PollVote, PageTitle +from .models import Quest, DiceRoll, PollOption, PollVote, Page from .forms import EditQuestForm def index(request): @@ -15,17 +16,23 @@ def index(request): return HttpResponse("Hello, world. You're at the quest index.") -def quest(request, quest_id, page_num=1): +def quest(request, quest_id, page_num='1'): """ Arbituary quest page view. """ + # TODO: 404 quest not found quest = Quest.objects.get(id=quest_id) - pages = PageTitle.objects.filter(quest=quest).order_by('page_num') - if page_num > pages.reverse()[0].page_num: - page_num = pages.reverse()[0].page_num - return redirect('quest:quest', quest_id=quest.id, page_num=page_num) - messages = quest.message_set.all() - posts = quest.post_set.filter(page_num=page_num) + pages = Page.objects.filter( + quest=quest, appendix=False).order_by('page_num') + appendices = Page.objects.filter( + quest=quest, appendix=True).order_by('title') + chat_messages = quest.message_set.all() + try: + page = Page.objects.get(quest=quest, page_num=page_num) + except Page.DoesNotExist: + messages.error(request, "Page not found, redirecting you.") + return redirect('quest:quest', quest_id=quest.id, page_num='1') + posts = quest.post_set.filter(page=page) # TODO: filter by page_num as well dice_rolls = DiceRoll.objects.filter(dicecall__post__quest=quest) poll_options = PollOption.objects.filter(poll__post__quest=quest) diff --git a/todo b/todo index 4f136c5..3c044d6 100644 --- a/todo +++ b/todo @@ -1,5 +1,4 @@ New Features: -Pages/appendixes Live indicator/countdown Notifications Banner images diff --git a/user/migrations/0001_initial.py b/user/migrations/0001_initial.py index fe0d2aa..2099e9b 100644 --- a/user/migrations/0001_initial.py +++ b/user/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 2.1.1 on 2018-09-05 17:44 +# Generated by Django 2.1.1 on 2018-09-25 13:36 import django.contrib.auth.models import django.core.validators