Пример #1
0
def page_summary(request):
  success = False
  user = request.user
  data = {}
  errors = {}
  errors['page_summary'] = []
  data['summary'] = {
    'summary': '',
  }

  if request.GET:
    url = process_url(request.GET.get('url'))

    try:
      p = Page.objects.get(url=url)
      s, s_created = Summary.objects.get_or_create(page=p)

      from_zone = tz.tzutc()
      to_zone = tz.tzlocal()

      date = s.date.replace(tzinfo=from_zone)
      local = date.astimezone(to_zone)

      data['summary'] = {
        'summary': s.summary,
        'user': s.last_editor.username,
        'date': local.strftime('%b %m, %Y,  %I:%M %p'),
      }
      success = True
    except:
      errors['page_summary'].append('Could not get page ' + url)

  if request.POST:
    url = process_url(request.POST.get('url'))
    domain = '{uri.netloc}'.format(uri=urlparse(url))
    summary = request.POST.get('summary')

    try:
      d, d_created = Domain.objects.get_or_create(url=domain)
      d.save()

      if len(Page.objects.filter(url=url)) == 0:
        p = Page(url=url, domain=d)
        p.save()
      else:
        p = Page.objects.get(url=url)

      s, s_created = Summary.objects.get_or_create(page=p)
      prev_summary = s.summary
      sh = SummaryHistory(user=user, new_summary=summary, previous_summary=prev_summary, summary=s)
      sh.save()

      s.summary = summary
      s.last_editor = user
      s.date = datetime.now()
      s.save()

      from_zone = tz.tzutc()
      to_zone = tz.tzlocal()

      local = s.date.replace(tzinfo=from_zone)

      data['summary'] = {
        'summary': summary,
        'user': user.username,
        'date': local.strftime('%b %m, %Y,  %I:%M %p'),
      }
      success = True
    except: 
      errors['page_summary'].append('Could not get page ' + url)

  return {
    'success': success,
    'errors': errors,
    'data': data,
  }
Пример #2
0
def initialize_page(request):
  tags = {}
  errors = {}
  user = request.user
  count_tags = False
  highlights = 0

  if request.POST:
    url = process_url(request.POST.get('url'))
    favIconUrl = request.POST.get('favIconUrl')
    domain_name = request.POST.get('domain_name')
    title = request.POST.get('title')
    add_usertags = request.POST.get('add_usertags')

    domain = '{uri.netloc}'.format(uri=urlparse(url))
    errors['add_page'] = []

    title = url if title == "" else title

    # Add domain
    d, d_created = Domain.objects.get_or_create(url=domain)
    if domain_name is not None:
      d.name = domain_name
    d.save()

    # Add page
    try: 
      p = Page.objects.get(url=url)
      p.title = title
      p.save()
    except:
      if len(Page.objects.filter(url=url)) == 0:
        p = Page(url=url, domain=d)
        p.title = title
        p.save()
        count_tags = True
      else:
        errors['add_page'].append("More than one page exists")

    if len(errors['add_page']) == 0:
      highlights = len(Highlight.objects.filter(page__url=url))
      vts = Tag.objects.filter(page__url=url, highlight=None)
      if len(vts) == 0:
        count_tags = True
      
      for vt in vts:
        vt_info = {
          'user_voted': False,
          'name': vt.common_tag.name,
          'color': vt.common_tag.color,
          'description': vt.common_tag.description,
          'is_private': vt.is_private,
          'vote_count': len(Vote.objects.filter(tag=vt)),
        }

        tags[vt.common_tag.name] = vt_info

        # Add tag to user
        if add_usertags == "true":
          uti, created = UserTagInfo.objects.get_or_create(user=user, page=p, tag=vt)
          uti.save()

      if count_tags:
        errors['get_tc'] = []
        try:
          tc = TagCollection.objects.get(subscribers=user)
          trie = json.loads(tc.trie_blob)
        except: 
          errors['get_tc'].append('User not subscribed')

        if len(errors['get_tc']) == 0:
          # Count value tags for page
          r = requests.get(url, verify=False)
          emotes = countEmote(r.text, trie)
          ts = [(e, emotes[e]) for e in emotes if e]
          sorted(ts, key=lambda x: x[1], reverse=True)

          errors['add_valuetags'] = []

          if len(ts) == 0:
            errors['add_valuetags'].append('No tags counted')

          count = 3
          for tag in ts:
            if tag[1] > 2 and count > 0:
              count -= 1
              name = tag[0]

              # Add tag to page
              try:
                vt = Tag.objects.get(page__url=url, common_tag__name=name, highlight=None)
              except Tag.DoesNotExist:
                try:
                  common_tag = CommonTag.objects.get(name=name)
                  vt = Tag(page=p, common_tag=common_tag, word_count=tag[1])
                  vt.save()

                  # Add tag to user
                  if add_usertags == "true":
                    uti, created = UserTagInfo.objects.get_or_create(user=user, page=p, tag=vt)
                    uti.save()
                except CommonTag.DoesNotExist:
                  errors['add_valuetags'].append("Could not get base tag")

              if len(errors['add_valuetags']) == 0:
                tags[name] = {
                  'name': name,
                  'color': vt.common_tag.color,
                  'description': vt.common_tag.description,
                }

      success = True
      for error_field in errors:
        if errors[error_field] != []:
          success = False

    return {
      'success': success,
      'errors': errors,
      'tags': tags,
      'highlights': highlights,
    }