def plantshare_view(request): """View for the main PlantShare page.""" MAX_RECENTLY_ANSWERED_QUESTIONS = 3 questions = _get_recently_answered_questions( MAX_RECENTLY_ANSWERED_QUESTIONS) prior_signup_detected = request.COOKIES.get('registration_complete', False) avatar_info = UserProfile.default_avatar_image() if request.user.is_authenticated(): try: profile = UserProfile.objects.get(user=request.user) avatar_info = profile.private_avatar_image() except UserProfile.DoesNotExist: avatar_info = UserProfile.default_avatar_image() return render_to_response('plantshare.html', { 'prior_signup_detected': prior_signup_detected, 'avatar': avatar_info, 'map': SIGHTINGS_MAP_DEFAULTS, 'questions': questions, 'max_questions': MAX_RECENTLY_ANSWERED_QUESTIONS }, context_instance=RequestContext(request))
def ajax_image_upload(request): """ Ajax form submission of image upload form """ if not request.user.is_authenticated(): return HttpResponse(simplejson.dumps({ 'error': True, 'info': 'Authentication error' }), mimetype='application/json') response = { 'success': True } if request.method == 'POST': form = ScreenedImageForm(request.POST, request.FILES) if form.is_valid(): new_image = form.save(commit=False) new_image.uploaded_by = request.user new_image.save() if new_image.image_type == 'AVATAR': # Since we're technically editing the user's profile by # uploading an avatar, create a user profile if they don't # have one. Otherwise, this avatar image ends up in limbo. profile, created = None, False try: profile = UserProfile.objects.get(user=request.user) except UserProfile.DoesNotExist: profile = UserProfile() profile.display_name = request.user.username if not created: # Flag all previous, unscreened avatars as "orphaned," # since the user has essentially changed his mind and # uploaded this new image. previous_avatars = ScreenedImage.objects.filter( image_type='AVATAR', uploaded_by=request.user, screened__isnull=True, deleted=False, orphaned=False, uploaded__lt=new_image.uploaded ) previous_avatars.update(orphaned=True) response.update({ 'id': new_image.pk, 'thumb': new_image.thumb.url, 'url': new_image.image.url, }) return HttpResponse(simplejson.dumps(response), mimetype='application/json')
def avatar(self): if self.instance.pk: avatar_info = self.instance.private_avatar_image() else: avatar_info = UserProfile.default_avatar_image() return avatar_info