Example #1
0
def view_modify_snippet(request, snippet_id, snippet_slug, modify_snippet_template, snippet_profile_template):
    modify = True
    from quest.forms import AddSnippetForm
    if request.method == 'POST':
        form = AddSnippetForm(post_data(request))
        existing_snippet = Snippet.objects.get(id=snippet_id)
        if form.is_valid():
            updated_snippet = _handle_snippet_updation(form, existing_snippet)
            from quest.messages import SNIPPET_UPDATION_SUCCESSFUL
            django_messages_framework.success(request, SNIPPET_UPDATION_SUCCESSFUL)
            return response(request, snippet_profile_template, {'snippet': updated_snippet,
                                                                'owner': True})
        return response(request, modify_snippet_template, {'form': form})
    else:
        snippet = Snippet.objects.get(id=snippet_id)
        form_data = {'title':snippet.title,
                     'explanation':snippet.explanation,
                     'code':snippet.code,
                     'public':snippet.public,
                     'lang':snippet.lang,
                     'tags':" ".join([tag.name for tag in snippet.tags()])}
        form = AddSnippetForm(form_data)
        return response(request, modify_snippet_template, {'form':form,
                                                           'snippet': snippet,
                                                           'modify':modify})
Example #2
0
 def test_add_snippet_form_valid_submission(self):
     form_data = {'title':'Can anybody improve this Imports_Checker',
                  'explanation':'Something goes in here',
                  'code':'''Hey this is nothing''',
                  'public':True,
                  'lang':'py',
                  'tags':'imports python'}
     form = AddSnippetForm(form_data)
     self.assertTrue(form.is_valid())
     self.assertFalse(form.errors)
     self.assertTrue(form.cleaned_data)
     self.assertEquals(form.cleaned_data.get('title'), form_data['title'])
     self.assertEquals(form.cleaned_data.get('explanation'), form_data['explanation'])
     self.assertEquals(form.cleaned_data.get('code'), form_data['code'])
     self.assertTrue(form.cleaned_data.get('public'))
     self.assertEquals(form.cleaned_data.get('lang'), form_data['lang'])
Example #3
0
def view_add_snippet(request, add_snippet_template, snippet_profile_template):
    user = request.user
    from quest.forms import AddSnippetForm
    if request.method == 'POST':
        form = AddSnippetForm(post_data(request))
        if form.is_valid():
            snippet = _handle_snippet_creation(form)
            owner = False
            if user.is_authenticated():
                userprofile = user.get_profile()
                owner = True
                from quest.models import UserProfileSnippetMembership
                UserProfileSnippetMembership.objects.create_membership(userprofile=userprofile,
                                                                      snippet=snippet)
            from quest.messages import SNIPPET_CREATION_SUCCESSFUL
            django_messages_framework.success(request, SNIPPET_CREATION_SUCCESSFUL)
            return response(request, snippet_profile_template, {'snippet': snippet,
                                                                'owner': owner})
    else:
        form = AddSnippetForm()
        if not user.is_authenticated():
            form.fields.get('public').widget.attrs = {'readonly': True, 
                                                      'disabled': 'disabled'}
    return response(request, add_snippet_template, {'form': form})