Skip to content

Commit

Permalink
Merge pull request #319 from domino14/fix-wrong-answers
Browse files Browse the repository at this point in the history
Fix wrong answers
  • Loading branch information
domino14 authored Apr 26, 2019
2 parents e2f1271 + 10f348c commit 03ee85b
Show file tree
Hide file tree
Showing 9 changed files with 368 additions and 27 deletions.
5 changes: 3 additions & 2 deletions djAerolith/accounts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
# To contact the author, please email delsolar at gmail dot com

from django.urls import path, re_path
from accounts.views import editProfile, viewProfile
from accounts.views import editProfile, viewProfile, set_default_lexicon

urlpatterns = [
re_path(r'^$', editProfile, name='accounts_edit_profile'),
path(r'<str:username>', viewProfile, name='accounts_view_profile'),
path(r'set_default_lexicon/', set_default_lexicon),
path(r'<str:username>/', viewProfile, name='accounts_view_profile'),
]
24 changes: 23 additions & 1 deletion djAerolith/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,42 @@
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.shortcuts import render
from django.views.decorators.http import require_POST

from accounts.models import AerolithProfile
from accounts.forms import ProfileEditForm, UsernameEditForm
from base.models import Lexicon
from lib.response import response, bad_request
from wordwalls.models import Medal

logger = logging.getLogger(__name__)
DEFAULT_LANGUAGE = 'en'


@require_POST
@login_required
def set_default_lexicon(request):
try:
profile = AerolithProfile.objects.get(user=request.user)
except AerolithProfile.DoesNotExist:
raise Http404

body = json.loads(request.body)
lex = body.get('defaultLexicon', -1)
try:
lexicon = Lexicon.objects.get(pk=lex)
except Lexicon.DoesNotExist:
return bad_request('Lexicon not found')
profile.defaultLexicon = lexicon
profile.save()
return response('OK')


@login_required
def editProfile(request):
try:
profile = AerolithProfile.objects.get(user=request.user)
except:
except AerolithProfile.DoesNotExist:
raise Http404

pForm = ProfileEditForm()
Expand Down
2 changes: 1 addition & 1 deletion djAerolith/current_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CURRENT_VERSION = '1.2.1.1'
CURRENT_VERSION = '1.2.2.0'
16 changes: 9 additions & 7 deletions djAerolith/djaerolith/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
import os
import sys

from django.utils.translation import ugettext_lazy as _

from logging_filters import skip_suspicious_operations


def tobool(val):
if val is True:
Expand Down Expand Up @@ -71,7 +75,7 @@ def tobool(val):
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'
from django.utils.translation import ugettext_lazy as _

LANGUAGES = [
('en', _('English')),
('es', _('Spanish')),
Expand Down Expand Up @@ -167,8 +171,8 @@ def tobool(val):
'django.contrib.messages',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
#'basic.blog',
#'basic.inlines',
# 'basic.blog',
# 'basic.inlines',
'base',
'flashcards',
'tablegame',
Expand All @@ -180,8 +184,8 @@ def tobool(val):
'registration',
'social_django',
'captcha',
#'debug_toolbar',
#'locking'
# 'debug_toolbar',
# 'locking'
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
Expand Down Expand Up @@ -253,8 +257,6 @@ def tobool(val):
CSRF_FAILURE_VIEW = 'views.csrf_failure'


from logging_filters import skip_suspicious_operations

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
Expand Down
64 changes: 63 additions & 1 deletion djAerolith/wordwalls/static/js/wordwalls/newtable/sidebar.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* eslint-disable jsx-a11y/anchor-is-valid */
/* eslint-disable jsx-a11y/no-static-element-interactions */
/* eslint-disable jsx-a11y/click-events-have-key-events */
import React from 'react';
import PropTypes from 'prop-types';

import Pills from './pills';
import Select from '../forms/select';
import NumberInput from '../forms/number_input';

import Notifications from '../notifications';
/**
* Get lexicon options from the given object in a Select-friendly format.
* @param {Array.<Object>} lexicaObject
Expand All @@ -17,6 +20,57 @@ function getLexiconOptions(lexicaObject) {
}));
}


class MakeDefaultLexLink extends React.Component {
constructor(props) {
super(props);
this.confirmNewDefault = this.confirmNewDefault.bind(this);
}

confirmNewDefault() {
const userFriendlyName = this.props
.availableLexica
.find(lex => lex.id === this.props.selectedLexicon).lexicon;

Notifications.confirm(
'Are you sure?',
`Are you sure you wish to change the default lexicon to ${userFriendlyName}?`,
() => this.props.setDefaultLexicon(this.props.selectedLexicon),
);
}

render() {
if (this.props.defaultLexicon === this.props.selectedLexicon) {
return null;
}
return (
<div
className="defaultLink"
style={{ marginTop: '-10px', marginBottom: '10px' }}
>
<a
onClick={this.confirmNewDefault}
>Make default
</a>
</div>
);
}
}

MakeDefaultLexLink.propTypes = {
defaultLexicon: PropTypes.number.isRequired,
selectedLexicon: PropTypes.number.isRequired,
setDefaultLexicon: PropTypes.func.isRequired,

availableLexica: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.number,
lexicon: PropTypes.string,
description: PropTypes.string,
counts: PropTypes.object,
})).isRequired,
};


const Sidebar = props => (
<div>

Expand All @@ -37,6 +91,12 @@ const Sidebar = props => (
options={getLexiconOptions(props.availableLexica)}
onChange={e => props.setLexicon(parseInt(e.target.value, 10))}
/>
<MakeDefaultLexLink
defaultLexicon={props.defaultLexicon}
setDefaultLexicon={props.setDefaultLexicon}
selectedLexicon={props.currentLexicon}
availableLexica={props.availableLexica}
/>
<NumberInput
colSize={10}
label="Minutes"
Expand All @@ -62,7 +122,9 @@ Sidebar.propTypes = {
activeGameType: PropTypes.string.isRequired,
setGameType: PropTypes.func.isRequired,
currentLexicon: PropTypes.number.isRequired,
defaultLexicon: PropTypes.number.isRequired,
setLexicon: PropTypes.func.isRequired,
setDefaultLexicon: PropTypes.func.isRequired,
desiredTime: PropTypes.string.isRequired,
setTime: PropTypes.func.isRequired,
questionsPerRound: PropTypes.number.isRequired,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,10 +510,12 @@ class TableCreator extends React.Component {
activeGameType: option,
})}
currentLexicon={this.state.currentLexicon}
defaultLexicon={this.props.defaultLexicon}
availableLexica={this.props.availableLexica}
setLexicon={lex => this.setState({
currentLexicon: lex,
})}
setDefaultLexicon={this.props.setDefaultLexicon}
desiredTime={this.state.desiredTime}
setTime={t => this.setState({
desiredTime: t,
Expand Down Expand Up @@ -545,6 +547,7 @@ class TableCreator extends React.Component {

TableCreator.propTypes = {
defaultLexicon: PropTypes.number.isRequired,
setDefaultLexicon: PropTypes.func.isRequired,
availableLexica: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.number,
lexicon: PropTypes.string,
Expand Down
104 changes: 103 additions & 1 deletion djAerolith/wordwalls/static/js/wordwalls/test/wordwalls_game.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Test functions for wordwalls_game.js
/* eslint-disable import/no-extraneous-dependencies, no-unused-expressions */

import WordwallsGame from '../wordwalls_game';
import WordwallsGame, { Internal } from '../wordwalls_game';

const testQuestions = `[
{
Expand Down Expand Up @@ -221,3 +221,105 @@ describe('Game State', () => {
});
});
});

describe('Internal functions', () => {
describe('letterCounts', () => {
it('should count letters properly', () => {
const testArray = [
['AEROLITH', {
A: 1, E: 1, R: 1, O: 1, L: 1, I: 1, T: 1, H: 1,
}],
['FOO', { F: 1, O: 2 }],
['BAR?', {
B: 1, A: 1, R: 1, '?': 1,
}],
['BAR??', {
B: 1, A: 1, R: 1, '?': 2,
}],
];
for (let i = 0; i < testArray.length; i += 1) {
expect(Internal().letterCounts(testArray[i][0])).toEqual(testArray[i][1]);
}
});
});

describe('inBlankAnagrams', () => {
const alphaHash = {
'ACHILORS?': true,
'AEHILOR?': true,
'AEHINOR?': true,
'AEINOR??': true,
'ABBINOR?': true,
AEFFGINR: true,
};

it('should find alphagram properly, no build mode', () => {
const testArray = [
['AEROLITH', 'AEHILOR?'],
['ANTIHERO', 'AEHINOR?'],
['ERASIONS', 'AEINOR??'],
['RABBONIS', 'ABBINOR?'],
['ROBINIAS', undefined],
['AERODYNE', undefined],
['ACROLITH', undefined],
['ACROLITHS', 'ACHILORS?'],
];

for (let i = 0; i < testArray.length; i += 1) {
expect(Internal().anagramOfQuestions(
testArray[i][0],
alphaHash,
)).toEqual(testArray[i][1]);
}
});

it('should properly calculate anagram of question', () => {
const testArrayTrue = [
['ACROLITH'.split(''), 'ACHILORS?', true, 5, 8],
['ACROLITH'.split(''), 'ACHILOR?', true, 5, 8],
['ACROLITH'.split(''), 'ACHILORT'],
['MUUMUUS'.split(''), 'MMSSUUU?', true, 5, 8],
];
const testArrayFalse = [
['MUUMUUS'.split(''), 'MMSSUU?', true, 5, 8],
['ACROLITH'.split(''), 'AHHILOR?', true, 5, 8],
['ACROLITH'.split(''), 'AEHILORT'],
];
for (let i = 0; i < testArrayTrue.length; i += 1) {
expect(Internal().anagramOfQuestion.apply(null, testArrayTrue[i])).toBeTruthy();
}

for (let i = 0; i < testArrayFalse.length; i += 1) {
expect(Internal().anagramOfQuestion.apply(null, testArrayFalse[i])).toBeFalsy();
}
});

it('should find alphagram properly, build mode', () => {
const testArray = [
['AEROLITH', 'AEHILOR?', 5, 8],
['ANTIHERO', 'AEHINOR?', 5, 8],
['ERASIONS', 'AEINOR??', 5, 8],
['RABBONIS', 'ABBINOR?', 5, 8],
['ROBINIAS', undefined, 5, 8],
['AERODYNE', undefined, 5, 8],
['ACROLITH', 'ACHILORS?', 5, 8],
['ACROLITHS', 'ACHILORS?', 5, 9],
['ACROLITHS', undefined, 5, 8],
['GRIFF', 'AEFFGINR', 5, 8],
['REFFING', 'AEFFGINR', 5, 8],
['FIRE', 'AEHILOR?', 4, 7], // assumes dict is ordered based on insertion
['FIRE', undefined, 5, 8], // If minimum length is 5, don't find it.
];

for (let i = 0; i < testArray.length; i += 1) {
expect(Internal().anagramOfQuestions(
testArray[i][0],
alphaHash,
true,
testArray[i][2],
testArray[i][3],
)).toEqual(testArray[i][1]);
}
});
});
});
Loading

0 comments on commit 03ee85b

Please sign in to comment.