pagination_types.py 1.54 KB
#-*- coding:utf-8 -*-

from datetime import datetime
from dictionary.models import History, Lexeme, visible_vocabularies
import dictionary.forms as dictionary_forms

types = {}

def lexeme_history_list(params, user):
  objects = History.objects.exclude(lexeme=None).distinct().values(
    'lexeme', 'user', 'transaction_began')
  lexemes = Lexeme.objects.filter(
    owner_vocabulary__in=visible_vocabularies(user)).exclude(entry='')
  for field, value in params['filters'].iteritems():
    if field == 'user':
      objects = objects.filter(user__pk=value)
    if field == 'time_from':
      try:
        date = datetime.strptime(value, '%d.%m.%Y')
        objects = objects.filter(transaction_began__gte=date)
      except ValueError:
        pass
    if field == 'time_to':
      try:
        date = datetime.strptime(value, '%d.%m.%Y')
        objects = objects.filter(transaction_began__lte=date)
      except ValueError:
        pass
    if field == 'user_other':
      objects = objects.exclude(user__username=value)
  objects = objects.filter(lexeme__in=lexemes)
  objects = objects.order_by(params['order_by'])
  return objects

LEXEME_HISTORY_FILTERS = {
  # 'label': (Form(), ????), # czy wymaga logowania - niepotrzebne tutaj
  'user': (dictionary_forms.UserFilterForm(), True),
  'time_from': (dictionary_forms.TimeFromForm(), True),
  'time_to': (dictionary_forms.TimeToForm(), True),
}

def dummy_perm(_user, _object, _action):
  return True

types['lexeme_history'] = (
  lexeme_history_list, 'lexeme_history_item.html', dummy_perm, {},
  LEXEME_HISTORY_FILTERS)