def edit_image(request, challenge_id): """ Allows the owner of a challenge to change the related image :param request: any request :param challenge_id: a valid ID of a Challenge object :return: a simple form page """ challenge = Challenge.objects.get(id=challenge_id) form = ChallengeImageEditForm() upload_url = blobstore.create_upload_url('/challenges/edit_image/%s' % challenge_id) if request.method == 'POST': form = ChallengeConfigureForm(request.POST) if form.is_valid(): uploaded_file = None if request.FILES: upload_form = UploadForm(request.POST, request.FILES) uploaded_file = upload_form.save() challenge.image = uploaded_file redirect_url = '/fan' if request.is_ajax(): return HttpResponse(redirect_url) return redirect(redirect_url) if request.is_ajax(): return HttpResponse("%s|%s" % ( blobstore.create_upload_url(upload_url), '<br/>'.join(['<br/>'.join([_e for _e in e]) for e in form.errors.values()]))) return render(request, 'spudderspuds/challenges/pages/edit_image.html', { 'upload_url': upload_url, 'form': form, 'image_url': '/file/serve/%s' % challenge.image if challenge.image else None, 'challenge_name': challenge.name })
def fan_profile_edit(request, page_id): fan_page = get_object_or_404(FanPage, pk=page_id) profile_form = FanPageForm(initial=fan_page.__dict__, image=fan_page.avatar) social_accounts_form = BasicSocialMediaForm(initial=fan_page.__dict__) if request.method == 'POST': profile_form = FanPageForm(request.POST, image=fan_page.avatar) social_accounts_form = BasicSocialMediaForm(request.POST) if profile_form.is_valid() and social_accounts_form.is_valid(): for attr in ('name', 'date_of_birth', 'state', ): fan_page.__setattr__(attr, profile_form.cleaned_data[attr]) set_social_media(fan_page, social_accounts_form) upload_form = UploadForm(request.POST, request.FILES) if upload_form.is_valid(): fan_page.avatar = upload_form.save() fan_page.save() redirect_to = request.session.pop('redirect_after_auth', '/fan/%s' % fan_page.id) return redirect(redirect_to) return render(request, 'spudderspuds/fans/pages/fan_page_edit.html', { 'profile_form': profile_form, 'social_accounts_form': social_accounts_form, 'page': fan_page, 'new_registration': request.GET.get('new_registration', False), 'upload_url': blobstore.create_upload_url('/fan/%s/edit' % page_id) })
def create_challenge_set_donation(request, template_id, state, club_id, club_class): club = get_object_or_404(club_class, id=club_id) template = get_object_or_404(ChallengeTemplate, id=template_id) form = ChallengeConfigureForm() upload_url = blobstore.create_upload_url('/challenges/create/%s/%s/%s/%s' % ( template_id, state, 'o' if club_class == Club else 't', club_id)) if request.method == 'POST': form = ChallengeConfigureForm(request.POST) if form.is_valid(): uploaded_file = None if request.FILES: upload_form = UploadForm(request.POST, request.FILES) uploaded_file = upload_form.save() challenge = _create_challenge(club_class, club_id, form, request, template, image=uploaded_file) redirect_url = '/challenges/%s/share' % challenge.id if request.is_ajax(): return HttpResponse(redirect_url) return redirect(redirect_url) if request.is_ajax(): return HttpResponse("%s|%s" % ( blobstore.create_upload_url(upload_url), '<br/>'.join(['<br/>'.join([_e for _e in e]) for e in form.errors.values()]))) template_data = { 'club': club, 'club_class': club_class.__name__, 'form': form, 'state': state, 'template': template, 'upload_url': upload_url} return render( request, 'spudderspuds/challenges/pages/create_challenge_configure.html', template_data)
def edit_team_page(request, page_id): team_page = TeamPage.objects.select_related('image').get(pk=page_id) form = EditTeamForm(initial=team_page.__dict__, image=team_page.image) if request.method == 'POST': form = EditTeamForm(request.POST, team_id=team_page.id, image=team_page.image) if form.is_valid(): upload_form = UploadForm(request.POST, request.FILES) uploaded_file_model = None if upload_form.is_valid(): uploaded_file_model = upload_form.save() data = form.cleaned_data team_page.name = data.get('name') team_page.contact_details = data.get('contact_details') team_page.free_text = data.get('free_text') if uploaded_file_model: team_page.image = uploaded_file_model team_page.save() return HttpResponseRedirect('/team/%s' % page_id) return render( request, 'spudderspuds/teams/pages/edit_team.html', { 'page': team_page, 'form': form, 'upload_url': blobstore.create_upload_url( '/team/%s/edit' % page_id) })
def edit_team_page(request, page_id): team_page = TeamPage.objects.select_related('image').get(pk=page_id) form = EditTeamForm(initial=team_page.__dict__, image=team_page.image) if request.method == 'POST': form = EditTeamForm(request.POST, team_id=team_page.id, image=team_page.image) if form.is_valid(): upload_form = UploadForm(request.POST, request.FILES) uploaded_file_model = None if upload_form.is_valid(): uploaded_file_model = upload_form.save() data = form.cleaned_data team_page.name = data.get('name') team_page.contact_details = data.get('contact_details') team_page.free_text = data.get('free_text') if uploaded_file_model: team_page.image = uploaded_file_model team_page.save() return HttpResponseRedirect('/team/%s' % page_id) return render(request, 'spudderspuds/teams/pages/edit_team.html', { 'page': team_page, 'form': form, 'upload_url': blobstore.create_upload_url('/team/%s/edit' % page_id) })
def fan_profile_edit(request, page_id): fan_page = get_object_or_404(FanPage, pk=page_id) profile_form = FanPageForm(initial=fan_page.__dict__, image=fan_page.avatar) social_accounts_form = BasicSocialMediaForm(initial=fan_page.__dict__) if request.method == 'POST': profile_form = FanPageForm(request.POST, image=fan_page.avatar) social_accounts_form = BasicSocialMediaForm(request.POST) if profile_form.is_valid() and social_accounts_form.is_valid(): for attr in ( 'name', 'date_of_birth', 'state', ): fan_page.__setattr__(attr, profile_form.cleaned_data[attr]) set_social_media(fan_page, social_accounts_form) upload_form = UploadForm(request.POST, request.FILES) if upload_form.is_valid(): fan_page.avatar = upload_form.save() fan_page.save() redirect_to = request.session.pop('redirect_after_auth', '/fan/%s' % fan_page.id) return redirect(redirect_to) return render( request, 'spudderspuds/fans/pages/fan_page_edit.html', { 'profile_form': profile_form, 'social_accounts_form': social_accounts_form, 'page': fan_page, 'new_registration': request.GET.get('new_registration', False), 'upload_url': blobstore.create_upload_url('/fan/%s/edit' % page_id) })
def dashboard(request): imported_clubs_count = Club.objects.filter(original_domain_name='leagueathletics').count() if not imported_clubs_count: imported_clubs_count = 0 upload_url = blobstore.create_upload_url('/spudderadmin/leagueathletics') error_message = None la_import = LeagueAthleticsImport.load() import_form = LeagueAthleticsImportClubsForm(initial={'action': LeagueAthleticsFormAction.IMPORT_CLUBS}) reset_form = LeagueAthleticsResetClubsForm(initial={'action': LeagueAthleticsFormAction.RESET_CLUBS}) if request.method == "POST": action = request.POST.get('action') if action == LeagueAthleticsFormAction.IMPORT_CLUBS: import_form = LeagueAthleticsImportClubsForm(request.POST) if import_form.is_valid(): upload_form = UploadForm(request.POST, request.FILES) uploaded_file = upload_form.save(False) uploaded_file.owner = request.user uploaded_file.content_type = request.FILES['file'].content_type uploaded_file.filename = request.FILES['file'].name uploaded_file.save() la_import.clear() la_import.in_progress = True la_import.zip_codes_file = uploaded_file la_import.save() trigger_backend_task('/spudderadmin/leagueathletics/import_clubs', eta=datetime.datetime.now() + datetime.timedelta(seconds=30)) messages.success(request, "<i class='fa fa-check'></i> Clubs import in progress") return redirect('/spudderadmin/leagueathletics') if action == LeagueAthleticsFormAction.RESET_CLUBS: reset_form = LeagueAthleticsResetClubsForm(request.POST) if reset_form.is_valid(): Club.objects.filter(original_domain_name='leagueathletics').delete() messages.success(request, "<i class='fa fa-check'></i> All imported clubs have been deleted") return redirect('/spudderadmin/leagueathletics') return render(request, 'spudderadmin/pages/leagueathletics/dashboard.html', { 'imported_clubs_count': imported_clubs_count, 'upload_url': upload_url, 'error_message': error_message, 'la_import': la_import, 'reset_form': reset_form, 'import_form': import_form })
def _state_engine_process_upload(request, challenge, engine, state, template_data): response = None template = challenge.template beneficiary = EntityController.GetWrappedEntityByTypeAndId( challenge.recipient_entity_type, challenge.recipient_entity_id, EntityBase.EntityWrapperByEntityType(challenge.recipient_entity_type)) participation = ChallengeParticipation.objects.get( challenge=challenge, participating_entity_id=request.current_role.entity.id, participating_entity_type=request.current_role.entity_type) redirect_url = '/challenges/%s/%s/4?just_pledged=True' % (challenge.id, engine) action_upload_image = 'upload_image' image_form = UploadImageForm(initial={'action': action_upload_image}) upload_url = '/challenges/%s/accept' % challenge.id if request.method == 'POST': action = request.POST.get('action') if action == action_upload_image: if request.FILES: upload_form = UploadForm(request.POST, request.FILES) file = upload_form.save() participation.image = file participation.state = ChallengeParticipation.ACCEPTED_STATE participation.state_engine = engine participation.save() if feature_is_enabled('tracking_pixels'): EventController.RegisterEvent( request, EventController.CHALLENGE_ACCEPTED) if request.is_ajax(): response = HttpResponse(redirect_url) return response, state response = redirect(redirect_url) return response, state if request.is_ajax(): response = HttpResponse( "%s|%s" % (blobstore.create_upload_url(upload_url), '<br/>'.join([ '<br/>'.join([_e for _e in e]) for e in image_form.errors.values() ]))) return response, state template_data['template'] = template template_data['beneficiary'] = beneficiary template_data['participation'] = participation template_data['redirect_url'] = redirect_url template_data['upload_url'] = blobstore.create_upload_url(upload_url) response = render( request, 'spudderspuds/challenges/pages_ajax/challenge_accept_upload.html', template_data) return response, state
def croppic_upload_endpoint(request): json_dict = {'status': 'success'} files_dict = {'file': [request.FILES['img']]} FILES = MultiValueDict(files_dict) form = UploadForm(request.POST, FILES) model = form.save(False) model.user = request.user model.content_type = FILES['file'].content_type model.save() blob_key = str(model.file.file.blobstore_info.key()) i = images.Image(blob_key=blob_key) i.rotate(0) i.execute_transforms() json_dict['width'] = i.width / 2 json_dict['height'] = i.height / 2 json_dict['url'] = ('/file/serve/%s' % model.pk) return HttpResponse(simplejson.dumps(json_dict))
def upload_image_endpoint(request): json_dict = {'uploaded_files': []} width = request.POST.get('width', False) height = request.POST.get('height', False) for i, _ in enumerate(request.FILES): files_dict = {} files_dict['file'] = [request.FILES['file-%s' % i]] FILES = MultiValueDict(files_dict) form = UploadForm(request.POST, FILES) model = form.save(False) if width and height: model.file = resize_image(model.file.file, int(width), int(height)) model.user = request.user model.content_type = FILES['file'].content_type model.save() json_dict['uploaded_files'].append('/file/serve/%s' % model.pk) return HttpResponse(simplejson.dumps(json_dict))
def upload_image_endpoint(request): json_dict = { 'uploaded_files' : [] } width = request.POST.get('width', False) height = request.POST.get('height', False) for i, _ in enumerate(request.FILES): files_dict = {} files_dict['file'] = [request.FILES['file-%s' % i]] FILES = MultiValueDict(files_dict) form = UploadForm(request.POST, FILES) model = form.save(False) if width and height: model.file = resize_image(model.file.file, int(width), int(height)) model.user = request.user model.content_type = FILES['file'].content_type model.save() json_dict['uploaded_files'].append('/file/serve/%s' % model.pk) return HttpResponse(simplejson.dumps(json_dict))
def create_challenge_set_donation(request, template_id, state, club_id, club_class): club = get_object_or_404(club_class, id=club_id) template = get_object_or_404(ChallengeTemplate, id=template_id) form = ChallengeConfigureForm() upload_url = blobstore.create_upload_url( '/challenges/create/%s/%s/%s/%s' % (template_id, state, 'o' if club_class == Club else 't', club_id)) if request.method == 'POST': form = ChallengeConfigureForm(request.POST) if form.is_valid(): uploaded_file = None if request.FILES: upload_form = UploadForm(request.POST, request.FILES) uploaded_file = upload_form.save() challenge = _create_challenge(club_class, club_id, form, request, template, image=uploaded_file) redirect_url = '/challenges/%s/share' % challenge.id if request.is_ajax(): return HttpResponse(redirect_url) return redirect(redirect_url) if request.is_ajax(): return HttpResponse( "%s|%s" % (blobstore.create_upload_url(upload_url), '<br/>'.join([ '<br/>'.join([_e for _e in e]) for e in form.errors.values() ]))) template_data = { 'club': club, 'club_class': club_class.__name__, 'form': form, 'state': state, 'template': template, 'upload_url': upload_url } return render( request, 'spudderspuds/challenges/pages/create_challenge_configure.html', template_data)
def edit_image(request, challenge_id): """ Allows the owner of a challenge to change the related image :param request: any request :param challenge_id: a valid ID of a Challenge object :return: a simple form page """ challenge = Challenge.objects.get(id=challenge_id) form = ChallengeImageEditForm() upload_url = blobstore.create_upload_url('/challenges/edit_image/%s' % challenge_id) if request.method == 'POST': form = ChallengeConfigureForm(request.POST) if form.is_valid(): uploaded_file = None if request.FILES: upload_form = UploadForm(request.POST, request.FILES) uploaded_file = upload_form.save() challenge.image = uploaded_file redirect_url = '/fan' if request.is_ajax(): return HttpResponse(redirect_url) return redirect(redirect_url) if request.is_ajax(): return HttpResponse( "%s|%s" % (blobstore.create_upload_url(upload_url), '<br/>'.join([ '<br/>'.join([_e for _e in e]) for e in form.errors.values() ]))) return render( request, 'spudderspuds/challenges/pages/edit_image.html', { 'upload_url': upload_url, 'form': form, 'image_url': '/file/serve/%s' % challenge.image if challenge.image else None, 'challenge_name': challenge.name })
def _state_engine_process_upload(request, challenge, engine, state, template_data): response = None template = challenge.template beneficiary = EntityController.GetWrappedEntityByTypeAndId( challenge.recipient_entity_type, challenge.recipient_entity_id, EntityBase.EntityWrapperByEntityType(challenge.recipient_entity_type)) participation = ChallengeParticipation.objects.get( challenge=challenge, participating_entity_id=request.current_role.entity.id, participating_entity_type=request.current_role.entity_type) redirect_url = '/challenges/%s/%s/4?just_pledged=True' % (challenge.id, engine) action_upload_image = 'upload_image' image_form = UploadImageForm(initial={'action': action_upload_image}) upload_url = '/challenges/%s/accept' % challenge.id if request.method == 'POST': action = request.POST.get('action') if action == action_upload_image: if request.FILES: upload_form = UploadForm(request.POST, request.FILES) file = upload_form.save() participation.image = file participation.state = ChallengeParticipation.ACCEPTED_STATE participation.state_engine = engine participation.save() if feature_is_enabled('tracking_pixels'): EventController.RegisterEvent(request, EventController.CHALLENGE_ACCEPTED) if request.is_ajax(): response = HttpResponse(redirect_url) return response, state response = redirect(redirect_url) return response, state if request.is_ajax(): response = HttpResponse("%s|%s" % (blobstore.create_upload_url(upload_url), '<br/>'.join( ['<br/>'.join([_e for _e in e]) for e in image_form.errors.values()]))) return response, state template_data['template'] = template template_data['beneficiary'] = beneficiary template_data['participation'] = participation template_data['redirect_url'] = redirect_url template_data['upload_url'] = blobstore.create_upload_url(upload_url) response = render(request, 'spudderspuds/challenges/pages_ajax/challenge_accept_upload.html', template_data) return response, state
def dashboard(request): imported_clubs_count = Club.objects.filter( original_domain_name='leagueathletics').count() if not imported_clubs_count: imported_clubs_count = 0 upload_url = blobstore.create_upload_url('/spudderadmin/leagueathletics') error_message = None la_import = LeagueAthleticsImport.load() import_form = LeagueAthleticsImportClubsForm( initial={'action': LeagueAthleticsFormAction.IMPORT_CLUBS}) reset_form = LeagueAthleticsResetClubsForm( initial={'action': LeagueAthleticsFormAction.RESET_CLUBS}) if request.method == "POST": action = request.POST.get('action') if action == LeagueAthleticsFormAction.IMPORT_CLUBS: import_form = LeagueAthleticsImportClubsForm(request.POST) if import_form.is_valid(): upload_form = UploadForm(request.POST, request.FILES) uploaded_file = upload_form.save(False) uploaded_file.owner = request.user uploaded_file.content_type = request.FILES['file'].content_type uploaded_file.filename = request.FILES['file'].name uploaded_file.save() la_import.clear() la_import.in_progress = True la_import.zip_codes_file = uploaded_file la_import.save() trigger_backend_task( '/spudderadmin/leagueathletics/import_clubs', eta=datetime.datetime.now() + datetime.timedelta(seconds=30)) messages.success( request, "<i class='fa fa-check'></i> Clubs import in progress") return redirect('/spudderadmin/leagueathletics') if action == LeagueAthleticsFormAction.RESET_CLUBS: reset_form = LeagueAthleticsResetClubsForm(request.POST) if reset_form.is_valid(): Club.objects.filter( original_domain_name='leagueathletics').delete() messages.success( request, "<i class='fa fa-check'></i> All imported clubs have been deleted" ) return redirect('/spudderadmin/leagueathletics') return render( request, 'spudderadmin/pages/leagueathletics/dashboard.html', { 'imported_clubs_count': imported_clubs_count, 'upload_url': upload_url, 'error_message': error_message, 'la_import': la_import, 'reset_form': reset_form, 'import_form': import_form })