<commit_before>########################################################################
# Rancho - Open Source Group/Project Management Tool
#    Copyright (C) 2008 The Rancho Team
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the 
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
########################################################################

from django.template.context import RequestContext
from django.shortcuts import render_to_response
from django.utils.translation import ugettext as _
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.contrib.auth.decorators import user_passes_test
from django.core import urlresolvers

from rancho.lib import utils
from rancho.project.models import Project
from rancho.company.forms import EditCompanySettingsForm, CreateCompanyForm
from rancho.company.models import Company

from rancho import settings


@login_required
@user_passes_test(lambda u: u.get_profile().is_account_owner)
def company_settings(request):
    
    user = request.user        
    projects = Project.objects.get_projects_for_user(user)

    company = Company.objects.get(main_company=True)
        
    if request.method=='POST':
        
        editCompanySettingsForm = EditCompanySettingsForm(request.POST,request.FILES)
         
        if editCompanySettingsForm.is_valid():
             
            company.short_name = editCompanySettingsForm.cleaned_data['short_name']
            company.long_name = editCompanySettingsForm.cleaned_data['long_name']
            company.description = editCompanySettingsForm.cleaned_data['description']
            company.mailing_address = editCompanySettingsForm.cleaned_data['mailing_address']
            company.phone = editCompanySettingsForm.cleaned_data['phone']
            company.webpage = editCompanySettingsForm.cleaned_data['webpage']
            company.display_logo_name = editCompanySettingsForm.cleaned_data['display_logo_name']
            logo = editCompanySettingsForm.cleaned_data['logo']
            if logo:
                utils.save_image(company, company.id, logo, settings.COMPANY_LOGO_SIZE, 'logo', 'JPEG')
            
            company.save()
            
            request.user.message_set.create(message=_("Company settings have been successfully edited."))         
    else :    
        data = {'short_name':company.short_name,
               'long_name':company.long_name,
               'description':company.description,
               'mailing_address':company.mailing_address,
               'phone':company.phone,
               'webpage':company.webpage,
               'display_logo_name':company.display_logo_name,
               'logo':company.logo}
        editCompanySettingsForm = EditCompanySettingsForm(data)
        
    context = {'edit_company': company,
                'projects': projects,
                'editCompanySettingsForm': editCompanySettingsForm,
                'is_main_company': True
                }        
            
    return render_to_response("company/settings.html", context, 
                              context_instance = RequestContext(request))


@login_required
@user_passes_test(lambda u: u.get_profile().is_account_owner)
def delete_logo(request, c_id=None):    
    if c_id:
        company = get_object_or_404(Company, id=c_id)
    else:
        company = Company.objects.get(main_company=True)
    
    if company.logo:
        company.logo.delete(save=False)
        company.logo = None
        company.save()
    
    return HttpResponse('')

@login_required
@user_passes_test(lambda u: u.get_profile().is_account_owner)
def create_company(request):
    
    if request.method == 'POST':
        form = CreateCompanyForm(request.POST)
        if form.is_valid():
            new_company = Company()
            new_company.short_name = form.cleaned_data['short_name']
            new_company.long_name = form.cleaned_data['long_name']
            new_company.save()
            
            request.user.message_set.create(message=_("Company %s have been successfully created."%new_company.short_name))
            return HttpResponseRedirect(urlresolvers.reverse('rancho.user.views.all_people'))            
    else:
        form = CreateCompanyForm()
    
    return render_to_response("company/create.html",
                              {'form': form}, 
                              context_instance = RequestContext(request))    
    

@login_required
@user_passes_test(lambda u: u.is_superuser)
def edit_company(request, c_id):    
    user = request.user
    company = get_object_or_404(Company, id=c_id)
    
    if company.main_company and user.get_profile().is_account_owner:
        return HttpResponseRedirect(urlresolvers.reverse('rancho.company.views.company_settings'))
    elif company.main_company:
        return HttpResponseRedirect(urlresolvers.reverse('rancho.user.views.all_people'))
    <commit_msg>Changed permissions on company's views to allow admin to use them<commit_after>########################################################################
# Rancho - Open Source Group/Project Management Tool
#    Copyright (C) 2008 The Rancho Team
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the 
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
########################################################################

from django.template.context import RequestContext
from django.shortcuts import render_to_response
from django.utils.translation import ugettext as _
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.contrib.auth.decorators import user_passes_test
from django.core import urlresolvers

from rancho.lib import utils
from rancho.project.models import Project
from rancho.company.forms import EditCompanySettingsForm, CreateCompanyForm
from rancho.company.models import Company

from rancho import settings


@login_required
@user_passes_test(lambda u: u.is_superuser)
def company_settings(request):
    
    user = request.user        
    projects = Project.objects.get_projects_for_user(user)

    company = Company.objects.get(main_company=True)
        
    if request.method=='POST':
        
        editCompanySettingsForm = EditCompanySettingsForm(request.POST,request.FILES)
         
        if editCompanySettingsForm.is_valid():
             
            company.short_name = editCompanySettingsForm.cleaned_data['short_name']
            company.long_name = editCompanySettingsForm.cleaned_data['long_name']
            company.description = editCompanySettingsForm.cleaned_data['description']
            company.mailing_address = editCompanySettingsForm.cleaned_data['mailing_address']
            company.phone = editCompanySettingsForm.cleaned_data['phone']
            company.webpage = editCompanySettingsForm.cleaned_data['webpage']
            company.display_logo_name = editCompanySettingsForm.cleaned_data['display_logo_name']
            logo = editCompanySettingsForm.cleaned_data['logo']
            if logo:
                utils.save_image(company, company.id, logo, settings.COMPANY_LOGO_SIZE, 'logo', 'JPEG')
            
            company.save()
            
            request.user.message_set.create(message=_("Company settings have been successfully edited."))         
    else :    
        data = {'short_name':company.short_name,
               'long_name':company.long_name,
               'description':company.description,
               'mailing_address':company.mailing_address,
               'phone':company.phone,
               'webpage':company.webpage,
               'display_logo_name':company.display_logo_name,
               'logo':company.logo}
        editCompanySettingsForm = EditCompanySettingsForm(data)
        
    context = {'edit_company': company,
                'projects': projects,
                'editCompanySettingsForm': editCompanySettingsForm,
                'is_main_company': True
                }        
            
    return render_to_response("company/settings.html", context, 
                              context_instance = RequestContext(request))


@login_required
@user_passes_test(lambda u: u.is_superuser)
def delete_logo(request, c_id=None):    
    if c_id:
        company = get_object_or_404(Company, id=c_id)
    else:
        company = Company.objects.get(main_company=True)
    
    if company.logo:
        company.logo.delete(save=False)
        company.logo = None
        company.save()
    
    return HttpResponse('')

@login_required
@user_passes_test(lambda u: u.is_superuser)
def create_company(request):
    
    if request.method == 'POST':
        form = CreateCompanyForm(request.POST)
        if form.is_valid():
            new_company = Company()
            new_company.short_name = form.cleaned_data['short_name']
            new_company.long_name = form.cleaned_data['long_name']
            new_company.save()
            
            request.user.message_set.create(message=_("Company %s have been successfully created."%new_company.short_name))
            return HttpResponseRedirect(urlresolvers.reverse('rancho.user.views.all_people'))            
    else:
        form = CreateCompanyForm()
    
    return render_to_response("company/create.html",
                              {'form': form}, 
                              context_instance = RequestContext(request))    
    

@login_required
@user_passes_test(lambda u: u.is_superuser)
def edit_company(request, c_id):    
    user = request.user
    company = get_object_or_404(Company, id=c_id)
    
    if company.main_company and user.get_profile().is_account_owner:
        return HttpResponseRedirect(urlresolvers.reverse('rancho.company.views.company_settings'))
    elif company.main_company:
        return HttpResponseRedirect(urlresolvers.reverse('rancho.user.views.all_people'))
    
{'fraction_non_alphanumeric': 0.10498029380150484, 'fraction_numerical': 0.0025080616266571123, 'mean_word_length': 3.0511611030478956, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3590394', 'n_tokens_mistral': 2942, 'n_tokens_neox': 2894, 'n_words': 664}