<commit_before>from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

import string

import urllib
import json
import locale
import hashlib
from PIL import Image

from django.core.exceptions import ValidationError
from django.shortcuts import get_object_or_404
from django.http import (HttpResponse, HttpResponseRedirect,
                         HttpResponseForbidden)
from django.views.decorators.http import etag, require_http_methods
from django.conf import settings
from django.contrib.gis.geos.point import Point
from django.utils.translation import ugettext as trans
from django.db import transaction
from django.db.models import Q

from treemap.util import json_api_call, render_template, instance_request
from treemap.search import create_filter
from treemap.audit import Audit
from treemap.models import (Plot, Tree, User, Species, Instance,
                            BenefitCurrencyConversion)

from ecobenefits.views import _benefits_for_trees

from opentreemap.util import json_from_request


def _plot_hash(request, instance, plot_id):
    instance_plots = instance.scope_model(Plot)
    return get_object_or_404(instance_plots, pk=plot_id).hash


def _search_hash(request, instance):
    audits = instance.scope_model(Audit)\
                     .order_by('-updated')

    try:
        audit_id_str = str(audits[0].pk)
    except IndexError:
        audit_id_str = 'none'

    eco_conversion = instance.eco_benefits_conversion

    if eco_conversion:
        eco_str = eco_conversion.hash
    else:
        eco_str = 'none'

    string_to_hash = audit_id_str + ":" + eco_str

    return hashlib.md5(string_to_hash).hexdigest()


#
# These are calls made by the API that aren't currently implemented
# as we make these features, please use these functions to share the
# love with mobile
#
def add_tree_photo(user_id, plot_id, uploaded_image):
    class TPShim(object):

        def __init__(self):
            self.pk = 2
            self.title = 'shim'

    return TPShim()


def _rotate_image_based_on_exif(img_path):
    img = Image.open(img_path)
    try:
        orientation = img._getexif()[0x0112]
        if orientation == 6:  # Right turn
            img = img.rotate(-90)
        elif orientation == 5:  # Left turn
            img = img.rotate(90)
    except:
        pass

    return img


def get_tree_photos(plot_id, photo_id):
    return None


def add_user_photo(user_id, uploaded_image):
    return None


def create_user(*args, **kwargs):
    # Clearly this is just getting the api working
    # it shouldn't stay here when real user stuff happens
    user = User(username=kwargs['username'], email=kwargs['email'])
    user.set_password(kwargs['password'])
    user.save()

    return user


def create_plot(user, instance, *args, **kwargs):
    if 'height' in kwargs and kwargs['height'] > 1000:
        return ["Height is too large."]

    if 'x' in kwargs and 'y' in kwargs:
        geom = Point(
            kwargs['x'],
            kwargs['y'])
    elif 'lon' in kwargs and 'lat' in kwargs:
        geom = Point(
            kwargs['lon'],
            kwargs['lat'])
    else:
        geom = Point(50, 50)

    p = Plot(geom=geom, instance=instance)
    p.save_with_user(user)

    if 'height' in kwargs:
        t = Tree(plot=p, instance=instance)
        t.height = kwargs['height']
        t.save_with_user(user)
    return p


def plot_detail(request, instance, plot_id):
    InstancePlot = instance.scope_model(Plot)
    plot = get_object_or_404(InstancePlot, pk=plot_id)
    tree = plot.current_tree()

    context = {}
    # If the the benefits calculation can't be done or fails, still display the
    # plot details
    if tree and tree.diameter is not None and tree.species.itree_code:
        try:<commit_msg>Fix plot detail view to check for species<commit_after>from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division

import string

import urllib
import json
import locale
import hashlib
from PIL import Image

from django.core.exceptions import ValidationError
from django.shortcuts import get_object_or_404
from django.http import (HttpResponse, HttpResponseRedirect,
                         HttpResponseForbidden)
from django.views.decorators.http import etag, require_http_methods
from django.conf import settings
from django.contrib.gis.geos.point import Point
from django.utils.translation import ugettext as trans
from django.db import transaction
from django.db.models import Q

from treemap.util import json_api_call, render_template, instance_request
from treemap.search import create_filter
from treemap.audit import Audit
from treemap.models import (Plot, Tree, User, Species, Instance,
                            BenefitCurrencyConversion)

from ecobenefits.views import _benefits_for_trees

from opentreemap.util import json_from_request


def _plot_hash(request, instance, plot_id):
    instance_plots = instance.scope_model(Plot)
    return get_object_or_404(instance_plots, pk=plot_id).hash


def _search_hash(request, instance):
    audits = instance.scope_model(Audit)\
                     .order_by('-updated')

    try:
        audit_id_str = str(audits[0].pk)
    except IndexError:
        audit_id_str = 'none'

    eco_conversion = instance.eco_benefits_conversion

    if eco_conversion:
        eco_str = eco_conversion.hash
    else:
        eco_str = 'none'

    string_to_hash = audit_id_str + ":" + eco_str

    return hashlib.md5(string_to_hash).hexdigest()


#
# These are calls made by the API that aren't currently implemented
# as we make these features, please use these functions to share the
# love with mobile
#
def add_tree_photo(user_id, plot_id, uploaded_image):
    class TPShim(object):

        def __init__(self):
            self.pk = 2
            self.title = 'shim'

    return TPShim()


def _rotate_image_based_on_exif(img_path):
    img = Image.open(img_path)
    try:
        orientation = img._getexif()[0x0112]
        if orientation == 6:  # Right turn
            img = img.rotate(-90)
        elif orientation == 5:  # Left turn
            img = img.rotate(90)
    except:
        pass

    return img


def get_tree_photos(plot_id, photo_id):
    return None


def add_user_photo(user_id, uploaded_image):
    return None


def create_user(*args, **kwargs):
    # Clearly this is just getting the api working
    # it shouldn't stay here when real user stuff happens
    user = User(username=kwargs['username'], email=kwargs['email'])
    user.set_password(kwargs['password'])
    user.save()

    return user


def create_plot(user, instance, *args, **kwargs):
    if 'height' in kwargs and kwargs['height'] > 1000:
        return ["Height is too large."]

    if 'x' in kwargs and 'y' in kwargs:
        geom = Point(
            kwargs['x'],
            kwargs['y'])
    elif 'lon' in kwargs and 'lat' in kwargs:
        geom = Point(
            kwargs['lon'],
            kwargs['lat'])
    else:
        geom = Point(50, 50)

    p = Plot(geom=geom, instance=instance)
    p.save_with_user(user)

    if 'height' in kwargs:
        t = Tree(plot=p, instance=instance)
        t.height = kwargs['height']
        t.save_with_user(user)
    return p


def plot_detail(request, instance, plot_id):
    InstancePlot = instance.scope_model(Plot)
    plot = get_object_or_404(InstancePlot, pk=plot_id)
    tree = plot.current_tree()

    context = {}
    # If the the benefits calculation can't be done or fails, still display the
    # plot details

    has_tree_diameter = tree is not None and tree.diameter is not None
    has_tree_species_with_code = tree is not None \
        and tree.species is not None and tree.species.itree_code is not None

    if has_tree_diameter and has_tree_species_with_code:
        try:
{'fraction_non_alphanumeric': 0.08176260825267448, 'fraction_numerical': 0.007896077432501274, 'mean_word_length': 3.113672079622839, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, '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': '7122285', 'n_tokens_mistral': 2488, 'n_tokens_neox': 2358, 'n_words': 694}