Skip to content

Commit

Permalink
demo for login required layout
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhuang committed May 15, 2013
1 parent a5cc7fe commit 848c806
Show file tree
Hide file tree
Showing 8 changed files with 453 additions and 406 deletions.
58 changes: 28 additions & 30 deletions deme_django/cms/forms.py
Expand Up @@ -73,9 +73,7 @@ def render(self, name, value, attrs=None):
var normalized_data = $.map(data, function(x){ return {value: x[0], id: x[1]} });
normalized_data.splice(0, 0, {value: "[None]", id: ""});
cache[term] = normalized_data;
if (xhr !== last_xhr) {
response(normalized_data);
}
response(normalized_data);
});
},
});
Expand All @@ -93,7 +91,7 @@ class AjaxModelChoiceField(forms.ModelChoiceField):
default_error_messages = {
'permission_denied': _(u'You do not have permission to set this field to this value.'),
}

def __init__(self, *args, **kwargs):
self.permission_cache = kwargs.pop('permission_cache')
self.required_abilities = kwargs.pop('required_abilities')
Expand Down Expand Up @@ -137,36 +135,36 @@ class SelectTimeWidget(Widget):
"""
A Widget that splits time input into <select> elements.
Allows form to show as 24hr: <hour>:<minute>:<second>,
or as 12hr: <hour>:<minute>:<second> <am|pm>
or as 12hr: <hour>:<minute>:<second> <am|pm>
Also allows user-defined increments for minutes/seconds
"""
hour_field = '%s_hour'
minute_field = '%s_minute'
second_field = '%s_second'
second_field = '%s_second'
meridiem_field = '%s_meridiem'
twelve_hr = False # Default to 24hr.

def __init__(self, attrs=None, hour_step=None, minute_step=None, second_step=None, twelve_hr=False):
'''
hour_step, minute_step, second_step are optional step values for
for the range of values for the associated select element
twelve_hr: If True, forces the output to be in 12-hr format (rather than 24-hr)
'''
self.attrs = attrs or {}

if twelve_hr:
self.twelve_hr = True # Do 12hr (rather than 24hr)
self.meridiem_val = 'a.m.' # Default to Morning (A.M.)

if hour_step and twelve_hr:
self.hours = range(1,13,hour_step)
self.hours = range(1,13,hour_step)
elif hour_step: # 24hr, with stepping.
self.hours = range(0,24,hour_step)
elif twelve_hr: # 12hr, no stepping
self.hours = range(1,13)
else: # 24hr, no stepping
self.hours = range(0,24)
self.hours = range(0,24)

if minute_step:
self.minutes = range(0,60,minute_step)
Expand All @@ -193,12 +191,12 @@ def render(self, name, value, attrs=None):
if match:
time_groups = match.groups();
hour_val = int(time_groups[HOURS]) % 24 # force to range(0-24)
minute_val = int(time_groups[MINUTES])
minute_val = int(time_groups[MINUTES])
if time_groups[SECONDS] is None:
second_val = 0
else:
second_val = int(time_groups[SECONDS])

# check to see if meridiem was passed in
if time_groups[MERIDIEM] is not None:
self.meridiem_val = time_groups[MERIDIEM]
Expand All @@ -210,7 +208,7 @@ def render(self, name, value, attrs=None):
self.meridiem_val = 'a.m.'
else:
self.meridiem_val = None


# If we're doing a 12-hr clock, there will be a meridiem value, so make sure the
# hours get printed correctly
Expand All @@ -219,7 +217,7 @@ def render(self, name, value, attrs=None):
hour_val = hour_val % 12
elif hour_val == 0:
hour_val = 12

output = []
if 'id' in self.attrs:
id_ = self.attrs['id']
Expand Down Expand Up @@ -247,7 +245,7 @@ def render(self, name, value, attrs=None):
select_html = Select(choices=second_choices).render(self.second_field % name, second_val, local_attrs)

output.append(select_html)

if self.twelve_hr:
# If we were given an initial value, make sure the correct meridiem get's selected.
if self.meridiem_val is not None and self.meridiem_val.startswith('p'):
Expand All @@ -268,17 +266,17 @@ def id_for_label(self, id_):
def value_from_datadict(self, data, files, name):
# if there's not h:m:s data, assume zero:
h = data.get(self.hour_field % name, 0) # hour
m = data.get(self.minute_field % name, 0) # minute
m = data.get(self.minute_field % name, 0) # minute
s = data.get(self.second_field % name, 0) # second
meridiem = data.get(self.meridiem_field % name, None)

#NOTE: if meridiem IS None, assume 24-hr
if meridiem is not None:
if meridiem.lower().startswith('p') and int(h) != 12:
h = (int(h)+12)%24
h = (int(h)+12)%24
elif meridiem.lower().startswith('a') and int(h) == 12:
h = 0

if (int(h) == 0 or h) and m and s:
return '%s:%s:%s' % (h, m, s)

Expand All @@ -299,39 +297,39 @@ def decompress(self,value):
if value:
return value.split(',')
return [None,None]


def render(self, name, value, attrs=None):
ints = (random.randint(0,9),random.randint(0,9),)
answer = hashlib.sha1(str(sum(ints))).hexdigest()
#print ints, sum(ints), answer

extra = """<span style="font-size: 11pt;">What is %d + %d?</span><br>""" % (ints[0], ints[1])
value = [answer, u'',]

return mark_safe(extra + super(CaptchaTextInput, self).render(name, value, attrs=attrs))


class CaptchaField(MultiValueField):
widget=CaptchaTextInput

def __init__(self, *args,**kwargs):
fields = (
CharField(show_hidden_initial=True),
CharField(show_hidden_initial=True),
CharField(),
)
super(CaptchaField,self).__init__(fields=fields, *args, **kwargs)

def compress(self,data_list):
if data_list:
return ','.join(data_list)
return None


def clean(self, value):
super(CaptchaField, self).clean(value)
response, value[1] = value[1].strip().lower(), ''

if not hashlib.sha1(str(response)).hexdigest() == value[0]:
raise forms.ValidationError("Sorry, you got the security question wrong - to prove you're not a spammer, please try again.")
return value
Expand Down

0 comments on commit 848c806

Please sign in to comment.