From 1e4a9c0565c3d2f52ec205ae7627ebfc84278735 Mon Sep 17 00:00:00 2001 From: Alvin Li Date: Wed, 30 Oct 2013 10:16:21 +0800 Subject: * merged commits from 'maxwell lou' by time '20131028_09:52'; * moved 'ValueKind' model from 'recommend/models' to 'indicator/models' * added field 'type' for 'indicator.models.Indicator' * added field 'kind' for 'indicator.models.IndicatorRecord', 'InnateConfine' * updated methods for models 'Indicator', 'IndicatorRecord', 'InnateConfine' * updated views and templates for 'apps/indicator' * added 'INDICATOR_RECOMMEND_PERIOD' into 'settings.py' * added 'type' field for 'indicator.models.Indicator' in 'search_indexes.py' * added a confirm step for 'EditHistoryData' page when delete a record * removed dir 'backupdata'; 'queries.txt' moved to 'backup' dir * moved *.json to 'backup' dir * removed 'apps/managers*.py' * updated 'indicator/popup/IndexDesc.html' * created dir 'apps/indicator/obsolete'; * moved 'indicator/forms.py' to dir 'obsolete'; * splitted views related forms.py from 'views.py', and placed in 'obsolete/views_forms.py' * loadded newest data into database * cleaned pervious data files in 'backup' dir * updated 'README.txt'; added a solution to a redis problem * added field 'type' of 'indicator.models.Indicator'; for filtering search results and only returning 'NORMAL_TYPE' --- 97suifangqa/apps/indicator/obsolete/forms.py | 321 +++++++++++++++++++++ 97suifangqa/apps/indicator/obsolete/views_forms.py | 311 ++++++++++++++++++++ 2 files changed, 632 insertions(+) create mode 100644 97suifangqa/apps/indicator/obsolete/forms.py create mode 100644 97suifangqa/apps/indicator/obsolete/views_forms.py (limited to '97suifangqa/apps/indicator/obsolete') diff --git a/97suifangqa/apps/indicator/obsolete/forms.py b/97suifangqa/apps/indicator/obsolete/forms.py new file mode 100644 index 0000000..2e0b709 --- /dev/null +++ b/97suifangqa/apps/indicator/obsolete/forms.py @@ -0,0 +1,321 @@ +# -*- coding: utf-8 -*- + +""" +forms for apps/indicator +""" + +from django import forms +from django.utils.translation import ugettext as _ + +from indicator import models as im + +import sympy +from sympy.core.sympify import SympifyError + + +class IndicatorCategoryForm(forms.ModelForm): # {{{ + """ + form for 'models.IndicatorCategory' + """ + class Meta: + model = im.IndicatorCategory + exclude = ('addByUser',) +# }}} + + +class IndicatorForm(forms.ModelForm): # {{{ + """ + form for 'models.Indicator' + """ + class Meta: + model = im.Indicator + exclude = ('addByUser',) +# }}} + + +class UnitForm(forms.ModelForm): # {{{ + """ + form for 'models.Unit' + """ + class Meta: + model = im.Unit + exclude = ('addByUser',) + + def __init__(self, *args, **kwargs): + super(UnitForm, self).__init__(*args, **kwargs) + # store 'instance_id', for edting instance + self.instance_id = self.instance.id + + # 'clean_standard()' cannot raise Vali dationError correctly?? + # TODO: clean each field and generate errors accordingly. + + def clean(self): + cleaned_data = super(UnitForm, self).clean() + instance_id = self.instance_id + standard = cleaned_data['standard'] + indicator = cleaned_data['indicator'] + std_unit_list = indicator.get_unit(type="standard") + relation = cleaned_data.get('relation', u'') + if standard: + if std_unit_list and (instance_id != std_unit_list[0].id): + raise forms.ValidationError(_(u'标准单位已存在'), + code='standard') + cleaned_data['relation'] = u'v' + else: + try: + fsym = sympy.sympify(relation) + except SympifyError: + raise forms.ValidationError(_(u'"%(relation)s" 不是合法的表达式'), + code='relation_invalid', + params={'relation': relation}) + # always return the full collection of cleaned data + return cleaned_data +# }}} + + +class InnateConfineForm(forms.ModelForm): # {{{ + """ + form for 'models.InnateConfine' + """ + unit = forms.ModelChoiceField(label=u"标准单位", + queryset=im.Unit.objects.filter(standard=True)) + + class Meta: + model = im.InnateConfine + exclude = ('addByUser',) + + def clean(self): # {{{ + """ + check the validity of data + """ + cleaned_data = super(InnateConfineForm, self).clean() + indicator = cleaned_data['indicator'] + unit = cleaned_data.get('unit') + val_norm = cleaned_data.get('val_norm') + human_max = cleaned_data.get('human_max') + human_min = cleaned_data.get('human_min') + math_max = cleaned_data.get('math_max') + math_min = cleaned_data.get('math_min') + # check data + if indicator.dataType in [indicator.FLOAT_TYPE, + indicator.RANGE_TYPE, indicator.FLOAT_RANGE_TYPE]: + # check unit + if not (unit and unit.standard): + raise forms.ValidationError(_(u'unit 未填写/不是标准单位'), + code='unit') + if (human_max is None) or (human_min is None): + raise forms.ValidationError(_(u'human_max/human_min 未填写'), + code='human_empty') + if (human_max <= human_min): + raise forms.ValidationError(_(u'human_max <= human_min'), + code='human_relation') + # check 'math_max' and 'math_min' + if (math_max is None) or (math_min is None): + raise forms.ValidationError(_(u'math_max/math_min 未填写'), + code='math_empty') + if (math_max <= math_min): + raise forms.ValidationError(_(u'math_max <= math_min'), + code='math_relation') + # compare 'human*' and 'math*' + if (human_max > math_max) or (human_min < math_min): + raise forms.ValidationError(_(u'Error: human_max>math_max / human_min math_max): + raise forms.ValidationError(_(u'value(std) < math_min or value(std) > math_max'), + code='value_std_relation') + # val_max + if val_max is not None: + # unit conversion + try: + val_max_std = float(rel_sym.evalf( + subs={v: val_max})) + except ValueError: + raise forms.ValidationError(_(u'"%s" 求值错误,请检查只有变量"v"' % unit_rel), + code='val_max_evalf') + if (val_max_std <= math_min) or ( + val_max_std > math_max): + raise forms.ValidationError(_(u'val_max(std) <= math_min or val_max(std) > math_max'), + code='val_max_std_relation') + # val_min + if val_min is not None: + try: + val_min_std = float(rel_sym.evalf( + subs={v: val_min})) + except ValueError: + raise forms.ValidationError(_(u'"%s" 求值错误,请检查只有变量"v"' % unit_rel), + code='val_min_evalf') + if (val_min_std < math_min) or ( + val_min_std >= math_max): + raise forms.ValidationError(_(u'val_min(std) < math_min or val_min(std) >= math_max'), + code='val_min_std_relation') + # }}} + # return cleaned data + return cleaned_data + # }}} +# }}} + + +class RecordHistoryForm(forms.ModelForm): # {{{ + """ + form for 'models.RecordHistory' + """ + class Meta: + model = im.RecordHistory + exclude = ('indicatorRecord',) +# }}} + + +# vim: set ts=4 sw=4 tw=0 fenc=utf-8 ft=python.django: # diff --git a/97suifangqa/apps/indicator/obsolete/views_forms.py b/97suifangqa/apps/indicator/obsolete/views_forms.py new file mode 100644 index 0000000..89af21f --- /dev/null +++ b/97suifangqa/apps/indicator/obsolete/views_forms.py @@ -0,0 +1,311 @@ +# -*- coding: utf-8 -*- + +""" +views for used with forms.py + +splitted from 'views.py' +""" + +from indicator.forms import * + +########################################################### +###### forms ###### + +## add_edit_category # {{{ +@login_required +def add_edit_category(request, category_id=None, template='indicator/simple.html'): + """ + add/edit category: 'models.IndicatorCategory' + for 'staff' or 'normal user' + """ + # get or create model instance + if category_id: + category_id = int(category_id) + category = get_object_or_404(im.IndicatorCategory, + id=category_id) + action = 'Edit' + # check the user + # 'staff' can edit all data; + # normal users can only edit their own. + if category.addByUser != request.user and ( + not request.user.is_staff): + return HttpResponseForbidden() + else: + category = im.IndicatorCategory(addByUser=request.user) + action = 'Add' + + if request.method == 'POST': + form = IndicatorCategoryForm(request.POST, instance=category) + if form.is_valid(): + # form posted and valid + # save form to create/update the model instance + form.save() + # redirect url, avoid page reload/refresh + return HttpResponseRedirect('/indicator/done/') + else: + # form with data of the specified instance + form = IndicatorCategoryForm(instance=category) + + return render(request, template, { + 'object': 'IndicatorCategory', + 'action': action, + 'form': form, + }) +# }}} + + +# add_edit_indicator # {{{ +@login_required +def add_edit_indicator(request, indicator_id=None, template='indicator/simple.html'): + """ + add/edit indicator: 'models.Indicator' + for 'staff' or 'normal user' + """ + if indicator_id: + indicator_id = int(indicator_id) + indicator = get_object_or_404(im.Indicator, + id=indicator_id) + action = 'Edit' + # check the user + # 'staff' can edit all data; + # normal users can only edit their own. + if indicator.addByUser != request.user and ( + not request.user.is_staff): + return HttpResponseForbidden() + else: + indicator = im.Indicator(addByUser=request.user) + action = 'Add' + + if request.method == 'POST': + form = IndicatorForm(request.POST, instance=indicator) + if form.is_valid(): + # form posted and valid + form.save() + # redirect url, avoid page reload/refresh + return HttpResponseRedirect('/indicator/done/') + else: + # form with instance + form = IndicatorForm(instance=indicator) + + return render(request, template, { + 'object': 'Indicator', + 'action': action, + 'form': form, + }) +# }}} + + +## add_edit_unit {{{ +@login_required +def add_edit_unit(request, unit_id=None, template='indicator/simple.html'): + """ + add unit for indicator + """ + if unit_id: + unit_id = int(unit_id) + unit = get_object_or_404(im.Unit, id=unit_id) + action = 'Edit' + # check the user + # 'staff' can edit all data; + # normal users can only edit their own. + if unit.addByUser != request.user and ( + not request.user.is_staff): + return HttpResponseForbidden() + else: + unit = im.Unit(addByUser=request.user) + action = 'Add' + + if request.method == 'POST': + form = UnitForm(request.POST, instance=unit) + if form.is_valid(): + # form posted and valid + form.save() + # redirect url, avoid page reload/refresh + return HttpResponseRedirect('/indicator/done/') + else: + # form with instance + form = UnitForm(instance=unit) + + return render(request, template, { + 'object': 'Unit', + 'action': action, + 'form': form, + }) +# }}} + + +## add_edit_confine {{{ +@login_required +def add_edit_confine(request, confine_id=None, template='indicator/simple.html'): + """ + InnateConfine + add confines for indicator + """ + if confine_id: + confine_id = int(confine_id) + confine = get_object_or_404(im.InnateConfine, id=confine_id) + action = 'Edit' + # check the user + # 'staff' can edit all data; + # normal users can only edit their own. + if confine.addByUser != request.user and ( + not request.user.is_staff): + return HttpResponseForbidden() + else: + confine = im.InnateConfine(addByUser=request.user) + action = 'Add' + + if request.method == 'POST': + form = InnateConfineForm(request.POST, instance=confine) + if form.is_valid(): + # form posted and valid + form.save() + # redirect url, avoid page reload/refresh + return HttpResponseRedirect('/indicator/done/') + else: + # form with instance + form = InnateConfineForm(instance=confine) + + return render(request, template, { + 'object': 'InnateConfine', + 'action': action, + 'form': form, + }) +# }}} + + +## add_edit_record {{{ +@login_required +def add_edit_record(request, record_id=None, template='indicator/simple.html'): + """ + add/edit 'IndicatorRecord' + + staff 能自由地修改所有的记录,并且无需填写"修改原因"; + 普通用户只能修改自己的记录,而且必须填写"修改原因" -> RecordHistory + + TODO: + * 当用户选择好"indicator"后,重新筛选"unit",只提供与"indicator" + 对应的"unit"供选择; + * 对"普通用户"增加限制,修改数据"记录"时必须同时提交"修改原因", + 对应模型"RecordHistory"。 + """ + if record_id: + record_id = int(record_id) + record = get_object_or_404(im.IndicatorRecord, id=record_id) + action = 'Edit' + # check the user + if request.user.is_staff: + # 'staff' can edit all data; + pass + elif request.user == record.user: + # user modify the record + return HttpResponse("Not finished yet ...") + #return modify_record(request, record_id) + else: + return HttpResponseForbidden() + else: + record = im.IndicatorRecord(user=request.user) + action = 'Add' + + if request.method == 'POST': + form = IndicatorRecordForm(request.POST, instance=record) + if form.is_valid(): + #raise ValueError + form.save() + # redirect url, avoid page reload/refresh + return HttpResponseRedirect('/indicator/done/') + else: + # form with instance + form = IndicatorRecordForm(instance=record) + + return render(request, template, { + 'object': 'IndicatorRecord', + 'action': action, + 'form': form, + }) +# }}} + + +## modify_record {{{ +@login_required +def modify_record(request, record_id=None, template='indicator/simple.html'): + """ + modify an existing IndicatorRecord + + TODO: + a new 'RecordHistory' is added to record the modification reason + and backup the original data + """ + if record_id: + record_id = int(record_id) + record = get_object_or_404(im.IndicatorRecord, id=record_id) + action = 'Edit' + # check the user + if request.user.is_staff: + # 'staff' can edit all data; + return add_edit_record(request, record_id) + elif request.user == record.user: + # user modify the record + action = 'Modify' + pass + else: + return HttpResponseForbidden() + else: + return add_edit_record(request) + + if request.method == 'POST': + form = IndicatorRecordForm(request.POST, instance=record) + if form.is_valid(): + # form posted and valid + # TODO + raise ValueError(u"该功能尚未完整实现") + form.save() + # redirect url, avoid page reload/refresh + return HttpResponseRedirect('/indicator/done/') + else: + # form with instance + form = IndicatorRecordForm(instance=record) + + return render(request, template, { + 'object': 'IndicatorRecord', + 'action': action, + 'form': form, + }) +## }}} + + +## add_recordhistory_view {{{ +@login_required +def add_recordhistory_view(request, record_id, template='indicator/simple.html'): + """ + add 'RecordHistory' for a record by given + + 'staff' should use the 'admin' interface. + """ + record_id = int(record_id) + record = get_object_or_404(im.IndicatorRecord, id=record_id) + recordhistory = im.RecordHistory(indicatorRecord=record) + action = 'Add' + # check the user + if request.user != record.user: + return HttpResponseForbidden() + + if request.method == 'POST': + form = RecordHistoryForm(request.POST, instance=recordhistory) + if form.is_valid(): + # form posted and valid + form.save() + # redirect url, avoid page reload/refresh + return HttpResponseRedirect('/indicator/done/') + else: + # form with instance + form = RecordHistoryForm(instance=recordhistory) + + return render(request, template, { + 'object': 'RecordHistory', + 'action': action, + 'form': form, + }) +# }}} + + -- cgit v1.2.2