# Path: crowdataapp/models.py
# DEFAULT_TEMPLATE_JS = """// Javascript function to insert the document into the DOM.
# // Receives the URL of the document as its only parameter.
# // Must be called insertDocument
# // JQuery is available
# // resulting element should be inserted into div#document-viewer-container
# function insertDocument(document_url) {
# }
# """
# ALLOWED_FIELD_TYPES = (
#     forms_builder.forms.fields.TEXT,
#     forms_builder.forms.fields.TEXTAREA,
#     forms_builder.forms.fields.CHECKBOX,
#     forms_builder.forms.fields.CHECKBOX_MULTIPLE,
#     forms_builder.forms.fields.SELECT,
#     forms_builder.forms.fields.SELECT_MULTIPLE,
#     forms_builder.forms.fields.DATE,
#     forms_builder.forms.fields.DATE_TIME,
#     forms_builder.forms.fields.HIDDEN,
#     forms_builder.forms.fields.NUMBER,
#     forms_builder.forms.fields.URL,
# )
#     GROUPING_FUNCTIONS = (
#         ('AVG', _('Average')),
#         ('COUNT', _('Count')),
#         ('SUM', _('Sum')),
#     )
#     SUBQUERY_LABEL = """
#     SELECT distinct(document.id) AS document_id,
#           coalesce(canonical_label.value, field_entry.value) AS value,
#           canonical_label.id as canonical_label_id
#     FROM crowdataapp_documentsetfieldentry field_entry
#     LEFT OUTER JOIN crowdataapp_canonicalfieldentrylabel canonical_label ON canonical_label.id = field_entry.canonical_label_id
#     INNER JOIN crowdataapp_documentsetformentry form_entry ON form_entry.id = field_entry.entry_id
#     INNER JOIN crowdataapp_document document ON document.id = form_entry.document_id
#     WHERE document.document_set_id = %(document_set_id)d
#       AND field_entry.verified = TRUE
#       AND field_entry.field_id = %(label_field_id)d
#     """
#     SUBQUERY_MAGNITUDE = """
#     SELECT distinct(document.id) AS document_id,
#           cast(field_entry.value AS double PRECISION) AS value
#      FROM crowdataapp_documentsetfieldentry field_entry
#      INNER JOIN crowdataapp_documentsetformentry form_entry ON form_entry.id = field_entry.entry_id
#      INNER JOIN crowdataapp_document document ON document.id = form_entry.document_id
#      WHERE document.document_set_id = %(document_set_id)d
#        AND field_entry.verified = TRUE
#     """
# class Organization(models.Model):
# class UserProfile(models.Model):
# class DocumentSetManager(models.Manager):
# class DocumentSet(models.Model):
#     class Meta:
# class DocumentSetForm(forms_builder.forms.models.AbstractForm):
# class DocumentSetFormFieldManager(models.Manager):
# class DocumentSetFormField(forms_builder.forms.models.AbstractField):
# class DocumentSetFormEntry(forms_builder.forms.models.AbstractFormEntry):
# class DocumentSetFieldEntry(forms_builder.forms.models.AbstractFieldEntry):
# class DocumentSetRankingDefinition(models.Model):
# class Document(models.Model):
#     class Meta:
# class CanonicalFieldEntryLabel(models.Model):
#     def __unicode__(self):
#     def num_entries(self):
#     def get_query_set(self):
#     def __unicode__(self):
#     def get_absolute_url(self):
#     def admin_links(self):
#     def field_names(self):
#     def get_pending_documents(self):
#     def get_pending_documents_with_entries(self):
#     def get_pending_documents_count_for_user(self, user):
#     def get_pending_documents_for_user(self, user):
#     def get_verified_documents_count_for_user(self, user):
#     def get_verified_documents_for_user(self, user):
#     def get_verified_documents(self):
#     def get_reviewed_documents_count_for_user(self, user):
#     def leaderboard(self):
#     def userboard(self, user):
#     def organization_board(self):
#     def amount_on_field(self):
#     def get_absolute_url(self):
#     def visible(self):
#     def save(self, *args, **kwargs):
#     def delete(self, *args, **kwargs):
#     def to_dict(self):
#     def get_answer_for_field(self, field):
#     def force_verify(self):
#     def get_user_documents_without_organization(cls, user):
#     def assigned_canonical_value(self):
#     def get_canonical_value(self):
#     def save_without_setting_canon(self, *args, **kwargs):
#     def save(self, *args, **kwargs):
#     def _ranking_query(self, search_term=None, limit=None, offset=0):
#     def calculate(self):
#     def calculate_all(self, search_term):
#     def __unicode__(self):
#     def get_absolute_url(self):
#     def entries_threshold(self):
#     def is_verified_by_staff(self):
#     def is_revised_by_staff(self):
#     def verified_answers(self):
#     def force_verify(self):
#     def verify(self):
#     def unverify(self):
#     def has_entry_for_user(self, user):
#   def get_verified_documents(self, document_set):
#   def get_verified_documents_rankings(self, document_set, ranking_id):
#   def has_entries(self):
#   def reassign_entries_to(self, new_canon):
#   def __unicode__(self):


from urlparse import urlparse
from django.dispatch import receiver
from django.core.urlresolvers import resolve
from forms_builder.forms.signals import form_valid, form_invalid
from crowdataapp import models
# coding: utf-8




def create_entry(sender=None, form=None, entry=None, document_id=None, **kwargs):

    request = sender

    if not request.user.is_authenticated() or document_id is None:
        raise

    # if the document already has an entry for the user, raise

# Path: crowdataapp/models.py
# DEFAULT_TEMPLATE_JS = """// Javascript function to insert the document into the DOM.
# // Receives the URL of the document as its only parameter.
# // Must be called insertDocument
# // JQuery is available
# // resulting element should be inserted into div#document-viewer-container
# function insertDocument(document_url) {
# }
# """
# ALLOWED_FIELD_TYPES = (
#     forms_builder.forms.fields.TEXT,
#     forms_builder.forms.fields.TEXTAREA,
#     forms_builder.forms.fields.CHECKBOX,
#     forms_builder.forms.fields.CHECKBOX_MULTIPLE,
#     forms_builder.forms.fields.SELECT,
#     forms_builder.forms.fields.SELECT_MULTIPLE,
#     forms_builder.forms.fields.DATE,
#     forms_builder.forms.fields.DATE_TIME,
#     forms_builder.forms.fields.HIDDEN,
#     forms_builder.forms.fields.NUMBER,
#     forms_builder.forms.fields.URL,
# )
#     GROUPING_FUNCTIONS = (
#         ('AVG', _('Average')),
#         ('COUNT', _('Count')),
#         ('SUM', _('Sum')),
#     )
#     SUBQUERY_LABEL = """
#     SELECT distinct(document.id) AS document_id,
#           coalesce(canonical_label.value, field_entry.value) AS value,
#           canonical_label.id as canonical_label_id
#     FROM crowdataapp_documentsetfieldentry field_entry
#     LEFT OUTER JOIN crowdataapp_canonicalfieldentrylabel canonical_label ON canonical_label.id = field_entry.canonical_label_id
#     INNER JOIN crowdataapp_documentsetformentry form_entry ON form_entry.id = field_entry.entry_id
#     INNER JOIN crowdataapp_document document ON document.id = form_entry.document_id
#     WHERE document.document_set_id = %(document_set_id)d
#       AND field_entry.verified = TRUE
#       AND field_entry.field_id = %(label_field_id)d
#     """
#     SUBQUERY_MAGNITUDE = """
#     SELECT distinct(document.id) AS document_id,
#           cast(field_entry.value AS double PRECISION) AS value
#      FROM crowdataapp_documentsetfieldentry field_entry
#      INNER JOIN crowdataapp_documentsetformentry form_entry ON form_entry.id = field_entry.entry_id
#      INNER JOIN crowdataapp_document document ON document.id = form_entry.document_id
#      WHERE document.document_set_id = %(document_set_id)d
#        AND field_entry.verified = TRUE
#     """
# class Organization(models.Model):
# class UserProfile(models.Model):
# class DocumentSetManager(models.Manager):
# class DocumentSet(models.Model):
#     class Meta:
# class DocumentSetForm(forms_builder.forms.models.AbstractForm):
# class DocumentSetFormFieldManager(models.Manager):
# class DocumentSetFormField(forms_builder.forms.models.AbstractField):
# class DocumentSetFormEntry(forms_builder.forms.models.AbstractFormEntry):
# class DocumentSetFieldEntry(forms_builder.forms.models.AbstractFieldEntry):
# class DocumentSetRankingDefinition(models.Model):
# class Document(models.Model):
#     class Meta:
# class CanonicalFieldEntryLabel(models.Model):
#     def __unicode__(self):
#     def num_entries(self):
#     def get_query_set(self):
#     def __unicode__(self):
#     def get_absolute_url(self):
#     def admin_links(self):
#     def field_names(self):
#     def get_pending_documents(self):
#     def get_pending_documents_with_entries(self):
#     def get_pending_documents_count_for_user(self, user):
#     def get_pending_documents_for_user(self, user):
#     def get_verified_documents_count_for_user(self, user):
#     def get_verified_documents_for_user(self, user):
#     def get_verified_documents(self):
#     def get_reviewed_documents_count_for_user(self, user):
#     def leaderboard(self):
#     def userboard(self, user):
#     def organization_board(self):
#     def amount_on_field(self):
#     def get_absolute_url(self):
#     def visible(self):
#     def save(self, *args, **kwargs):
#     def delete(self, *args, **kwargs):
#     def to_dict(self):
#     def get_answer_for_field(self, field):
#     def force_verify(self):
#     def get_user_documents_without_organization(cls, user):
#     def assigned_canonical_value(self):
#     def get_canonical_value(self):
#     def save_without_setting_canon(self, *args, **kwargs):
#     def save(self, *args, **kwargs):
#     def _ranking_query(self, search_term=None, limit=None, offset=0):
#     def calculate(self):
#     def calculate_all(self, search_term):
#     def __unicode__(self):
#     def get_absolute_url(self):
#     def entries_threshold(self):
#     def is_verified_by_staff(self):
#     def is_revised_by_staff(self):
#     def verified_answers(self):
#     def force_verify(self):
#     def verify(self):
#     def unverify(self):
#     def has_entry_for_user(self, user):
#   def get_verified_documents(self, document_set):
#   def get_verified_documents_rankings(self, document_set, ranking_id):
#   def has_entries(self):
#   def reassign_entries_to(self, new_canon):
#   def __unicode__(self):


# Path: crowdataapp/signals.py
from urlparse import urlparse
from django.dispatch import receiver
from django.core.urlresolvers import resolve
from forms_builder.forms.signals import form_valid, form_invalid
from crowdataapp import models

# coding: utf-8




def create_entry(sender=None, form=None, entry=None, document_id=None, **kwargs):

    request = sender

    if not request.user.is_authenticated() or document_id is None:
        raise

    # if the document already has an entry for the user, raise

    if models.Document.objects.get(pk=document_id).has_entry_for_user(request.user):