def to_python(self, value): if isinstance(value, StatusType): return value elif isinstance(value, int): return StatusTypes.from_id(value) elif isinstance(value, str) or isinstance(value, unicode): return StatusTypes.from_name(value)
def get_user(request, name): user = get_object_or_404(User, username=name) stats = {'total': user.record_set.count()} for t in StatusTypes.types: stats[t.name] = 0 for d in user.record_set.values('status_type').annotate(count=Count('status_type')).order_by(): stats[StatusTypes.to_name(d['status_type'])] = d['count'] uncategorized = Uncategorized(user) categories = [] for c in [uncategorized] + list(user.category_set.annotate(record_count=Count('record'))): categories.append({'id': c.id, 'name': c.name, 'count': c.record_count}) result = { 'name': user.username, 'joined_at': _serialize_datetime(user.date_joined), 'stats': stats, 'categories': categories, } if request.GET.get('include_library_items', 'true') == 'true': result['library_items'] = [{ 'id': record.id, 'title': record.title, 'status': _serialize_status(record), 'category': _category_as_dict(record.category or uncategorized), 'updated_at': _serialize_datetime(record.updated_at), } for record in user.record_set.order_by('-updated_at')] return result
def create_record(request): if 'work' in request.POST: title = request.POST['work'] work = get_or_create_work(title) else: return {"error": "work is required."} if 'status_type' not in request.POST: return {"error": "status_type is required."} status_type = StatusTypes.from_name(request.POST['status_type']) if status_type is None: return {"error": "status_type should be watching, finished, suspended, or interested."} record, created = request.user.record_set.get_or_create(work=work, defaults={'title': title}) history = request.user.history_set.create( work = work, status = request.POST.get('status_text', ''), status_type = status_type, comment = request.POST.get('comment', ''), ) result = _history_as_dict(history) result['record_id'] = record.id return result
def _work_as_dict(work, include_watchers=False): watchers = {'total': work.index.record_count} if include_watchers: for t in StatusTypes.types: watchers[t.name] = [] for record in work.record_set.order_by('status_type', 'user__username'): watchers[StatusTypes.to_name(record.status_type)].append(record.user.username) else: for t in StatusTypes.types: watchers[t.name] = 0 for d in work.record_set.values('status_type').annotate(count=Count('status_type')).order_by(): watchers[StatusTypes.to_name(d['status_type'])] = d['count'] return { 'title': work.title, 'rank': work.index.rank, 'watchers': watchers, }
def post(self, request, name): self.check_login() if request.user.username != name: self.raise_error('Permission denied.', status=403) title = request.POST.get('work_title') if not title: self.raise_error(u'작품 제목을 입력하세요.', status=400) # 400 Bad Request work = get_or_create_work(title) category_id = request.POST.get('category_id') if category_id: # TODO: Raise appropriate exception if not exist/no permission category = request.user.category_set.get(id=category_id) else: category = None try: record = Record.objects.get(user=request.user, work=work) self.raise_error(u'이미 같은 작품이 "%s"로 등록되어 있습니다.' % record.title, status=422) # 422 Unprocessable Entity except Record.DoesNotExist: record = Record.objects.create( user=request.user, work=work, title=title, category=category, ) history = History.objects.create( user=request.user, work=record.work, status='', status_type=StatusTypes.from_name(request.POST['status_type']), ) # Sync fields record.status = history.status record.status_type = history.status_type record.updated_at = history.updated_at return { 'record': serialize_record(record), 'post': serialize_post(history), }
def post(self, request, id): record = get_object_or_404(Record, id=id) self.check_login() if request.user.id != record.user_id: self.raise_error('Permission denied.', status=403) history = History.objects.create( user=request.user, work=record.work, status=request.POST['status'], status_type=StatusTypes.from_name(request.POST['status_type']), comment=request.POST['comment'], ) services = [] if request.POST.get('publish_twitter') == 'on': services.append('twitter') post_history(history, services) return { 'record': serialize_record(history.record), 'post': serialize_post(history), }