def add_benefactor_credit(request): if not request.user.is_authenticated: # TODO Raise Authentication Error context = error_context_generate('Authentication Error', 'You are not Signed In!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) if not request.user.is_active: context = error_context_generate( 'Deactivated Account Error', 'Your Account Has Been Marked as Deactivated!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) if request.user.is_charity: # TODO Raise Account Type Error context = error_context_generate( 'Account Type Error', 'I Don\'t Know How You Ended Here But Charities Cannot Add Credits!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) # try: benefactor = get_object(Benefactor, user=request.user) amount = float(request.POST.get('deposit_amount')) benefactor.credit += amount benefactor.save() # FIXME Redirect to user profile view Logger.account_update(request.user, None, None) return HttpResponseRedirect(reverse('user_profile'))
def submit_cooperation_request(request, project_id): if not request.user.is_authenticated: # TODO Raise Authentication Error context = error_context_generate('Authentication Error', 'You are not Signed In!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) try: # FIXME How should we find the project? I mean which data is given to find the project with? project = NonFinancialProject.objects.all().filter( project_id=project_id) if request.user.is_benefactor: benefactor = get_object(Benefactor, user=request.user) charity = get_object(Charity, user=get_object( User, username=request.POST.get('username'))) new_notification = Notification.objects.create( type='new_request', user=charity.user, datetime=datetime.datetime.now()) new_notification.description = 'A new Cooperation Request is Received for Project ' + project.project new_notification.save() Logger.request_submit(request.user, charity.user, project.project) request_type = 'b2c' else: benefactor = get_object(Benefactor, user=get_object( User, username=request.POST.get('username'))) charity = get_object(Charity, user=request.user) new_notification = Notification.objects.create( type='new_request', user=benefactor.user, datetime=datetime.datetime.now()) new_notification.description = 'A new Cooperation Request Has Been Received!' new_notification.save() Logger.request_submit(request.user, benefactor.user, project.project) request_type = 'c2b' new_request = CooperationRequest.objects.create( benefactor=benefactor, charity=charity, type=request_type, description=request.POST.get('description')) new_request.nonfinancialproject = project new_request.save() return HttpResponseRedirect([]) except: # TODO Raise Error context = { 'error_message': 'Unexpected Error: Some of the Data Needed for The Page is Lost or Damaged' } context = error_context_generate( 'Unexpected Error', 'Some of the Data Needed for The Page is Lost or Damaged', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request))
def deny_request(request, rid): if not request.user.is_authenticated: # TODO Raise Authentication Error context = sign_in_error() template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) if not request.user.is_active: context = error_context_generate( 'Deactivated Account Error', 'Your Account Has Been Marked as Deactivated!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) try: req = get_object(CooperationRequest, id=rid) benefactor = req.benefactor charity = req.charity if req.state is 'closed': # TODO Raise Request State Error return HttpResponse([]) if (request.user.is_benefactor and request.user != benefactor.user ) or (request.user.is_charity and request.user != charity.user): # TODO Raise Authentication Error return HttpResponse([]) project = get_object(Project, id=req.nonfinancialproject.project.id) if project.type is 'financial': # TODO Raise Project Type Error return HttpResponse([]) if request.user.is_benefactor: new_notification = Notification.objects.create( type='request_deny', user=charity.user, datetime=datetime.datetime.now()) new_notification.description = 'Your Cooperation Request for Project ' + project + ' Has Been Denied' new_notification.save() else: new_notification = Notification.objects.create( type='request_deny', user=benefactor.user, datetime=datetime.datetime.now()) new_notification.description = 'Your Cooperation Request for Project ' + project + ' Has Been Denied' new_notification.save() req.save() if request.user.is_charity: Logger.deny_request(request.user, benefactor.user, project) else: Logger.deny_request(request.user, charity.user, project) # TODO Success Redirect return HttpResponse([]) except: # TODO Raise Unexpected Error context = error_context_generate( 'Unexpected Error', 'Some Required Files are Lost or Damaged!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request))
def logout_user(request): if not request.user.is_authenticated: # TODO Raise Authentication Error context = error_context_generate('Authentication Error', 'You are not Signed In!', 'accounts:login_view') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) Logger.logout(request.user, None, None) logout(request) template = loader.get_template('accounts/login.html') return HttpResponse(template.render({}, request))
def login_user(request): # tmp_user = get_object_or_404(User,username=request.POST.get("username"),password=request.POST.get("password")) # try: if request.user.is_authenticated: Logger.logout(request.user, None, None) logout(request) tmp_user = User.objects.filter(username=request.POST.get("username")) if len(tmp_user) == 0: return render(request, 'accounts/login.html', {'error_message': 'کاربر موردنظر یافت نشد!'}) tmp_user = get_object(User, username=request.POST.get("username")) if tmp_user.password != request.POST.get("password"): return render(request, 'accounts/login.html', {'error_message': 'رمز اشتباهه -.-'}) if not tmp_user.is_active: return render(request, 'accounts/login.html', {'error_message': 'لطفاً حساب خود را تایید کنید.'}) if tmp_user.is_charity: login(request, user=tmp_user) Logger.login(request.user, None, None) return HttpResponseRedirect(reverse('accounts:charity_dashboard')) elif tmp_user.is_benefactor: login(request, tmp_user) Logger.login(request.user, None, None) return HttpResponseRedirect(reverse('accounts:benefactor_dashboard')) else: login(request, tmp_user) Logger.login(request.user, None, None) return HttpResponseRedirect(reverse('admin'))
def submit_charity_score(request, charity_username): if not request.user.is_authenticated: # TODO Raise Authentication Error context = error_context_generate('Authentication Error', ' You are not Signed In!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) if request.user.is_charity: context = error_context_generate( 'Account Type Error', 'You Can\'t Submit Score for Another Charity!', '') # TODO Raise Account Type Error template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) benefactor = get_object(Benefactor, user=request.user) if benefactor.charity_set.get(user=get_object( User, username=request.POST.get('charity_username'))).count <= 0: context = error_context_generate( 'No Cooperation Error', 'You Cannot Submit Score for a Charity with Which You Had no Cooperation!', '') # TODO Raise No_Cooperation Error template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) try: charity = get_object(Charity, user=get_object(User, username=charity_username)) score = benefactor.charityscore_set.filter(benefactor=benefactor, charity=charity).all()[0] if score is None: score = get_object(CharityScore, charity=charity, benefactor=get_object(Benefactor, user=request.user)) if float(request.POST.get('score')) > 10.0: score.score = 10.0 else: score.score = float(request.POST.get('score')) score.save() Logger.submit_score(request.user, charity.user, None) return HttpResponseRedirect([]) except: # TODO raise error context = error_context_generate( 'Unexpected Error', 'Some of the Data Needed for The Page is Lost or Damaged', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request))
def customize_user(request): if not request.user.is_authenticated: # TODO Raise Authentication Error context = error_context_generate('Authentication Error', 'لطفاً اول وارد شوید', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) if not request.user.is_active: context = error_context_generate( 'Deactivated Account Error', 'Your Account Has Been Marked as Deactivated!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) if check_valid(request.POST.get('password')): request.user.password = request.POST.get("password") if check_valid(request.POST.get('description')): request.user.description = request.POST.get("description") if check_valid(request.POST.get("province")): request.user.contact_info.province = request.POST.get("province") if check_valid(request.POST.get("city")): request.user.contact_info.city = request.POST.get("city") if check_valid(request.POST.get("address")): request.user.contact_info.address = request.POST.get("address") if check_valid(request.POST.get("phone_number")): request.user.contact_info.phone_number = request.POST.get( "phone_number") request.user.save() request.user.contact_info.save() if request.user.is_charity: if check_valid(request.POST.get("name")): request.user.charity.name = request.POST.get("name") request.user.charity.save() else: if check_valid(request.POST.get("first_name")): request.user.benefactor.first_name = request.POST.get("first_name") if check_valid(request.POST.get("last_name")): request.user.benefactor.last_name = request.POST.get("last_name") if check_valid(request.POST.get("gender")): request.user.benefactor.gender = request.POST.get("gender") if check_valid(request.POST.get("age")): request.user.benefactor.age = int(request.POST.get("age")) request.user.benefactor.save() Logger.account_update(request.user, None, None) # TODO Fix Redirect return HttpResponseRedirect(reverse('accounts:user_profile'))
def add_ability_to_benefactor(request): benefactor_id = int(request.POST.get('add_ability_benefactor_id')) if request.user.id != benefactor_id: # TODO Return Error context = error_context_generate( 'Authentication Error', 'You Don\'t Have Permission to Change this Account!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) ability_type_name = request.POST.get('add_ability_ability_type_name') ability_description = request.POST.get('add_ability_description') ability_type = AbilityType.objects.all().filter( name__iexact=ability_type_name)[0] benefactor = Benefactor.objects.all().filter(user_id=benefactor_id)[0] ability = Ability(benefactor=benefactor, ability_type=ability_type, description=ability_description) ability.save() benefactor.ability_set.add(ability) Logger.add_ability_benefactor(request.user, None, None) # TODO Fix Path return HttpResponseRedirect('path')
def submit_ability_request(request): if not request.user.is_authenticated: # TODO Raise Authentication Error context = error_context_generate('Authentication Error', 'You are not Signed In!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) try: new_request = AbilityRequest.objects.create( type=request.POST.get('type'), name=request.POST.get('name'), description=request.POST.get('description')) new_request.save() Logger.request_new_ability_type(request.user, None, None) return HttpResponseRedirect([]) except: # TODO Raise Error context = error_context_generate( 'Unexpected Error', 'Some of the Data Needed for The Page is Lost or Damaged', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request))
def submit_benefactor_score(request, ability_id): if not request.user.is_authenticated: # TODO Raise Authentication Error context = error_context_generate('Authentication Error', 'You are not Signed In!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) if request.user.is_benefactor: # TODO Raise Account Type Error context = error_context_generate( 'Account Type Error', 'You Can\'t Submit Score for Another Benefactor!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) ability = get_object(Ability, id=ability_id) if ability is None: # TODO Raise Not Found Error context = error_context_generate('Not Found', 'Requested Ability Cannot be Found', 'accounts:charity_dashboard') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) try: benefactor = ability.benefactor charity = ability.charity charity_projects = [ nf_project for nf_project in charity.project_set if nf_project.type != 'financial' ] if len([ project for project in NonFinancialProject.objects.all() if project.project in charity_projects and project.ability_type is ability.ability_type ]) <= 0: context = error_context_generate( 'No Cooperation Error', 'You Cannot Submit a Score for a Benefactor with Whom You Had no ' + 'Cooperation On This Ability Type!', 'accounts:charity_dashboard') # TODO Raise No_Cooperation Error template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) score = charity.benefactorscore_set.filter(benefactor=benefactor, charity=charity).all()[0] if score is None: score = BenefactorScore.objects.create(ability_type=ability, benefactor=benefactor, charity=get_object( Charity, user=request.user)) if float(request.POST.get('score')) > 10.0: score.score = 10.0 else: score.score = float(request.POST.get('score')) score.save() Logger.submit_score(request.user, benefactor.user, None) return HttpResponseRedirect([]) except: context = error_context_generate( 'Unexpected Error', 'Some of the Data Needed for The Page is Lost or Damaged', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request))
def signup(request): # try: test1_user = User.objects.filter(username=request.POST.get("username")) test2_user = User.objects.filter(username=request.POST.get("email")) if test1_user.__len__() != 0 and test2_user.__len__() != 0: return render( request, 'accounts/register.html', { 'error_message': 'Account already exists! Try login or forget password.' }) if test1_user.__len__() == 0 and len(test2_user) != 0: return render(request, 'accounts/register.html', {'error_message': 'Email is already taken! '}) if len(test1_user) != 0 and len(test2_user) == 0: return render(request, 'accounts/register.html', { 'error_message': 'Username is already taken! try another username. ' }) tmp_contact_info = ContactInfo.objects.create( country="ایران", province=request.POST.get("province"), city=request.POST.get("city"), address=request.POST.get("address"), postal_code=request.POST.get("postal_code"), phone_number=request.POST.get("phone_number")) tmp_user = User.objects.create(username=request.POST.get("username"), password=request.POST.get("password"), email=request.POST.get("email"), contact_info=tmp_contact_info, description=request.POST.get("description")) tmp_user.is_active = False tmp_user.save() code = generate_recover_string() message = 'برای فعال شدن حساب خود بر روی لینک زیر کلیک کنید:' + '\n' message += ip + 'activate/' + str(tmp_user.id) + '/' + code tmp_user.activation_string = code email_message = EmailMessage('Activation Email', message, to=[tmp_user.email]) email_message.send() Logger.create_account(tmp_user, None, None) if request.POST.get("account_type") == "Charity": tmp_user.is_charity = True tmp_charity = Charity.objects.create( user=tmp_user, name=request.POST.get("charity_name")) tmp_charity.save() tmp_user.save() login(request, tmp_user) Logger.login(request.user, None, None) return HttpResponseRedirect(reverse('accounts:user_profile')) else: tmp_user.is_benefactor = True age = request.POST.get('age') age = None if age is None else int(age) tmp_benefactor = Benefactor.objects.create( user=tmp_user, first_name=request.POST.get("first_name"), last_name=request.POST.get("last_name"), age=age, gender=request.POST.get('gender')) tmp_benefactor.save() tmp_user.save() login(request, tmp_user) Logger.login(request.user, None, None) template = loader.get_template('accounts/login.html') context = {'error_message': 'لطفاً ایمیل خود را تایید کنید.'} return HttpResponse(template.render(context, request))
def edit_project(request, pk): # TODO fix path template = loader.get_template('path-to-template') if not request.user.is_authenticated: context = sign_in_error() template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) if request.user.is_active: context = error_context_generate( 'Deactivated Account Error', 'Your Account Has Been Marked as Deactivated!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) if request.method != 'POST': context = error_context_generate('Connection Error', 'Request Method is not POST!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) project = get_object(Project, pk=pk) try: dic = request.POST project.project_name = dic['project_name'] project.project_state = dic['project_state'] project.description = dic['description'] if project.type is 'financial': fin_project = get_object(FinancialProject, project=project) fin_project.target_money = dic['target_money'] fin_project.start_date = convert_str_to_date(dic['start_date']) fin_project.end_date = convert_str_to_date(dic['end_date']) # FIXME should the current_money be editable or not? fin_project.current_money = dic['current_money'] fin_project.save() else: nf_project = get_object(NonFinancialProject, project=project) nf_project.ability_type = dic['ability_type'] nf_project.city = dic['city'] nf_project.country = dic['country'] nf_project.max_age = dic['max_age'] nf_project.min_age = dic['min_age'] nf_project.project = dic['province'] nf_project.required_gender = dic['required_gender'] date_interval = nf_project.dateinterval if check_valid(dic.get('start_date')): date_interval.begin_date = convert_str_to_date( dic['start_date']) date_interval.end_date = convert_str_to_date(dic['end_date']) if check_valid(dic.get('week_schedule')): date_interval.to_json( create_query_schedule(dic['week_schedule'])) nf_project.save() date_interval.save() project.save() Logger.update_project(request.user, None, project) except: context = {'pk': pk, 'error_message': 'Error in finding the Project!'} context = error_context_generate( 'Unexpected Error', 'Some of the Required Files Are Damaged or Lost!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) # FIXME maybe all responses should be Redirects / parameters need fixing return HttpResponseRedirect([])
def submit_charity_comment(request, charity_username): if not request.user.is_authenticated: # TODO Raise Authentication Error context = error_context_generate('Authentication Error', 'You are not Signed In!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) if not request.user.is_active: context = error_context_generate( 'Deactivated Account Error', 'Your Account Has Been Marked as Deactivated!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) if request.user.is_charity: # TODO Raise Account Type Error context = error_context_generate( 'Account Type Error', 'Charities Cannot Comment on Other Charities', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) if request.user.benefactor.charity_set.get( user=get_object(User, username=charity_username)).count <= 0: context = error_context_generate( 'No Cooperation Error', 'You Cannot Submit a Comment for a Charity with Which You Had no Cooperation!', '') # TODO Raise No_Cooperation Error template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) charity_users = User.objects.filter(username=charity_username) if charity_users.count() <= 0: # TODO Raise Not Found Error context = error_context_generate('Not Found', 'Requested User Cannot be Found', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) charity_user = charity_users.all()[0] try: charity = charity_user.charity comment_set = charity.charitycomment_set.filter( benefactor=request.user.benefactor, charity=charity).all() if comment_set.count() <= 0: comment = CharityComment.objects.create( commented=charity, commentor=request.user.benefactor, comment_string=request.POST.get('comment_string')) else: comment = comment_set[0] comment.comment_string = request.POST.get('comment_string') comment.save() comment = CharityComment.objects.create( commented=charity_user.charity, commentor=request.user.benefactor, comment_string=request.POST.get('comment_string')) # TODO Redirect to Charity Profile Page Logger.account_update(request.user, charity_user, None) return HttpResponseRedirect([]) except: # TODO Raise Unexpected Error context = error_context_generate('Unexpected Error', 'Error While Submitting The comment', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request))
def accept_request(request, rid): if not request.user.is_authenticated: # TODO Raise Authentication Error context = sign_in_error() template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) if not request.user.is_active: context = error_context_generate( 'Deactivated Account Error', 'Your Account Has Been Marked as Deactivated!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) try: req = get_object(CooperationRequest, id=rid) benefactor = req.benefactor charity = req.charity if req.state is 'closed': # TODO Raise Request State Error context = error_context_generate('Request Closed', 'Request is Already Closed', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) if (request.user.is_benefactor and request.user != benefactor.user ) or (request.user.is_charity and request.user != charity.user): # TODO Raise Authentication Error context = error_context_generate( 'Access Error', 'You Don\'t Have Permission to Accept This Request', '') project = get_object(Project, id=req.nonfinancialproject.project.id) if project.type is 'financial': # TODO Raise Project Type Error context = error_context_generate('Project Type Error', 'Project is Financial', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) if project.benefactors.count() > 0 or project.project_state != 'open': # TODO Raise Project Occupied Error context = error_context_generate( 'Project Occupied', 'Project is Already Taken by Another Benefactor', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) project.benefactors.add(benefactor) if len(charity.benefactor_history.filter(user=benefactor.user)) == 0: charity.benefactor_history.add(benefactor) project.project_state = 'in_progress' if request.user.is_benefactor: new_notification = Notification.objects.create( type='request_accept', user=charity.user, datetime=datetime.datetime.now()) new_notification.description = 'Your Cooperation Request for Project ' + project + ' Has Been Accepted' new_notification.save() else: new_notification = Notification.objects.create( type='request_accept', user=benefactor.user, datetime=datetime.datetime.now()) new_notification.description = 'Your Cooperation Request for Project ' + project + ' Has Been Accepted' new_notification.save() req.state = 'closed' req.save() if request.user.is_charity: Logger.accept_request(request.user, benefactor.user, project) else: Logger.accept_request(request.user, charity.user, project) # TODO Success Redirect return HttpResponse([]) except: # TODO Raise Unexpected Error return HttpResponse([])
def contribute_to_project(request, project_id): if not request.user.is_authenticated: # TODO Raise Authentication Error context = sign_in_error() template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) if not request.user.is_active: context = error_context_generate( 'Deactivated Account Error', 'Your Account Has Been Marked as Deactivated!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) if request.user.is_charity: # TODO Raise Account Type Error context = error_context_generate( 'Account Type', 'Charities Cannot Contribute to Other Charities\' Projects', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) project = get_object(Project, id=project_id) if project.type != 'financial': # TODO Raise Project Type Error context = error_context_generate('Project Type', 'Project Type is Not Financial', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) fin_project = get_object(FinancialProject, project=project) try: if project.project_state is 'completed': # TODO Raise Project Closed Error context = error_context_generate('Project Closed', 'Project is Already Closed', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) amount = float(request.POST.get('money')) benefactor = get_object(Benefactor, user=request.user) if benefactor.credit < amount: # TODO Raise Not Enough Money Error context = error_context_generate( 'Low Credits', 'Your Account\'s Credits is not Enough', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) contribution = get_object(FinancialContribution, benefactor=benefactor, financial_project=fin_project) if contribution is not None: contribution.money += amount contribution.save() else: FinancialContribution.objects.create(benefactor=benefactor, financial_project=fin_project, money=amount) benefactor.credit -= amount fin_project.add_contribution(amount) benefactor.save() new_notification = Notification.objects.create( type='project_contribution', user=project.charity.user, datetime=datetime.datetime.now()) new_notification.description = str( amount) + ' Has been Contributed to Project ' + project if fin_project.project.project_state is 'completed': new_notification.description += '\n Project Has Been Completed Successfully!' new_notification.save() Logger.financial_contribution(request.user, project.charity, project) # TODO Redirect return HttpResponseRedirect([]) except: # TODO Raise Unexpected Error context = error_context_generate( 'Unexpected Error', 'Some Required Files are Lost or Damaged!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request))
def submit_benefactor_comment(request, ability_id): if not request.user.is_authenticated: # TODO Raise Authentication Error context = error_context_generate('Authentication Error', 'You are not Signed In!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) if not request.user.is_active: context = error_context_generate( 'Deactivated Account Error', 'Your Account Has Been Marked as Deactivated!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) if request.user.is_benefactor: # TODO Raise Account Type Error context = error_context_generate( 'Account Type Error', 'Benefactors Cannot Comment on Other Benefactors', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) ability = get_object(Ability, id=ability_id) # benefactor_users = User.objects.filter(username=benefactor_username) if ability is None: # TODO Raise Not Found Error context = error_context_generate('Not Found', 'Requested Ability Cannot be Found', 'accounts:charity_dashboard') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) try: benefactor = ability.benefactor charity = ability.charity charity_projects = [ nf_project for nf_project in charity.project_set if nf_project.type != 'financial' ] if len([ project for project in NonFinancialProject.objects.all() if project.project in charity_projects and project.ability_type is ability.ability_type ]) <= 0: context = error_context_generate( 'No Cooperation Error', 'You Cannot Submit a Comment for a Benefactor with Whom You Had no ' + 'Cooperation On This Ability Type!', 'accounts:charity_dashboard') # TODO Raise No_Cooperation Error template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) comment_set = benefactor.benefactorcomment_set.filter( benefactor=benefactor, ability=ability, charity=charity).all() if comment_set.count() <= 0: comment = BenefactorComment.objects.create( commented=benefactor, commentor=request.user.charity, ability=ability, comment_string=request.POST.get('comment_string')) else: comment = comment_set[0] comment.comment_string = request.POST.get('comment_string') comment.save() # TODO Redirect to Benefactor Profile Page Logger.submit_comment(request.user, benefactor.user, None) return HttpResponseRedirect([]) except: # TODO Raise Unexpcted Error context = error_context_generate('Unexpected Error', 'Error While Submitting The Comment', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request))
def create_new_project(request): if request.method == 'GET': projects = request.user.charity.project_set return render(request, 'accounts/create-project.html', {'result_set': projects}) if request.user.is_authenticated: if not request.user.is_active: context = error_context_generate( 'Deactivated Account Error', 'Your Account Has Been Marked as Deactivated!', '') template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request)) project = Project.objects.create() project.project_name = request.POST.get('project_name') project.charity = request.user project.description = request.POST.get('description') project.state = 'open' project.save() if request.POST.get('type') is 'financial': project.type = 'financial' financial_project = FinancialProject.objects.create() if request.POST.get('start_date') is not None: financial_project.start_date = datetime.datetime.strptime( request.POST.get('start_date'), '%y-%m-%d') else: financial_project.start_date = datetime.date.today() # FIXME only date not datetime? financial_project.end_date = datetime.datetime.strptime( request.POST.get('end_date'), '%y-%m-%d') financial_project.project = project financial_project.target_money = float( request.POST.get('target_money')) if request.POST.get('current_money') is not None: financial_project.current_money = float( request.POST.get('current_money')) else: financial_project.current_money = 0 financial_project.save() else: project.type = 'non-financial' non_financial_project = NonFinancialProject.objects.create() non_financial_project.project = project if check_valid(request.POST.get("ability_type")): non_financial_project.ability_type = request.POST.get( "ability_type") if check_valid(request.POST.get("min_age")): non_financial_project.min_age = int( request.POST.get("min_age")) if check_valid(request.POST.get("max_age")): non_financial_project.max_age = int( request.POST.get("max_age")) non_financial_project.required_gender = request.POST.get( "required_gender") non_financial_project.country = request.POST.get("country") non_financial_project.province = request.POST.get("province") non_financial_project.city = request.POST.get("city") date_interval = DateInterval.objects.create() date_interval.begin_date = convert_str_to_date( request.POST.get('start_date')) date_interval.end_date = convert_str_to_date( request.POST.get('end_date')) date_interval.non_financial_project = non_financial_project # FIXME check if the input is JSON or not date_interval.to_json( create_query_schedule(request.POST.get('week_schedule'))) non_financial_project.save() projects = request.user.charity.project_set Logger.create_new_project(request.user, None, project) # TODO Fix redirect path return render(request, 'accounts/create-project.html', {'result_set': projects}) else: # TODO Fix content context = sign_in_error() template = loader.get_template('accounts/error_page.html') return HttpResponse(template.render(context, request))