<commit_before>#!/usr/bin/env python
#:coding=utf-8:
#:tabSize=2:indentSize=2:noTabs=true:
#:folding=explicit:collapseFolds=1:

from django.contrib import admin
from django import forms
from django.forms.util import ErrorList
from django.contrib.contenttypes import generic
from django.contrib.comments.models import *

from dlife.lifestream.models import *
from dlife.util import feedparser
from dlife.util import get_url_domain
from dlife.lifestream.feeds import clean_item_content

class LifestreamAdmin(admin.ModelAdmin):
  list_display    = ('ls_title',)
  exclude         = ['ls_user',]
  list_per_page   = 20
  
  model = Lifestream
  
  def save_model(self, request, obj, form, change):
    obj.ls_user = request.user
    obj.save()
    if not change:
      # Create a new feed that is not editable
      # so that the user can use it when adding
      # items directly to the lifestream.
      basic_feed = Feed()
      basic_feed.feed_lifestream = obj
      basic_feed.feed_name = obj.ls_title
      
      #This is ignored
      basic_feed.feed_url = "http://localhost"
      basic_feed.feed_domain = "Local"
      basic_feed.feed_fetchable = False
      basic_feed.feed_basic_feed = True
      basic_feed.save()

admin.site.register(Lifestream, LifestreamAdmin)

class FeedAdminForm(forms.ModelForm):
  class Meta:
    model = Feed
  
  def clean(self):
    """
    Checks to make sure a feed url is valid and gets the feed title
    and domain.
    """
    cleaned_data = self.cleaned_data
    feed_url = cleaned_data.get('feed_url')
    if not feed_url:
      # Feed url was not validated by the field validator
      return
    feed = feedparser.parse(feed_url)
    
    # Check if the feed was not parsed correctly.
    if feed['bozo']:
      self._errors['feed_url'] = ErrorList(["This is not a valid feed: %s" % feed['bozo_exception']])
      # This field is no longer valid. Remove from cleaned_data
      del cleaned_data['feed_url']
      return
    # Check if the feed has a title field
    feed_info = feed.get('feed')
    if not feed_info.get('title'):
      self._errors['feed_url'] = ErrorList(["This is not a valid feed: The feed is empty"])
      # This field is no longer valid. Remove from cleaned_data
      del cleaned_data['feed_url']
      return
    cleaned_data['feed_name'] = feed_info['title']
    cleaned_data['feed_domain'] = get_url_domain(feed_url)
    return cleaned_data

class FeedAdmin(admin.ModelAdmin):
  exclude         = ['feed_name', 'feed_domain', 'feed_basic_feed']
  list_display    = ('feed_name', 'feed_domain')
  list_filter     = ('feed_domain',)
  
  form = FeedAdminForm
  
  model = Feed
  
  def queryset(self, request):
    return self.model.objects.get_feeds()

admin.site.register(Feed, FeedAdmin)

# class CommentAdmin(admin.ModelAdmin):
#   list_display    = ('comment_item', 'comment_name')
#   exclude         = ['comment_item',]
#   list_filter     = ('comment_user',)
#   search_fields   = ('comment_name','comment_content')
#   list_per_page   = 20

# admin.site.register(Comment, CommentAdmin)

# class CommentInline(admin.StackedInline):
#   model           = Comment
#   max_num         = 1   #TODO: Fix this
#   exclude         = ['comment_item','content_type','object_id']

class ItemAdmin(admin.ModelAdmin):
  list_display    = ('item_title', 'item_date')
  exclude         = ['item_clean_content',]
  list_filter     = ('item_feed',)
  search_fields   = ('item_title','item_clean_content')
  list_per_page   = 20
  
  model = Item
  # inlines         = [CommentInline,]
  
  def save_model(self, request, obj, form, change):
    obj.item_content, obj.item_clean_content = clean_item_content(obj.item_content)
    obj.save()
    

admin.site.register(Item, ItemAdmin)

class TagAdmin(admin.ModelAdmin):
  list_display   = ('tag_name', 'tag_count')
  ordering       = ('-tag_count',)
  search_fields  = ('tag_name',)
  list_per_page   = 20
  
  model = Tag

admin.site.register(Tag, TagAdmin)<commit_msg>Added item_published to the items list view in the admin.<commit_after>#!/usr/bin/env python
#:coding=utf-8:
#:tabSize=2:indentSize=2:noTabs=true:
#:folding=explicit:collapseFolds=1:

from django.contrib import admin
from django import forms
from django.forms.util import ErrorList
from django.contrib.contenttypes import generic
from django.contrib.comments.models import *

from dlife.lifestream.models import *
from dlife.util import feedparser
from dlife.util import get_url_domain
from dlife.lifestream.feeds import clean_item_content

class LifestreamAdmin(admin.ModelAdmin):
  list_display    = ('ls_title',)
  exclude         = ['ls_user',]
  list_per_page   = 20
  
  model = Lifestream
  
  def save_model(self, request, obj, form, change):
    obj.ls_user = request.user
    obj.save()
    if not change:
      # Create a new feed that is not editable
      # so that the user can use it when adding
      # items directly to the lifestream.
      basic_feed = Feed()
      basic_feed.feed_lifestream = obj
      basic_feed.feed_name = obj.ls_title
      
      #This is ignored
      basic_feed.feed_url = "http://localhost"
      basic_feed.feed_domain = "Local"
      basic_feed.feed_fetchable = False
      basic_feed.feed_basic_feed = True
      basic_feed.save()

admin.site.register(Lifestream, LifestreamAdmin)

class FeedAdminForm(forms.ModelForm):
  class Meta:
    model = Feed
  
  def clean(self):
    """
    Checks to make sure a feed url is valid and gets the feed title
    and domain.
    """
    cleaned_data = self.cleaned_data
    feed_url = cleaned_data.get('feed_url')
    if not feed_url:
      # Feed url was not validated by the field validator
      return
    feed = feedparser.parse(feed_url)
    
    # Check if the feed was not parsed correctly.
    if feed['bozo']:
      self._errors['feed_url'] = ErrorList(["This is not a valid feed: %s" % feed['bozo_exception']])
      # This field is no longer valid. Remove from cleaned_data
      del cleaned_data['feed_url']
      return
    # Check if the feed has a title field
    feed_info = feed.get('feed')
    if not feed_info.get('title'):
      self._errors['feed_url'] = ErrorList(["This is not a valid feed: The feed is empty"])
      # This field is no longer valid. Remove from cleaned_data
      del cleaned_data['feed_url']
      return
    cleaned_data['feed_name'] = feed_info['title']
    cleaned_data['feed_domain'] = get_url_domain(feed_url)
    return cleaned_data

class FeedAdmin(admin.ModelAdmin):
  exclude         = ['feed_name', 'feed_domain', 'feed_basic_feed']
  list_display    = ('feed_name', 'feed_domain')
  list_filter     = ('feed_domain',)
  
  form = FeedAdminForm
  
  model = Feed
  
  def queryset(self, request):
    return self.model.objects.get_feeds()

admin.site.register(Feed, FeedAdmin)

# class CommentAdmin(admin.ModelAdmin):
#   list_display    = ('comment_item', 'comment_name')
#   exclude         = ['comment_item',]
#   list_filter     = ('comment_user',)
#   search_fields   = ('comment_name','comment_content')
#   list_per_page   = 20

# admin.site.register(Comment, CommentAdmin)

# class CommentInline(admin.StackedInline):
#   model           = Comment
#   max_num         = 1   #TODO: Fix this
#   exclude         = ['comment_item','content_type','object_id']

class ItemAdmin(admin.ModelAdmin):
  list_display    = ('item_title', 'item_date','item_published')
  exclude         = ['item_clean_content',]
  list_filter     = ('item_feed',)
  search_fields   = ('item_title','item_clean_content')
  list_per_page   = 20
  
  model = Item
  # inlines         = [CommentInline,]
  
  def save_model(self, request, obj, form, change):
    obj.item_content, obj.item_clean_content = clean_item_content(obj.item_content)
    obj.save()
    

admin.site.register(Item, ItemAdmin)

class TagAdmin(admin.ModelAdmin):
  list_display   = ('tag_name', 'tag_count')
  ordering       = ('-tag_count',)
  search_fields  = ('tag_name',)
  list_per_page   = 20
  
  model = Tag

admin.site.register(Tag, TagAdmin)
{'fraction_non_alphanumeric': 0.10694254019693382, 'fraction_numerical': 0.0032406830362707215, 'mean_word_length': 3.367991290146979, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3795913', 'n_tokens_mistral': 2610, 'n_tokens_neox': 2574, 'n_words': 630}