def view( request, project_id ): project = project_api.get_project(project_api.id2uri(project_id)) project = fetch_resources(project) badge = badge_api.get_badge(project['badge_uri']) badge = fetch_badge_resources(badge) feedback = project_api.get_project_feedback(project['uri']) can_revise = False can_give_feedback = False if request.session.get('user'): user_uri = request.session['user']['uri'] if user_uri == project['author_uri']: can_revise = project_api.can_revise_project(project['uri']) can_give_feedback = project_api.ready_for_feedback(project['uri']) can_give_feedback &= user_uri in badge_api.get_badge_experts(badge['uri']) context = { 'project': project, 'badge': badge, 'feedback': feedback, 'can_revise': can_revise, 'can_give_feedback': can_give_feedback } return render_to_response( 'project/view.html', context, context_instance=RequestContext(request) )
def revise(request, project_id): project = project_api.get_project(project_api.id2uri(project_id)) badge = badge_api.get_badge(project['badge_uri']) fetch_badge_resources(badge) if request.method == 'POST': form = RevisionForm(request.POST) else: form = RevisionForm() if form.is_valid(): try: project_api.revise_project( project['uri'], form.cleaned_data['improvement'], form.cleaned_data.get('work_url', None) ) except Exception as e: messages.error(request, _(e.args[0])) return http.HttpResponseRedirect(reverse('project_view', args=(project_id,))) context = { 'badge': badge, 'project': project, 'form': form } return render_to_response( 'project/revise.html', context, context_instance=RequestContext(request) )
def create( request, badge_id ): badge = badge_api.get_badge(badge_api.id2uri(badge_id)) fetch_badge_resources(badge) context = { 'badge': badge } user_uri = request.session['user']['uri'] if request.method == 'POST': form = ProjectForm(request.POST, request.FILES) else: form = ProjectForm() if form.is_valid(): image = media_api.upload_image(request.FILES['image'], user_uri) try: project = project_api.create_project( badge['uri'], user_uri, form.cleaned_data['title'], image['uri'], form.cleaned_data['work_url'], form.cleaned_data['description'], form.cleaned_data['reflection'], form.cleaned_data['tags'] ) return http.HttpResponseRedirect(reverse('project_view', args=(project['id'],))) except project_api.MultipleProjectError: messages.error(request, _('You have already submitted a project for this badge.')) context['form'] = form return render_to_response( 'project/create.html', context, context_instance=RequestContext(request) )
def create_project(badge_uri, author_uri, title, image_uri, work_url, description, reflection, tags): if Project.objects.filter(author_uri=author_uri, badge_uri=badge_uri, date_deleted__isnull=True).exists(): raise MultipleProjectError('A user can only submit 1 project for a badge') badge = badge_api.get_badge(badge_uri) if author_uri in badge_api.get_badge_experts(badge_uri): raise Exception(u'Badge {0} already awarded to user'.format(badge_uri)) if isinstance(tags, list): tags = ','.join(tags) project_db = Project( title=title, image_uri=image_uri, work_url=work_url, description=description, reflection=reflection, tags=tags, badge_uri=badge_uri, author_uri=author_uri, date_created=datetime.utcnow(), date_updated=datetime.utcnow() ) project_db.save() project = get_project(id2uri(project_db.id)) send_project_creation_notification(project) experts = badge_api.get_badge_experts(project['badge_uri']) send_project_creation_expert_notification(project, badge, experts) return project
def preview( request, badge_id ): context = { 'badge': badge_api.get_badge(badge_api.id2uri(badge_id)) } fetch_badge_resources(context['badge']) return render_to_response( 'badge/preview.html', context, context_instance=RequestContext(request) )
def test_update_badge(self): """ Test that we can update a badge """ badge = badge_api.create_badge(*self.badge_values) attrs = self.badge_attrs del attrs[1] kwargs = dict(zip(self.badge_attrs, [badge['uri']] + self.badge_values)) del kwargs['author_uri'] kwargs['title'] = 'A new title' badge_api.update_badge(**kwargs) badge2 = badge_api.get_badge(badge['uri']) self.assertNotEquals(badge, badge2)
def fetch_resources(project, feedback_list=None): project['image'] = get_image(project['image_uri']) project['author'] = get_user(project['author_uri']) project['badge'] = get_badge(project['badge_uri']) fetch_badge_resources(project['badge']) if feedback_list: add_feedback_to_project(feedback_list, project) return project
def test_create_and_get_badge(self): #Test that we can create a badge badge = badge_api.create_badge(**self.BADGE_MOCK_ATTRIBUTES) # test make sure attributes are equal badge2 = badge_api.get_badge(badge['uri']) self.assertEqual(badge, badge2) # test that the badge shows up in drafts badges = badge_api.get_user_draft_badges(badge['author_uri']) self.assertEquals(len(badges), 1) self.assertEquals(badge2, badges[0])
def edit(request, badge_id): user_uri = request.session['user']['uri'] user = p2pu_user_api.get_user(user_uri) user_partner = user['partner'] badge = badge_api.get_badge(badge_api.id2uri(badge_id)) if not user_uri == badge['author_uri']: messages.error(request, _('You cannot edit someone elses badge!')) return http.HttpResponseRedirect(reverse( 'badge_preview', args=(badge_id,) )) if request.method == 'POST': form = BadgeForm(request.POST, request.FILES, user_uri=user_uri, editing=True, published=badge['published']) else: form = BadgeForm(badge, user_uri=user_uri, editing=True, published=badge['published']) if request.method == 'POST' and form.is_valid(): try: updated = {} if 'image_uri' in request.FILES: image = media_api.upload_image( request.FILES['image_uri'], request.session['user']['uri'], media_root=settings.MEDIA_ROOT, ) updated['image_uri'] = image['uri'] for attr in ['title', 'description', 'requirements']: if not badge[attr] == form.cleaned_data[attr]: updated[attr] = form.cleaned_data[attr] badge = badge_api.update_badge(badge['uri'], **updated) if badge['published']: return http.HttpResponseRedirect( reverse('badge_view', args=(badge_api.uri2id(badge['uri']),)) ) return http.HttpResponseRedirect( reverse('badge_preview', args=(badge_api.uri2id(badge['uri']),)) ) except badge_api.DuplicateTitleError: form.errors['title'] = [_('Badge title needs to be unique'), ] return render_to_response( 'badge/badge_info/edit.html', { 'badge': badge, 'form': form, 'user_is_partner': user_partner, 'existing_img': media_api.get_image(badge['image_uri']), }, context_instance=RequestContext(request))
def list_projects_by_user(user_uri): """ Listing all the projects that user has """ projects = search_projects(author_uri=user_uri) for project in projects: badge = get_badge(project['badge_uri']) fetch_badge_resources(badge) feedback_list = get_project_feedback(project['uri']) fetch_resources(project, feedback_list=feedback_list) return projects
def preview(request, badge_id): badge = badge_api.get_badge(badge_api.id2uri(badge_id)) fetch_badge_resources(badge) user = request.session['user'] user['is_author'] = _user_is_author(badge, user) return render_to_response( 'badge/badge_info/preview.html', { 'badge': badge, 'user': user, }, context_instance=RequestContext(request) )
def profile(request, username ): context = {} user_uri = u'/uri/user/{0}'.format(username) context['draft_badges'] = badge_api.get_user_draft_badges(user_uri) context['earned_badges'] = badge_api.get_user_earned_badges(user_uri) context['created_badges'] = badge_api.get_user_created_badges(user_uri) context['awarded_badges'] = badge_api.get_user_awarded_badges(user_uri) map(fetch_badge_resources, context['draft_badges']) map(fetch_badge_resources, context['earned_badges']) map(fetch_badge_resources, context['created_badges']) map(fetch_badge_resources, context['awarded_badges']) context['feedback_your_projects'] = project_api.search_projects(author_uri=user_uri) map(fetch_resources, context['feedback_your_projects']) peer_projects = [] feedback_latest = [] for badge in context['earned_badges']: feedback_latest += project_api.get_projects_ready_for_feedback(badge['uri']) peer_projects += project_api.search_projects(badge_uri=badge['uri']) filter_func = lambda project: not project['author_uri'] == user_uri peer_projects = filter(filter_func, peer_projects) badges_under_revision = [] context['badges_under_revision'] = None for project in context['feedback_your_projects']: badge_uri = project_api.get_badge_uri_from_project_under_revision(project['uri']) if badge_uri: badge = badge_api.get_badge(badge_uri) fetch_badge_resources(badge) badges_under_revision.append(badge) if badges_under_revision: context['badges_under_revision'] = badges_under_revision context['feedback_peer_projects'] = map(fetch_resources, peer_projects) context['feedback_latest'] = map(fetch_resources, feedback_latest) context['user'] = user_api.get_user(user_api.username2uri(username)) context['can_delete'] = False if 'user' in request.session: if context['user']['username'] == request.session.get('user')['username']: context['can_delete'] = True return render_to_response( 'dashboard/dashboard.html', context, context_instance=RequestContext(request) )
def view( request, badge_id ): badge = badge_api.get_badge(badge_api.id2uri(badge_id)) fetch_badge_resources(badge) context = { 'badge': badge } context['projects'] = map(fetch_resources, project_api.search_projects(badge_uri=badge['uri'])) context['experts'] = badge_api.get_badge_experts(badge['uri']) return render_to_response( 'badge/view.html', context, context_instance=RequestContext(request) )
def test_create_and_get_partner_badge(self): #Test that we can create a badge badge_values = self.BADGE_MOCK_ATTRIBUTES.copy() badge_values['partner_name'] = 'Test partner' badge = badge_api.create_badge(**badge_values) # test make sure attributes are equal badge2 = badge_api.get_badge(badge['uri']) self.assertEquals(badge, badge2) self.assertEquals('Test partner', badge['partner_name']) # test that the badge shows up in drafts badges = badge_api.get_user_draft_badges(badge['author_uri']) self.assertEquals(len(badges), 1) self.assertEquals(badge2, badges[0])
def list_projects_that_user_gave_feedback(user_uri): """ Listing all project that user gave feedback to """ projects_list = get_projects_user_gave_feedback(user_uri) for project in projects_list: badge = get_badge(project['badge_uri']) fetch_badge_resources(badge) feedback_list = get_project_feedback(project['uri']) fetch_resources(project, feedback_list=feedback_list) return projects_list
def publish( request, badge_id ): #TODO check user badge = badge_api.get_badge(badge_api.id2uri(badge_id)) if request.method == 'POST': badge_api.publish_badge(badge_api.id2uri(badge_id)) return http.HttpResponseRedirect(reverse( 'badge_view', args=(badge_id,) )) return render_to_response( 'badge/publish.html', { 'badge': badge }, context_instance=RequestContext(request) )
def get_filtered_badges(option): if option == 'popular': badges = [] items = sort_badge_uris_by_attached_projects()[:12] for item in items: badges.append(get_badge(item['badge_uri'])) elif option == 'new': badges = last_n_published_badges(12) elif option == 'featured': badges = get_featured_badges() else: badges = get_published_badges() return map(fetch_badge_resources, badges)
def test_update_badge(self): badge_attributes = { 'title':'Test Badge', 'image_uri': '/uri/image/1', 'description': 'Short description', 'requirements': 'Requirements are getting listed here', 'author_uri': '/uri/autor/badgemaker', } badge = badge_api.create_badge(**badge_attributes) # author uri has to be taken out since it is not demanded by the method del badge_attributes['author_uri'] badge_attributes['title'] = 'A new title' badge_api.update_badge(badge['uri'], **badge_attributes) badge2 = badge_api.get_badge(badge['uri']) # Test that we can update a badge self.assertNotEquals(badge, badge2)
def preview(request, badge_id): badge = badge_api.get_badge(badge_api.id2uri(badge_id)) if badge['published_date']: return HttpResponseRedirect(reverse('badge_view', args=[badge_id])) fetch_badge_resources(badge) user = request.session['user'] user['is_author'] = _user_is_author(badge, user) return render_to_response( 'badge/badge_info/preview.html', { 'badge': badge, 'user': user, }, context_instance=RequestContext(request) )
def submit_feedback(project_uri, expert_uri, good, bad, ugly, award_badge=False): """ Checks if badge can be awarded and creates feedback """ project = Project.objects.get(id=uri2id(project_uri)) badge = get_badge(project.badge_uri) result = submit_feedback_result.NOT_AWARDED if not expert_uri in get_badge_experts(project.badge_uri): raise Exception('Only experts can submit feedback on projects.') if not ready_for_feedback(project_uri): raise Exception('Badge has already been awarded to project.') feedback = Feedback( project=project, expert_uri=expert_uri, good=good, bad=bad, ugly=ugly, date_created=datetime.utcnow(), badge_awarded=False ) if award_badge and can_badge_be_awarded(badge, expert_uri): result = submit_feedback_result.AWARDED feedback.badge_awarded = True elif award_badge: #and badge requires approval by partner result = submit_feedback_result.REQUIRES_APPROVAL send_badge_needs_partner_feedback_notification(badge, _project2dict(project)) last_revision = None if project.revision_set.count() > 0: last_revision = project.revision_set.latest('date_created') if last_revision: feedback.revision = last_revision feedback.save() send_feedback_notification(get_project(project_uri)) #return _feedback2dict(feedback) return result
def publish(request, badge_id): user = request.session['user'] badge = badge_api.get_badge(badge_api.id2uri(badge_id)) if not user['uri'] == badge['author_uri']: messages.error(request, _('You cannot publish someone elses badge!')) return http.HttpResponseRedirect(reverse( 'badge_preview', args=(badge_id,) )) if request.method == 'POST': badge_api.publish_badge(badge['uri']) return http.HttpResponseRedirect(reverse( 'badge_view', args=(badge_id,) )) return http.HttpResponseRedirect(reverse( 'badge_preview', args=(badge_id,) ))
def create(request, badge_id): badge = badge_api.get_badge(badge_api.id2uri(badge_id)) fetch_badge_resources(badge) context = {'badge': badge} user_uri = request.session['user']['uri'] if request.method == 'POST': form = ProjectForm(request.POST, request.FILES) else: form = ProjectForm() if form.is_valid(): try: image = media_api.upload_image( request.FILES['image'], user_uri, media_root=settings.MEDIA_ROOT, delete_original=True) project = project_api.create_project( badge['uri'], user_uri, form.cleaned_data['title'], image['uri'], form.cleaned_data['work_url'], form.cleaned_data['description'], form.cleaned_data['reflection'], form.cleaned_data['tags'] ) return http.HttpResponseRedirect(reverse('project_view', args=(project['id'],))) except project_api.MultipleProjectError: messages.error(request, _('You have already submitted a project for this badge.')) except media_api.UploadImageError: messages.error(request, _('Project image cannot be uploaded. Possible reasons: format not supported' '(png, jpeg, jpg, gif), file size too large (up to 256kb).')) context['form'] = form return render_to_response( 'project/create.html', context, context_instance=RequestContext(request) )
def view( request, badge_id ): badge = badge_api.get_badge(badge_api.id2uri(badge_id)) fetch_badge_resources(badge) context = { 'badge': badge } context['projects'] = map(fetch_resources, project_api.search_projects(badge_uri=badge['uri'])) expert_uris = badge_api.get_badge_experts(badge['uri']) if request.session.get('user'): context['user_is_expert'] = request.session['user']['uri'] in expert_uris context['experts'] = map(p2pu_user_api.get_user, expert_uris) return render_to_response( 'badge/view.html', context, context_instance=RequestContext(request) )
def view(request, badge_id): badge = badge_api.get_badge(badge_api.id2uri(badge_id)) fetch_badge_resources(badge) projects = project_api.search_projects_awarded_badges(badge_uri=badge['uri']) for project in projects: fetch_resources(project, feedback_list=project_api.get_project_feedback(project['uri'])) expert_uris = badge_api.get_badge_experts(badge['uri']) user = request.session.get('user') if user: user['is_expert'] = user['uri'] in expert_uris user['is_author'] = _user_is_author(badge, user) if user['is_author']: user['can_delete_badge'] = badge_api.is_allowed_to_remove(badge['uri']) if user['is_expert']: user['added_to_backpack'] = badge_api.pushed_to_backpack(badge, user['uri']) projects_ready_for_feedback = project_api.get_projects_ready_for_feedback(badge['uri']) for project in projects_ready_for_feedback: feedback_list = project_api.get_project_feedback(project['uri']) fetch_resources(project, feedback_list=feedback_list) experts = map(p2pu_user_api.get_user, expert_uris) iframe = '//%s%s?rendering=normal' % (settings.ORGANISATION_URL, reverse('badge_view_embedded', args=[badge_id])) return render_to_response( 'badge/badge_info/view.html', { 'user': user, 'badge': badge, 'projects': projects, 'projects_ready_for_feedback': projects_ready_for_feedback, 'experts': experts, 'iframe': iframe, 'organisation_url': settings.ORGANISATION_URL, }, context_instance=RequestContext(request) )
def view(request, badge_id): badge = badge_api.get_badge(badge_api.id2uri(badge_id)) fetch_badge_resources(badge) context = {'badge': badge} context['projects'] = map(fetch_resources, project_api.search_projects_awarded_badges(badge_uri=badge['uri'])) expert_uris = badge_api.get_badge_experts(badge['uri']) if request.session.get('user'): context['user_is_expert'] = request.session['user']['uri'] in expert_uris context['experts'] = map(p2pu_user_api.get_user, expert_uris) context['iframe'] = 'http://%s%s?rendering=normal' % (settings.ORGANISATION_URL, reverse('badge_view_embedded', args=[badge_id])) return render_to_response( 'badge/view.html', context, context_instance=RequestContext(request) )
def edit( request, badge_id ): template_name = 'badge/edit.html' badge = badge_api.get_badge(badge_api.id2uri(badge_id)) if not request.session['user']['uri'] == badge['author_uri']: messages.error(request, _('You cannot edit someone elses badge!')) return http.HttpResponseRedirect(reverse( 'badge_preview', args=(badge_id,) )) if badge['publised']: messages.error(request, _('Badge already publised, create a new badge instead')) return http.HttpResponseRedirect(reverse( 'badge_view', args=(badge_id,) )) if request.method == 'POST': form = BadgeForm(request.POST, request.FILES) else: form = BadgeForm(badge) if request.method == 'POST' and form.is_valid(): #TODO update image #image = media_api.upload_image(request.FILES['image'], '/uri/user/1') try: updated = {} for attr in ['title', 'description', 'requirements']: if not badge[attr] == form.cleaned_data[attr]: updated[attr] = form.cleaned_data[attr] badge = badge_api.update_badge(badge['uri'], **updated) return http.HttpResponseRedirect( reverse('badge_preview', args=(badge_api.uri2id(badge['uri']),)) ) except badge_api.DuplicateTitleError: form.errors['title'] = [_('Badge title needs to be unique'),] return render_to_response( template_name, {'form': form}, context_instance=RequestContext(request))
def feedback( request, project_id ): project = project_api.get_project(project_api.id2uri(project_id)) badge = badge_api.get_badge(project['badge_uri']) user_uri = request.session['user']['uri'] if request.method == 'POST': form = FeedbackForm(request.POST) else: form = FeedbackForm() if form.is_valid(): project_api.submit_feedback( project['uri'], user_uri, form.cleaned_data['good'], form.cleaned_data['bad'], form.cleaned_data['ugly'], form.cleaned_data.get('award_badge', form.cleaned_data.get('award_badge', False) ) ) if form.cleaned_data.get('award_badge'): badge_api.award_badge( badge['uri'], project['author_uri'], user_uri, reverse('project_view', args=(project_id,)), ) messages.success(request, _('Badge awarded to user!')) return http.HttpResponseRedirect(reverse('project_view', args=(project_id,))) context = { 'project': project, 'form': form } return render_to_response( 'project/feedback.html', context, context_instance=RequestContext(request) )
def view_embedded(request, badge_id): # Get username from parameters or None username = request.GET.get('username', None) # Check if user is logged in if 'user' in request.session: username = request.session['user']['username'] badge_uri = badge_api.id2uri(badge_id) badge = badge_api.get_badge(badge_uri) fetch_badge_resources(badge) projects = None user_has_badge = False template = 'badge/embed/embedded_large.html' if username: user_has_badge = badge_api.check_if_user_has_badge(badge['uri'], p2pu_user_api.username2uri(username)) if user_has_badge: projects = project_api.get_projects_ready_for_feedback(badge_uri) if projects: map(fetch_resources, projects) # Extract rendering preference -> default large rendering = request.GET.get('rendering', 'large') if rendering == 'normal': template = 'badge/embed/embedded_normal.html' return render_to_response( template, { 'badge': badge, 'projects': projects, 'user_has_badge': user_has_badge, 'rendering': rendering, }, context_instance=RequestContext(request) )
def test_create_and_get_badge(self): """ Test that we can create a badge """ badge = badge_api.create_badge(*self.badge_values) # test the presence or attributes for a badge for attr in self.badge_attrs: self.assertIn(attr, badge) # test attribute values attrs = self.badge_attrs del attrs[0] del attrs[0] for key, value in zip(attrs, self.badge_values): self.assertEquals(badge[key], value) # test make sure attributes are equal badge2 = badge_api.get_badge(badge['uri']) self.assertEqual(badge, badge2) # test that the badge shows up in drafts badges = badge_api.get_user_draft_badges(badge['author_uri']) self.assertEquals(len(badges), 1) self.assertEquals(badge2, badges[0])
def feedback(request, project_id): project = project_api.get_project(project_api.id2uri(project_id)) fetch_resources(project) feedback = project_api.get_project_feedback(project_api.id2uri(project_id)) badge = badge_api.get_badge(project['badge_uri']) fetch_badge_resources(badge) user_uri = request.session['user']['uri'] if request.method == 'POST': form = FeedbackForm(request.POST) else: form = FeedbackForm() if form.is_valid(): try: feedback = project_api.submit_feedback( project['uri'], user_uri, form.cleaned_data['good'], form.cleaned_data['bad'], form.cleaned_data['ugly'], form.cleaned_data.get('award_badge', form.cleaned_data.get('award_badge', False)) ) if feedback == project_api.submit_feedback_result.AWARDED: badge_api.award_badge( badge['uri'], project['author_uri'], user_uri, reverse('project_view', args=(project_id,)), ) messages.success(request, _("Success! You've awarded the Badge to %s" % project['author']['username'])) elif feedback == project_api.submit_feedback_result.REQUIRES_APPROVAL: messages.error(request, _("Badge can only be awarded by it's creator. " "We had notified them that you liked this project.")) except Exception, e: messages.error(request, _(e[0])) return http.HttpResponseRedirect(reverse('project_view', args=(project_id,)))