My class communication design problem.

46 views
Skip to first unread message

Yarick Antonov

unread,
Sep 5, 2014, 11:17:33 AM9/5/14
to django...@googlegroups.com
I have serious OOP design problem, and i don't know the way to solve it.

Here are two of my views:
def exercise(request):
   
if request.method == 'GET':
        request
.session["exercise_start"] = time()
        form
= ExerciseForm(language = request.session["language"], dictionary = request.session["dictionary"])

   
elif request.method == 'POST':
        form
= ExerciseForm(request.POST, language = request.session["language"], dictionary = request.session["dictionary"])

       
if form.is_valid():
           
ExerciseTime = time() - request.session["exercise_start"]
           
ExerciseText = form.cleaned_data["exercise_field"]

           
if request.user.is_authenticated():
                exercise
= Exercise(player = request.user)
                exercise
.time = ExerciseTime
                exercise
.save()

            request
.session["exercise_text"] = ExerciseText
            request
.session["exercise_time"] = ExerciseTime
           
return redirect('game_result')
   
return render(request, "game/exercise.html", {'form': form})

def result(request):
    exercise_result
= {"exercise_text": request.session["exercise_text"], "exercise_time": request.session["exercise_time"]}
   
return render(request, "game/result.html", exercise_result)

And here is my "exercise form" which obviously used by "exercise view":
class ExerciseForm(forms.Form):
   
def __init__(self, *args, **kwargs):
       
self.language = kwargs.pop('language', None)
       
self.dictionary = kwargs.pop('dictionary', None)
       
self.exercise_text = self.getRandomText(lng=self.language, dct=self.dictionary)
       
self.SetExText()
       
super(ExerciseForm, self).__init__(*args, **kwargs)

   
def SetExText(self):
       
self.exercise_text = self.getRandomText(self.language, self.dictionary)
       
self.exercise_field.exercise_text = self.exercise_text

   
def getRandomText(self, lng, dct):
        count
= ExerciseText.objects.filter(dictionary__dictionary_name = dct).count()
       
if count:
            rand_id
= sample(range(1, count), 1)
           
return ExerciseText.objects.get(id__in=rand_id).text
       
else:
           
return "lol"

    exercise_field
= ExerciseField(exercise_text = exercise_text, widget = forms.TextInput(attrs = {'autocomplete': 'off', 'autofocus': 'autofocus'}))

My field, used in form above:
class ExerciseField(forms.CharField):
   
def __init__(self, exercise_text, *args, **kwargs):
       
self.exercise_text = exercise_text
       
super(ExerciseField, self).__init__(*args, **kwargs)

   
def validate(self, value):
       
if value != self.exercise_text:
           
raise ValidationError("Texts don't match")

And finally, simple template:
{% extends "base.html" %}

{% block content %}
<form action="game_result" method="post">
   
{% csrf_token %}
   
{{ form.exercise_text }}<br/>
   
{{ form.as_p }}
</form>
{% endblock content %}

What i try to do:
I try to bargain two arguments which come from "create view" which not represent here in sake of space saving, and these two arguments are "language", and "dictionary".
I use them to get a record from db table, to get random text.

And here I need your help, because i can't dynamically create variable "exercise_text", because i need it in Class instance.
Meaning - it need to be predefined before I'll use my "exercise_field.

And also i can't put exercise_field inside of "__init__", because it will not be accessible in template.

In short:
I need to get random record from db, and i need to put this record in my field. How to do that? Dynamically.

Kyle Connors

unread,
Sep 5, 2014, 2:06:11 PM9/5/14
to django...@googlegroups.com
I believe what you're looking for is an initial value. In __init__ the following would achieve this:

self.fields['exercise_field'].initial = self.getRandomText(self.language, self.dictionary)

Collin Anderson

unread,
Sep 5, 2014, 2:27:30 PM9/5/14
to django...@googlegroups.com
I'd recommend not using a custom field for this. Instead, you can do something like:

class ExerciseForm(forms.Form):
        exercise_field
= forms.CharField(widget=forms.TextInput(attrs={'autocomplete': 'off', 'autofocus': 'autofocus'}))


       
def __init__(self, *args, **kwargs):
           
self.language = kwargs.pop('language', None)
           
self.dictionary = kwargs.pop('dictionary', None)

           
self.exercise_text = self.getRandomText(self.language, self.dictionary)
           
super(ExerciseForm, self).__init__(*args, **kwargs)

       
def clean_exercise_field(self):
           
if self.cleaned_data['exercise_field'] != self.exercise_text:
               
raise forms.ValidationError("Texts don't match")
           
return self.cleaned_data['exercise_field']

Yarick Antonov

unread,
Sep 6, 2014, 5:52:58 AM9/6/14
to django...@googlegroups.com
Your solution works!!! Thanks a lot!!!
Reply all
Reply to author
Forward
0 new messages