Adding comments
parent
14a9a392a7
commit
59dc9f5b58
32
migrate.py
32
migrate.py
|
@ -4,7 +4,7 @@ import MySQLdb
|
|||
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
from snipts.models import Snipt
|
||||
from snipts.models import Comment, Snipt
|
||||
|
||||
conn = MySQLdb.connect(host='localhost', user='root', passwd='root', db='sniptold')
|
||||
cursor = conn.cursor()
|
||||
|
@ -12,6 +12,7 @@ cursor = conn.cursor()
|
|||
def i():
|
||||
users()
|
||||
snipts()
|
||||
comments()
|
||||
|
||||
def users():
|
||||
|
||||
|
@ -90,3 +91,32 @@ def snipts():
|
|||
snipt.save()
|
||||
|
||||
print 'Done with snipts'
|
||||
|
||||
def comments():
|
||||
|
||||
print "Deleting existing comments"
|
||||
comments = Comment.objects.all()
|
||||
for c in comments:
|
||||
c.delete()
|
||||
|
||||
cursor.execute("SELECT * FROM django_comments")
|
||||
rows = cursor.fetchall()
|
||||
|
||||
for row in rows:
|
||||
snipt_id = row[2]
|
||||
user_id = row[4]
|
||||
cmt = row[8]
|
||||
created = row[9]
|
||||
|
||||
try:
|
||||
comment = Comment(
|
||||
snipt=Snipt.objects.get(id=snipt_id),
|
||||
user=User.objects.get(id=user_id),
|
||||
comment=cmt,
|
||||
created=created,
|
||||
)
|
||||
comment.save()
|
||||
except:
|
||||
print "Couldn't get snipt " + str(snipt_id)
|
||||
|
||||
print 'Done with comments'
|
||||
|
|
|
@ -1,11 +1,24 @@
|
|||
from django.contrib import admin
|
||||
|
||||
from snipts.models import Snipt
|
||||
from snipts.models import Comment, Snipt
|
||||
|
||||
class CommentInline(admin.TabularInline):
|
||||
model = Comment
|
||||
extra = 0
|
||||
allow_add = False
|
||||
|
||||
class SniptAdmin(admin.ModelAdmin):
|
||||
list_display = ('title', 'slug', 'tags', 'user', 'lexer', 'public', 'created', 'modified',)
|
||||
search_fields = ('title', 'user__username', 'tags', 'lexer',)
|
||||
search_fields = ('title', 'user__username', 'tags', 'lexer', 'id',)
|
||||
ordering = ('created',)
|
||||
prepopulated_fields = {'slug': ('title',)}
|
||||
|
||||
inlines = [CommentInline]
|
||||
|
||||
admin.site.register(Snipt, SniptAdmin)
|
||||
|
||||
class CommentAdmin(admin.ModelAdmin):
|
||||
list_display = ('comment', 'user', 'snipt', 'created', 'modified',)
|
||||
search_fields = ('comment', 'user__username',)
|
||||
ordering = ('created',)
|
||||
|
||||
admin.site.register(Comment, CommentAdmin)
|
||||
|
|
|
@ -0,0 +1,92 @@
|
|||
# encoding: utf-8
|
||||
import datetime
|
||||
from south.db import db
|
||||
from south.v2 import SchemaMigration
|
||||
from django.db import models
|
||||
|
||||
class Migration(SchemaMigration):
|
||||
|
||||
def forwards(self, orm):
|
||||
|
||||
# Adding model 'Comment'
|
||||
db.create_table('snipts_comment', (
|
||||
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
|
||||
('user', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'])),
|
||||
('snipt', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['snipts.Snipt'])),
|
||||
('comment', self.gf('django.db.models.fields.TextField')()),
|
||||
('created', self.gf('django.db.models.fields.DateTimeField')()),
|
||||
('modified', self.gf('django.db.models.fields.DateTimeField')(auto_now=True, blank=True)),
|
||||
))
|
||||
db.send_create_signal('snipts', ['Comment'])
|
||||
|
||||
|
||||
def backwards(self, orm):
|
||||
|
||||
# Deleting model 'Comment'
|
||||
db.delete_table('snipts_comment')
|
||||
|
||||
|
||||
models = {
|
||||
'auth.group': {
|
||||
'Meta': {'object_name': 'Group'},
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
|
||||
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
|
||||
},
|
||||
'auth.permission': {
|
||||
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
|
||||
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
|
||||
},
|
||||
'auth.user': {
|
||||
'Meta': {'object_name': 'User'},
|
||||
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
|
||||
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
|
||||
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
|
||||
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
|
||||
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
|
||||
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
|
||||
'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
|
||||
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
|
||||
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
|
||||
},
|
||||
'contenttypes.contenttype': {
|
||||
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
|
||||
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
|
||||
},
|
||||
'snipts.comment': {
|
||||
'Meta': {'object_name': 'Comment'},
|
||||
'comment': ('django.db.models.fields.TextField', [], {}),
|
||||
'created': ('django.db.models.fields.DateTimeField', [], {}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
|
||||
'snipt': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['snipts.Snipt']"}),
|
||||
'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"})
|
||||
},
|
||||
'snipts.snipt': {
|
||||
'Meta': {'object_name': 'Snipt'},
|
||||
'code': ('django.db.models.fields.TextField', [], {}),
|
||||
'created': ('django.db.models.fields.DateTimeField', [], {}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'key': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||
'lexer': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
|
||||
'modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
|
||||
'public': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'slug': ('django.db.models.fields.SlugField', [], {'max_length': '50', 'db_index': 'True'}),
|
||||
'stylized': ('django.db.models.fields.TextField', [], {}),
|
||||
'tags': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
|
||||
'title': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
|
||||
'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"})
|
||||
}
|
||||
}
|
||||
|
||||
complete_apps = ['snipts']
|
|
@ -4,7 +4,7 @@ from django.db import models
|
|||
class Snipt(models.Model):
|
||||
"""An individual code snippet."""
|
||||
|
||||
user = models.ForeignKey(User)
|
||||
user = models.ForeignKey(User, editable=False)
|
||||
|
||||
title = models.CharField(max_length=255)
|
||||
slug = models.SlugField()
|
||||
|
@ -23,3 +23,15 @@ class Snipt(models.Model):
|
|||
|
||||
def __unicode__(self):
|
||||
return u'%s' %(self.title)
|
||||
|
||||
class Comment(models.Model):
|
||||
"""A comment on a Snipt"""
|
||||
|
||||
user = models.ForeignKey(User, editable=False)
|
||||
snipt = models.ForeignKey(Snipt, editable=False)
|
||||
|
||||
comment = models.TextField()
|
||||
|
||||
# TODO Set auto_now_add back to True for production!
|
||||
created = models.DateTimeField(auto_now_add=False, editable=False)
|
||||
modified = models.DateTimeField(auto_now=True, editable=False)
|
||||
|
|
Loading…
Reference in New Issue