1. boblefrag
  2. django-simple-snippet

Commits

boblefrag  committed 36c498c Draft

Every link are now using {% url %} instead of hard link. Deployment
instruction have been updated too.

  • Participants
  • Parent commits 5fa2f4f
  • Branches default

Comments (0)

Files changed (16)

File README.txt

View file
 
 Then add snippet to the list of INSTALLED_APPS
 
-django-simple-snippet use 'django.contrib.markup' you shoud add it to
-your INSTALLED_APPS too.
+django-simple-snippet use 'django.contrib.markup' for rendering the
+description of your snippets.
+
+It also user django-registration for the registration process.
+
+your INSTALLED_APPS
 
     ::
 
             ...
             'snippet',
             'django.contrib.markup'
+            'registration'
             ...
             )
 
 
 to create the needed models
 
+
 snippet rely on the admin app to let you edit the language allowed to
 be used. If you do not want to use the admin, you should do the
 following :
                         description=<a fancy description
                         about your beloved language>)
 
+You also need to modify your root urls to link to the needed urls for exemple:
+
+
+
+::
+
+    url(r'', include('registration.urls')),
+    url(r'^accounts/profile/', 'snippet.views.profile'),
+    url(r'^snippet/', include('snippet.urls')),
+
+A note about django-registration
+--------------------------------
+
+::
+
+    For your convenience, snippet use django registration to manage
+    the user creation process and the login and logout. Templates for
+    registrations are avalaible in the snippet/templates/regisration
+    directory. If you do not whant to use this app, you must provide a
+    auth_login, auth_logout and an acount_profile urls.
 
 Usage
 =====
 - subscribe to a user/tag/language snippet rss feed
 
 - bookmark snippets you liked
+
+- let you tweet the snippet you like or you just created

File models.py

View file
         """
         vals = []
         if value == None:
-            return 
+            return
         vals = [Tag.objects.get(pk=item).name for item in value]
         return ','.join(vals)
-       
+
     def to_python(self, value):
         """
-        Take the values returned by a form or a manual save 
+        Take the values returned by a form or a manual save
         and convert them in a list of Tag objects. It create
         missing Tag if not found in the database
         """
         vals = [item.strip() for item in value.split(',')]
+        print vals
         values = []
         for val in vals:
             if val != None and val !='':
                 values.append(tag.id)
         return values
 
+
 class CommaSeparatedManyToManyField(models.ManyToManyField):
     """
     Same as ManyToManyField but use StringManyToManyField as display
     def formfield(self, **kwargs):
         return models.Field.formfield(self, StringManyToManyField, **kwargs)
 
+
 class Tag(models.Model):
     """
     You can/must give some tags to a snippet to retreive it easily
 
     def __unicode__(self):
         return self.name
-    
+
+
 class Language(models.Model):
     """
     Used to get a list of avalaible language and do count on snippets
 
     def __unicode__(self):
         return self.name
-    
+
+
 class Snippet(models.Model):
     """
     The base model. Snippets are code example posted 
     description = models.TextField()
     code = models.TextField()
     tags = CommaSeparatedManyToManyField(Tag)
-    date = models.DateField(auto_now = True)
+    date = models.DateField(auto_now=True)
 
     def __unicode__(self):
         return self.title
 
     def get_absolute_url(self):
         return reverse("snippet_detail", args=[self.pk])
-    
+
     def hightlighted(self):
         """
         Used to render a html of self.code with syntax coloration

File templates/block/user_menu.html

View file
 
       <b class="caret"></b></a>
     <ul class="dropdown-menu">
-      <li><a href="/authors/{{user.pk}}">My Snippets</a></li>
-      <li><a href="/snippet/new">Add a New Snippet</a></li>
+      <li><a href="{% url author user.pk%}">My Snippets</a></li>
+      <li><a href="{% url create_snippet %}">Add a New Snippet</a></li>
       <li class="divider"></li>
-      <li><a href="/logout/">Log Out</a></li>
+      <li><a href="{% url auth_logout %}">LogOut</a></li>
     </ul>
   </li>
 </ul>
 {% else %}
   <li class="dropdown">
-    <a href="/login/" 
+    <a href="{% url auth_login %}" 
        class="dropdown-toggle" 
        data-toggle="dropdown">
       Log In

File templates/registration/activate.html

View file
+{% extends "base.html" %}
+{% block content %}
+<h2>Welcome to Trackable</h2>
+<h3>You can now <a href="/login/">Log In</a></h3>
+{% endblock content %}

File templates/registration/activation_complete.html

View file
+{% extends "base.html" %}
+{% block content %}
+<h2>Welcome to Trackable</h2>
+<h3>You can now <a href="/login/">Log In</a></h3>
+{% endblock content %}

File templates/registration/activation_email.txt

View file
+You have asked to register an account at
+Trackable. 
+
+To activate this account, please click the following link within the next
+{{ expiration_days }} days:
+
+http://{{site.domain}}/activate/{{ activation_key }}
+
+Sincerely,
+Trackable Management

File templates/registration/activation_email_subject.txt

View file
+Account registration for Trackable

File templates/registration/login.html

View file
+{% extends "base.html" %}
+
+{% block content %}
+<div class="row">
+  <div class="span6">
+    <form class="well" 
+	  method="POST"
+	  action="{% url django.contrib.auth.views.login %}" >
+      {% csrf_token %}
+      <h2>Login</h2>
+      {% if form.non_field_errors %}
+        <div class="alert alert-error">
+	  {% for error in form.non_field_errors %}
+	  {{error}}
+	  {% endfor %}
+        </div>
+      {% endif %}
+	<div
+	   class="control-group
+		  {% if form.username.errors %}
+		  error
+		  {% endif %}"
+	   >
+	  <label for="id_username">Username : </label>
+
+	  <input type="text"
+		 id="id_username"
+		 value=""
+		 name="username">
+	  <span class="help-inline">
+	    {% if form.username.errors %}
+	    {% for error in  form.username.errors  %}
+              {{error}}
+            {% endfor %}
+	    {%endif%}
+	  </span>
+	</div>
+	<div
+	   class="control-group
+		  {% if form.password.errors %}
+		  error
+		  {%endif %}
+		  "
+	   >
+
+	  <label for="id_password">Password : </label>
+	  {{form.password}}
+	  <span class="help-inline">
+	    {%if form.password.error %}
+	    {% for error in form.password.errors %}
+	    {{error}}
+	    {% endfor %}
+	    {%endif%}
+	  </span>
+	</div>
+      <input type="submit" value="Submit">
+    </form>
+    You do not have an account ?
+    <a href="{% url registration_register%}">Sign In</a>
+  </div>
+</div>
+{% endblock content %}

File templates/registration/logout.html

View file
+{% extends "base.html" %}
+{% block content %}
+<h2>Successful Log Out</h2>
+<div>
+  <p>Now you can <a href="/login/">Login Again</a> or <a href="/register/">Register a
+  new user</a></p>
+  <p></p>
+</div>
+{% endblock content %}

File templates/registration/registration_complete.html

View file
+{% extends "base.html" %}
+
+{% block content %}
+<h2>Activation email sent</h2>
+An activation email has been sent. Please check your email and click on the link to activate your account.
+{% endblock content %}

File templates/registration/registration_form.html

View file
+{% extends "base.html" %}
+{% block content %}
+<div class="span8">
+  <form method="post" class="form well " action="">
+    <h2>Registration Form</h2>
+    {%csrf_token %}
+    {% if form.non_field_errors %}  
+      <div class="alert alert-error">
+
+        {% for error in form.non_field_errors %}
+          {{error}}
+        {% endfor %}
+
+      </div>
+    {% endif %}
+      <div>
+	<div 
+	   class="control-group 
+		  {% if form.username.errors %}
+		  error
+		  {% endif %}"
+	   >
+	  
+	  <label for="id_username">Username : </label>
+
+	  <input type="text" 
+		 id="id_username" 
+		 value=""
+		 name="username">
+	  <span class="help-inline">
+	    {% if form.username.errors %}
+	    {% for error in  form.username.errors  %}
+              {{error}}
+            {% endfor %}
+	    {% else %}
+	    *required
+	    {%endif%}
+	  </span>
+	</div>
+	<div 
+	   class="control-group 
+		  {% if form.email.errors %}
+		  error
+		  {%endif %}" 
+	   >
+	  
+	  <label for="id_email">E-mail : </label>
+	  {{ form.email }}
+	  <span class="help-inline">
+	    {% if form.email.errors %}
+	    {% for error in form.email.errors %} 
+	    {{error}} 
+	    {% endfor%}
+	    {%else%}
+	    *required
+	    {%endif%}
+	  </span>
+	</div>
+	<div 
+	   class="control-group
+		  {% if form.password1.errors %}
+		  error
+		  {%endif %}
+		  "
+	   >
+
+	  <label for="id_password1">Password : </label>
+	  {{form.password1}}
+	  <span class="help-inline">
+	    {%if form.password1.error %}
+	    {% for error in form.password1.errors %}
+	    {{error}}
+	    {% endfor %}
+	    {%else%}
+	    *required
+	    {%endif%}
+	  </span>
+	</div>
+	<div 
+	   class="control-group
+		  {% if form.password2.errors %}
+		  error
+		  {%endif %}
+		  "
+	   >
+
+	  <label for="id_password2">Password (again): </label>
+	  {{form.password2}}
+	  <span class="help-inline">
+	    {% if form.password2.errors %}
+	    {% for error in form.password2.errors %}
+	    
+	    {{error}}
+	    {% endfor %}
+	    {%else%}
+	    *required
+	    {%endif%}
+	  </span>
+	</div>  
+	
+    <input type="submit" value="Submit" />
+  </form>
+</div>
+
+{% endblock content %}

File templates/snippet/blocks/snippet.html

View file
 {% load markup %}
 <div class="row-fluid">
   <h2>{{object.title}}</h2>
-  <h4>By <a href="/authors/{{object.user.pk}}">{{object.user.username}}</a>
-  using <a href="/languages/{{object.language.pk}}">{{object.language.name}}</a></h4>
+  <h4>By <a href="{% url author object.user.pk %}">{{object.user.username}}</a>
+  using <a href="{% url language object.language.pk %}">{{object.language.name}}</a></h4>
   <div class="span8">
     {% autoescape off%}{{object.hightlighted}}{% endautoescape %}
-    <p>Tagged as : {% for tag in object.tags.all%}<a class="btn" href="/tags/{{tag.pk}}">{{tag}}</a> {%endfor%}</p>
+    <p>Tagged as : {% for tag in object.tags.all%}<a class="btn"
+    href="{% url tag tag.id %}">{{tag}}</a> {%endfor%}</p>
   </div> 
   
   <div class="span3">

File templates/snippet/snippet_confirm_delete.html

View file
 {% csrf_token %}
 <input type="submit" value="Delete" class="btn btn-danger" />
 </form>
+{% else %}
+Hum... This is an error <a href="{%url snippet_root %}">Take me back home</a>
 {% endif %}
-Hum... This is an error <a href="/">Take me back home</a>
 {% endblock content %}
-

File templates/snippet/snippet_list.html

View file
   {% for object in object_list %}
 <div class="row-fluid snippet">
 <p>
-  <a href="/snippet/{{object.pk}}/">
-    {{object.title}} by <a href="/authors/{{object.user.pk}}">{{object.user.username}}</a> / {{object.language}} 
+  <a href="{%url snippet_detail object.pk %}">
+    {{object.title}} by <a href="{% url author object.user.pk %}">
+    {{object.user.username}}</a> / 
+    <a href="{% url language object.language.id %}">{{object.language}}</a>
   </a>
   
 </p>

File urls.py

View file
 
 """
 from django.conf.urls.defaults import patterns, include, url
+from django.core.urlresolvers import reverse_lazy
 from django.views.generic import CreateView,UpdateView,DeleteView, DetailView,\
       ListView
 from models import Snippet, Language, Tag
 from django.contrib.auth.models import User
+from django.contrib.auth.views import login, logout
 from feeds import LatestSnippetFeed
 
 urlpatterns = patterns('',
-    url(r'^new/$',CreateView.as_view(model=Snippet), name="create_snippet"),
+    url(r'^$', ListView.as_view(model=Snippet), name="snippet_root"),
+    url(r'^new/$', CreateView.as_view(model=Snippet), name="create_snippet"),
     url(r'^(?P<pk>\d+)/$',
         DetailView.as_view(model=Snippet),
         name="snippet_detail"),
-    url(r'^(?P<pk>\d+)/edit/$',UpdateView.as_view(model=Snippet)),
-    url(r'^(?P<pk>\d+)/delete/$',DeleteView.as_view(model=Snippet,
-                                         success_url="snippet_root")),
-    url(r'^$',ListView.as_view(model=Snippet), name="snippet_root"),
+    url(r'^(?P<pk>\d+)/edit/$', UpdateView.as_view(model=Snippet)),
+    url(r'^(?P<pk>\d+)/delete/$',
+        DeleteView.as_view(
+            model=Snippet,
+            success_url=reverse_lazy("snippet_root")
+            )
+        ),
 #TODO add a paginated by on those views
-    url(r'^authors/(?P<pk>\d+)/$',DetailView.as_view(model=User)),
+    url(r'^authors/(?P<pk>\d+)/$',DetailView.as_view(model=User), name="author"),
     url(r'^authors/$',ListView.as_view(model=User),name="authors"),
     url(r'^languages/(?P<pk>\d+)/$',
-        DetailView.as_view(model=Language),
+        DetailView.as_view(model=Language),name="language"
         ),
     url(r'^languages/$',ListView.as_view(model=Language),
         name="languages"),
-    url(r'^tags/(?P<pk>\d+)/$',DetailView.as_view(model=Tag)),
+    url(r'^tags/(?P<pk>\d+)/$',DetailView.as_view(model=Tag), name="tag"),
     url(r'^tags/$',ListView.as_view(model=Tag), name="tags"),
     url(r'^feeds/snippet/$',LatestSnippetFeed())
     )

File views.py

View file
 # Create your views here.
 from django.http import HttpResponseRedirect
+from django.shortcuts import redirect
 
 def profile(request):
-    return HttpResponseRedirect('/authors/%i'%request.user.pk)
+
+    return redirect('author', request.user.pk)