<commit_before>{
	"includes": [
		"./common.gypi",
		"./libgdal_formats.gypi"
	],
	"targets": [
		{
			"target_name": "libgdal",
			"type": "static_library",
			"sources": [
				"gdal/frmts/gdalallregister.cpp",

				"gdal/ogr/osr_cs_wkt.c",
				"gdal/ogr/osr_cs_wkt_parser.c",
				"gdal/ogr/gml2ogrgeometry.cpp",
				"gdal/ogr/ogr2gmlgeometry.cpp",
				"gdal/ogr/ogr_api.cpp",
				"gdal/ogr/ogr_expat.cpp",
				"gdal/ogr/ogr_fromepsg.cpp",
				"gdal/ogr/ogr_geocoding.cpp",
				"gdal/ogr/ogr_opt.cpp",
				"gdal/ogr/ogr_srs_dict.cpp",
				"gdal/ogr/ogr_srs_erm.cpp",
				"gdal/ogr/ogr_srs_esri.cpp",
				"gdal/ogr/ogr_srs_ozi.cpp",
				"gdal/ogr/ogr_srs_panorama.cpp",
				"gdal/ogr/ogr_srs_pci.cpp",
				"gdal/ogr/ogr_srs_proj4.cpp",
				"gdal/ogr/ogr_srs_usgs.cpp",
				"gdal/ogr/ogr_srs_validate.cpp",
				"gdal/ogr/ogr_srs_xml.cpp",
				"gdal/ogr/ogr_srsnode.cpp",
				"gdal/ogr/ograssemblepolygon.cpp",
				"gdal/ogr/ogrct.cpp",
				"gdal/ogr/ogrcurve.cpp",
				"gdal/ogr/ogrfeature.cpp",
				"gdal/ogr/ogrfeaturedefn.cpp",
				"gdal/ogr/ogrfeaturequery.cpp",
				"gdal/ogr/ogrfeaturestyle.cpp",
				"gdal/ogr/ogrfielddefn.cpp",
				"gdal/ogr/ogrgeomediageometry.cpp",
				"gdal/ogr/ogrgeometry.cpp",
				"gdal/ogr/ogrgeometrycollection.cpp",
				"gdal/ogr/ogrgeometryfactory.cpp",
				"gdal/ogr/ogrgeomfielddefn.cpp",
				"gdal/ogr/ogrlinearring.cpp",
				"gdal/ogr/ogrlinestring.cpp",
				"gdal/ogr/ogrmultilinestring.cpp",
				"gdal/ogr/ogrmultipoint.cpp",
				"gdal/ogr/ogrmultipolygon.cpp",
				"gdal/ogr/ogrpgeogeometry.cpp",
				"gdal/ogr/ogrpoint.cpp",
				"gdal/ogr/ogrpolygon.cpp",
				"gdal/ogr/ogrspatialreference.cpp",
				"gdal/ogr/ogrsurface.cpp",
				"gdal/ogr/ogrutils.cpp",
				"gdal/ogr/swq.cpp",
				"gdal/ogr/swq_expr_node.cpp",
				"gdal/ogr/swq_op_general.cpp",
				"gdal/ogr/swq_op_registrar.cpp",
				"gdal/ogr/swq_parser.cpp",
				"gdal/ogr/swq_select.cpp",

				"gdal/alg/contour.cpp",
				"gdal/alg/fpolygonize.cpp",
				"gdal/alg/gdal_octave.cpp",
				"gdal/alg/gdal_rpc.cpp",
				"gdal/alg/gdal_simplesurf.cpp",
				"gdal/alg/gdal_tps.cpp",
				"gdal/alg/gdalchecksum.cpp",
				"gdal/alg/gdalcutline.cpp",
				"gdal/alg/gdaldither.cpp",
				"gdal/alg/gdalgeoloc.cpp",
				"gdal/alg/gdalgrid.cpp",
				"gdal/alg/gdalgridavx.cpp",
				"gdal/alg/gdalmatching.cpp",
				"gdal/alg/gdalmediancut.cpp",
				"gdal/alg/gdalproximity.cpp",
				"gdal/alg/gdalrasterfpolygonenumerator.cpp",
				"gdal/alg/gdalrasterize.cpp",
				"gdal/alg/gdalrasterpolygonenumerator.cpp",
				"gdal/alg/gdalsievefilter.cpp",
				"gdal/alg/gdalsimplewarp.cpp",
				"gdal/alg/gdaltransformer.cpp",
				"gdal/alg/gdaltransformgeolocs.cpp",
				"gdal/alg/gdalwarper.cpp",
				"gdal/alg/gdalwarpkernel.cpp",
				"gdal/alg/gdalwarpoperation.cpp",
				"gdal/alg/llrasterize.cpp",
				"gdal/alg/polygonize.cpp",
				"gdal/alg/rasterfill.cpp",
				"gdal/alg/thinplatespline.cpp",
				"gdal/alg/gdal_crs.c",
				"gdal/alg/gdal_nrgcrs.c",
				"gdal/alg/gdalwarpkernel_opencl.c",

				"gdal/bridge/gdalbridge.cpp",
				"gdal/bridge/gbgetsymbol.cpp",

				"gdal/gcore/gdal_misc.cpp",
				"gdal/gcore/gdal_rat.cpp",
				"gdal/gcore/gdal_rpcimdio.cpp",
				"gdal/gcore/gdalallvalidmaskband.cpp",
				"gdal/gcore/gdalclientserver.cpp",
				"gdal/gcore/gdalcolortable.cpp",
				"gdal/gcore/gdaldataset.cpp",
				"gdal/gcore/gdaldefaultasync.cpp",
				"gdal/gcore/gdaldefaultoverviews.cpp",
				"gdal/gcore/gdaldllmain.cpp",
				"gdal/gcore/gdaldriver.cpp",
				"gdal/gcore/gdaldrivermanager.cpp",
				"gdal/gcore/gdalexif.cpp",
				"gdal/gcore/gdalgeorefpamdataset.cpp",
				"gdal/gcore/gdalgmlcoverage.cpp",
				"gdal/gcore/gdaljp2abstractdataset.cpp",
				"gdal/gcore/gdaljp2box.cpp",
				"gdal/gcore/gdaljp2metadata.cpp",
				"gdal/gcore/gdalmajorobject.cpp",
				"gdal/gcore/gdalmultidomainmetadata.cpp",
				"gdal/gcore/gdalnodatamaskband.cpp",
				"gdal/gcore/gdalnodatavaluesmaskband.cpp",
				"gdal/gcore/gdalopeninfo.cpp",
				"gdal/gcore/gdalpamdataset.cpp",
				"gdal/gcore/gdalpamproxydb.cpp",
				"gdal/gcore/gdalpamrasterband.cpp",
				"gdal/gcore/gdalproxydataset.cpp",
				"gdal/gcore/gdalproxypool.cpp",
				"gdal/gcore/gdalrasterband.cpp",
				"gdal/gcore/gdalrasterblock.cpp",
				"gdal/gcore/gdalvirtualmem.cpp",
				"gdal/gcore/jp2dump.cpp",
				"gdal/gcore/overview.cpp",
				"gdal/gcore/rasterio.cpp",

				# "gdal/port/cpl_odbc.cpp",
				# "gdal/port/cpl_win32ce_api.cpp",
				# "gdal/port/vsipreload.cpp",
				"gdal/port/cpl_atomic_ops.cpp",
				"gdal/port/cpl_base64.cpp",
				"gdal/port/cpl_conv.cpp",
				"gdal/port/cpl_csv.cpp",
				"gdal/port/cpl_error.cpp",
				"gdal/port/cpl_findfile.cpp",
				"gdal/port/cpl_getexecpath.cpp",
				"gdal/port/cpl_google_oauth2.cpp",
				"gdal/port/cpl_hash_set.cpp",
				"gdal/port/cpl_http.cpp",
				"gdal/port/cpl_list.cpp",
				"gdal/port/cpl_minixml.cpp",
				"gdal/port/cpl_minizip_ioapi.cpp",
				"gdal/port/cpl_minizip_unzip.cpp",
				"gdal/port/cpl_minizip_zip.cpp",
				"gdal/port/cpl_multiproc.cpp",
				"gdal/port/cpl_path.cpp",
				"gdal/port/cpl_progress.cpp",
				"gdal/port/cpl_quad_tree.cpp",
				"gdal/port/cpl_recode.cpp",
				"gdal/port/cpl_recode_iconv.cpp",
				"gdal/port/cpl_recode_stub.cpp",
				"gdal/port/cpl_spawn.cpp",
				"gdal/port/cpl_string.cpp",
				"gdal/port/cpl_strtod.cpp",
				"gdal/port/cpl_time.cpp",
				"gdal/port/cpl_virtualmem.cpp",
				"gdal/port/cpl_vsi_mem.cpp",
				"gdal/port/cpl_vsil.cpp",
				"gdal/port/cpl_vsil_abstract_archive.cpp",
				"gdal/port/cpl_vsil_buffered_reader.cpp",
				"gdal/port/cpl_vsil_cache.cpp",
				"gdal/port/cpl_vsil_curl.cpp",
				"gdal/port/cpl_vsil_curl_streaming.cpp",
				"gdal/port/cpl_vsil_gzip.cpp",
				"gdal/port/cpl_vsil_simple.cpp",
				"gdal/port/cpl_vsil_sparsefile.cpp",
				"gdal/port/cpl_vsil_stdin.cpp",
				"gdal/port/cpl_vsil_stdout.cpp",
				"gdal/port/cpl_vsil_subfile.cpp",
				"gdal/port/cpl_vsil_tar.cpp",
				"gdal/port/cpl_vsil_unix_stdio_64.cpp",
				"gdal/port/cpl_vsil_win32.cpp",
				"gdal/port/cpl_vsisimple.cpp",
				"gdal/port/cpl_xml_validate.cpp",
				"gdal/port/cplgetsymbol.cpp",
				"gdal/port/cplkeywordparser.cpp",
				"gdal/port/cplstring.cpp",
				"gdal/port/cplstringlist.cpp",
				"gdal/port/xmlreformat.cpp"
			],
			"include_dirs": [
				"./gdal/alg",
				"./gdal/gcore",
				"./gdal/port",
				"./gdal/bridge",
				"./gdal/frmts",
				"./gdal/frmts/zlib",
				"./gdal/ogr",
				"./gdal/ogr/ogrsf_frmts",
				"./gdal/ogr/ogrsf_frmts/mem"
			],
			"dependencies": [
				'<@(gdal_format_gyps)'
            ],
			"defines": [
				'<@(gdal_format_defs)'
			],
			"direct_dependent_settings": {
				"include_dirs": [
					"./gdal/alg",
					"./gdal/gcore",
					"./gdal/port",
					"./gdal/bridge",
					"./gdal/ogr"
				],
				"conditions": [
					["OS == 'win'", {
						"include_dirs": ["./arch/win"]
					}, {
						"include_dirs": ["./arch/unix"]
					}]
				],
				"defines": [
					"_LARGEFILE_SOURCE",
					"_FILE_OFFSET_BITS=64"
				]
			}
		}
	]
}
<commit_msg>Removed unneeded components: gdal_nrgcrs, gdalclientserver.<commit_after>{
	"includes": [
		"./common.gypi",
		"./libgdal_formats.gypi"
	],
	"targets": [
		{
			"target_name": "libgdal",
			"type": "static_library",
			"sources": [
				"gdal/frmts/gdalallregister.cpp",

				"gdal/ogr/osr_cs_wkt.c",
				"gdal/ogr/osr_cs_wkt_parser.c",
				"gdal/ogr/gml2ogrgeometry.cpp",
				"gdal/ogr/ogr2gmlgeometry.cpp",
				"gdal/ogr/ogr_api.cpp",
				"gdal/ogr/ogr_expat.cpp",
				"gdal/ogr/ogr_fromepsg.cpp",
				"gdal/ogr/ogr_geocoding.cpp",
				"gdal/ogr/ogr_opt.cpp",
				"gdal/ogr/ogr_srs_dict.cpp",
				"gdal/ogr/ogr_srs_erm.cpp",
				"gdal/ogr/ogr_srs_esri.cpp",
				"gdal/ogr/ogr_srs_ozi.cpp",
				"gdal/ogr/ogr_srs_panorama.cpp",
				"gdal/ogr/ogr_srs_pci.cpp",
				"gdal/ogr/ogr_srs_proj4.cpp",
				"gdal/ogr/ogr_srs_usgs.cpp",
				"gdal/ogr/ogr_srs_validate.cpp",
				"gdal/ogr/ogr_srs_xml.cpp",
				"gdal/ogr/ogr_srsnode.cpp",
				"gdal/ogr/ograssemblepolygon.cpp",
				"gdal/ogr/ogrct.cpp",
				"gdal/ogr/ogrcurve.cpp",
				"gdal/ogr/ogrfeature.cpp",
				"gdal/ogr/ogrfeaturedefn.cpp",
				"gdal/ogr/ogrfeaturequery.cpp",
				"gdal/ogr/ogrfeaturestyle.cpp",
				"gdal/ogr/ogrfielddefn.cpp",
				"gdal/ogr/ogrgeomediageometry.cpp",
				"gdal/ogr/ogrgeometry.cpp",
				"gdal/ogr/ogrgeometrycollection.cpp",
				"gdal/ogr/ogrgeometryfactory.cpp",
				"gdal/ogr/ogrgeomfielddefn.cpp",
				"gdal/ogr/ogrlinearring.cpp",
				"gdal/ogr/ogrlinestring.cpp",
				"gdal/ogr/ogrmultilinestring.cpp",
				"gdal/ogr/ogrmultipoint.cpp",
				"gdal/ogr/ogrmultipolygon.cpp",
				"gdal/ogr/ogrpgeogeometry.cpp",
				"gdal/ogr/ogrpoint.cpp",
				"gdal/ogr/ogrpolygon.cpp",
				"gdal/ogr/ogrspatialreference.cpp",
				"gdal/ogr/ogrsurface.cpp",
				"gdal/ogr/ogrutils.cpp",
				"gdal/ogr/swq.cpp",
				"gdal/ogr/swq_expr_node.cpp",
				"gdal/ogr/swq_op_general.cpp",
				"gdal/ogr/swq_op_registrar.cpp",
				"gdal/ogr/swq_parser.cpp",
				"gdal/ogr/swq_select.cpp",

				"gdal/alg/contour.cpp",
				"gdal/alg/fpolygonize.cpp",
				"gdal/alg/gdal_octave.cpp",
				"gdal/alg/gdal_rpc.cpp",
				"gdal/alg/gdal_simplesurf.cpp",
				"gdal/alg/gdal_tps.cpp",
				"gdal/alg/gdalchecksum.cpp",
				"gdal/alg/gdalcutline.cpp",
				"gdal/alg/gdaldither.cpp",
				"gdal/alg/gdalgeoloc.cpp",
				"gdal/alg/gdalgrid.cpp",
				"gdal/alg/gdalgridavx.cpp",
				"gdal/alg/gdalmatching.cpp",
				"gdal/alg/gdalmediancut.cpp",
				"gdal/alg/gdalproximity.cpp",
				"gdal/alg/gdalrasterfpolygonenumerator.cpp",
				"gdal/alg/gdalrasterize.cpp",
				"gdal/alg/gdalrasterpolygonenumerator.cpp",
				"gdal/alg/gdalsievefilter.cpp",
				"gdal/alg/gdalsimplewarp.cpp",
				"gdal/alg/gdaltransformer.cpp",
				"gdal/alg/gdaltransformgeolocs.cpp",
				"gdal/alg/gdalwarper.cpp",
				"gdal/alg/gdalwarpkernel.cpp",
				"gdal/alg/gdalwarpoperation.cpp",
				"gdal/alg/llrasterize.cpp",
				"gdal/alg/polygonize.cpp",
				"gdal/alg/rasterfill.cpp",
				"gdal/alg/thinplatespline.cpp",
				"gdal/alg/gdal_crs.c",
				# "gdal/alg/gdal_nrgcrs.c",
				"gdal/alg/gdalwarpkernel_opencl.c",

				"gdal/bridge/gdalbridge.cpp",
				"gdal/bridge/gbgetsymbol.cpp",

				"gdal/gcore/gdal_misc.cpp",
				"gdal/gcore/gdal_rat.cpp",
				"gdal/gcore/gdal_rpcimdio.cpp",
				"gdal/gcore/gdalallvalidmaskband.cpp",
				# "gdal/gcore/gdalclientserver.cpp",
				"gdal/gcore/gdalcolortable.cpp",
				"gdal/gcore/gdaldataset.cpp",
				"gdal/gcore/gdaldefaultasync.cpp",
				"gdal/gcore/gdaldefaultoverviews.cpp",
				"gdal/gcore/gdaldllmain.cpp",
				"gdal/gcore/gdaldriver.cpp",
				"gdal/gcore/gdaldrivermanager.cpp",
				"gdal/gcore/gdalexif.cpp",
				"gdal/gcore/gdalgeorefpamdataset.cpp",
				"gdal/gcore/gdalgmlcoverage.cpp",
				"gdal/gcore/gdaljp2abstractdataset.cpp",
				"gdal/gcore/gdaljp2box.cpp",
				"gdal/gcore/gdaljp2metadata.cpp",
				"gdal/gcore/gdalmajorobject.cpp",
				"gdal/gcore/gdalmultidomainmetadata.cpp",
				"gdal/gcore/gdalnodatamaskband.cpp",
				"gdal/gcore/gdalnodatavaluesmaskband.cpp",
				"gdal/gcore/gdalopeninfo.cpp",
				"gdal/gcore/gdalpamdataset.cpp",
				"gdal/gcore/gdalpamproxydb.cpp",
				"gdal/gcore/gdalpamrasterband.cpp",
				"gdal/gcore/gdalproxydataset.cpp",
				"gdal/gcore/gdalproxypool.cpp",
				"gdal/gcore/gdalrasterband.cpp",
				"gdal/gcore/gdalrasterblock.cpp",
				"gdal/gcore/gdalvirtualmem.cpp",
				"gdal/gcore/jp2dump.cpp",
				"gdal/gcore/overview.cpp",
				"gdal/gcore/rasterio.cpp",

				# "gdal/port/cpl_odbc.cpp",
				# "gdal/port/cpl_win32ce_api.cpp",
				# "gdal/port/vsipreload.cpp",
				"gdal/port/cpl_atomic_ops.cpp",
				"gdal/port/cpl_base64.cpp",
				"gdal/port/cpl_conv.cpp",
				"gdal/port/cpl_csv.cpp",
				"gdal/port/cpl_error.cpp",
				"gdal/port/cpl_findfile.cpp",
				"gdal/port/cpl_getexecpath.cpp",
				"gdal/port/cpl_google_oauth2.cpp",
				"gdal/port/cpl_hash_set.cpp",
				"gdal/port/cpl_http.cpp",
				"gdal/port/cpl_list.cpp",
				"gdal/port/cpl_minixml.cpp",
				"gdal/port/cpl_minizip_ioapi.cpp",
				"gdal/port/cpl_minizip_unzip.cpp",
				"gdal/port/cpl_minizip_zip.cpp",
				"gdal/port/cpl_multiproc.cpp",
				"gdal/port/cpl_path.cpp",
				"gdal/port/cpl_progress.cpp",
				"gdal/port/cpl_quad_tree.cpp",
				"gdal/port/cpl_recode.cpp",
				"gdal/port/cpl_recode_iconv.cpp",
				"gdal/port/cpl_recode_stub.cpp",
				"gdal/port/cpl_spawn.cpp",
				"gdal/port/cpl_string.cpp",
				"gdal/port/cpl_strtod.cpp",
				"gdal/port/cpl_time.cpp",
				"gdal/port/cpl_virtualmem.cpp",
				"gdal/port/cpl_vsi_mem.cpp",
				"gdal/port/cpl_vsil.cpp",
				"gdal/port/cpl_vsil_abstract_archive.cpp",
				"gdal/port/cpl_vsil_buffered_reader.cpp",
				"gdal/port/cpl_vsil_cache.cpp",
				"gdal/port/cpl_vsil_curl.cpp",
				"gdal/port/cpl_vsil_curl_streaming.cpp",
				"gdal/port/cpl_vsil_gzip.cpp",
				"gdal/port/cpl_vsil_simple.cpp",
				"gdal/port/cpl_vsil_sparsefile.cpp",
				"gdal/port/cpl_vsil_stdin.cpp",
				"gdal/port/cpl_vsil_stdout.cpp",
				"gdal/port/cpl_vsil_subfile.cpp",
				"gdal/port/cpl_vsil_tar.cpp",
				"gdal/port/cpl_vsil_unix_stdio_64.cpp",
				"gdal/port/cpl_vsil_win32.cpp",
				"gdal/port/cpl_vsisimple.cpp",
				"gdal/port/cpl_xml_validate.cpp",
				"gdal/port/cplgetsymbol.cpp",
				"gdal/port/cplkeywordparser.cpp",
				"gdal/port/cplstring.cpp",
				"gdal/port/cplstringlist.cpp",
				"gdal/port/xmlreformat.cpp"
			],
			"include_dirs": [
				"./gdal/alg",
				"./gdal/gcore",
				"./gdal/port",
				"./gdal/bridge",
				"./gdal/frmts",
				"./gdal/frmts/zlib",
				"./gdal/ogr",
				"./gdal/ogr/ogrsf_frmts",
				"./gdal/ogr/ogrsf_frmts/mem"
			],
			"dependencies": [
				'<@(gdal_format_gyps)'
            ],
			"defines": [
				'<@(gdal_format_defs)'
			],
			"direct_dependent_settings": {
				"include_dirs": [
					"./gdal/alg",
					"./gdal/gcore",
					"./gdal/port",
					"./gdal/bridge",
					"./gdal/ogr"
				],
				"conditions": [
					["OS == 'win'", {
						"include_dirs": ["./arch/win"]
					}, {
						"include_dirs": ["./arch/unix"]
					}]
				],
				"defines": [
					"_LARGEFILE_SOURCE",
					"_FILE_OFFSET_BITS=64"
				]
			}
		}
	]
}
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Base management command that provides common functionality for the other commands in this app.
"""
import codecs
import locale
import logging
import os
import requests
import sys
from re import sub
from email.utils import parsedate_tz, mktime_tz
from datetime import datetime
from django.utils import timezone
from django.utils.termcolors import colorize
from django.core.management.base import BaseCommand
from calaccess_raw import get_data_directory
from calaccess_raw.models import RawDataVersion
logger = logging.getLogger(__name__)

class CalAccessCommand(BaseCommand):
    """
    Base management command that provides common functionality for the other commands in this app.
    """
    url = 'https://campaignfinance.cdn.sos.ca.gov/dbwebexport.zip'

    def handle(self, *args, **options):
        """
        The generic handler function.

        Any command subclassing this object should implement its own
        handle method, as is standard in Django, and run this method
        via a super call to inherit its functionality.
        """
        self.set_global_options(options)

    def set_global_options(self, options):
        """
        Set options to all commands.
        """
        # Set global options
        self.verbosity = options.get("verbosity")
        self.no_color = options.get("no_color")

        # set up data directories
        self.data_dir = get_data_directory()
        self.tsv_dir = os.path.join(self.data_dir, 'tsv')
        self.csv_dir = os.path.join(self.data_dir, 'csv')

        os.path.exists(self.data_dir) or os.makedirs(self.data_dir)
        os.path.exists(self.tsv_dir) or os.makedirs(self.tsv_dir)
        os.path.exists(self.csv_dir) or os.makedirs(self.csv_dir)

        # set path where zip will be downloaded
        self.download_dir = os.path.join(self.data_dir, 'download')
        self.zip_path = os.path.join(
            self.download_dir,
            self.url.split('/')[-1]
        )

        # Start the clock
        self.start_datetime = datetime.now()

    def parse_imf_datetime_str(self, datetime_str):
        """
        Parse a string containing a datetime value in Internet Message Format.

        See Section 7.1.1.1 of RFC 7231:
        https://tools.ietf.org/html/rfc7231.html#section-7.1.1.1

        Return a utc datetime object.
        """
        datetime_tuple = parsedate_tz(datetime_str)
        timestamp = mktime_tz(datetime_tuple)
        datetime_obj = datetime.fromtimestamp(timestamp, timezone.utc)
        return datetime_obj

    def get_download_metadata(self):
        """
        Returns a dict with metadata about the current CAL-ACCESS snapshot.
        """
        response = requests.head(self.url)
        logger.debug(
            'Response status {0.status_code} ({0.reason}) from HEAD request.'.format(response)
        )
        if not response.ok:
            response.raise_for_status()
        # content length is a string, need to convert
        try:
            # long int type is big enough for double the current size of the zip
            length = long(response.headers['content-length'])
        except NameError:
            # in py3, no long(), instead int will suffice
            length = int(response.headers['content-length'])
        return {
            # should prob not call int here, can this remain a string until writing to db?
            'content-length': length,
            'last-modified': response.headers['last-modified'],
            'etag': response.headers['etag'],
            'server': response.headers['server'],
        }

    def get_or_create_version(self, expected_size, release_datetime):
        """
        Get or create a RawDataVersion.

        Create a new one only if:
        * expected_size is different from the latest version; or
        * release_datetime is five minutes later than latest_version's.

        Return a tuple of (object, created), where created is a boolean
        specifying whether an object was created.
        """
        obj = None
        try:
            latest = RawDataVersion.objects.latest('release_datetime')
        except RawDataVersion.DoesNotExist:
            obj = RawDataVersion.objects.create(
                release_datetime=release_datetime,
                expected_size=expected_size
            )
            created = True
        else:
            diff = release_datetime - latest.release_datetime
            if latest.expected_size == expected_size and diff.total_seconds() < 300:
                obj = latest
                created = False
            else:
                obj = RawDataVersion.objects.create(
                    release_datetime=release_datetime,
                    expected_size=expected_size,
                )
                created = True
        return obj, created

    #
    # Logging methods
    #

    def header(self, string):
        """
        Writes out a string to stdout formatted to look like a header.
        """
        logger.debug(string)
        if not self.no_color:
            string = colorize(string, fg="cyan", opts=("bold",))
        self.stdout.write(string)

    def log(self, string):
        """
        Writes out a string to stdout formatted to look like a standard line.
        """
        logger.debug(string)
        if not self.no_color:
            string = colorize("%s" % string, fg="white")
        self.stdout.write(string)

    def success(self, string):
        """
        Writes out a string to stdout formatted green to communicate success.
        """
        logger.debug(string)
        if not self.no_color:
            string = colorize(string, fg="green")
        self.stdout.write(string)

    def failure(self, string):
        """
        Writes out a string to stdout formatted red to communicate failure.
        """
        logger.debug(string)
        if not self.no_color:
            string = colorize(string, fg="red")
        self.stdout.write(string)

    def duration(self):
        """
        Calculates how long the command has been running and writes it to stdout.
        """
        duration = datetime.now() - self.start_datetime
        self.stdout.write('Duration: {}'.format(str(duration)))
        logger.debug('Duration: {}'.format(str(duration)))

    def confirm_proceed(self, prompt):
        """
        Prompts the user for yes/no confirmation to proceed.
        """
        # Ensure stdout can handle Unicode data: http://bit.ly/1C3l4eV
        locale_encoding = locale.getpreferredencoding()
        old_stdout = sys.stdout
        sys.stdout = codecs.getwriter(locale_encoding)(sys.stdout)

        # Send the confirmation prompt out to the user
        user_input = input(prompt)

        confirm = None

        while confirm is None:
            if user_input.lower() in ['y', 'yes']:
                confirm = True
            elif user_input.lower() in ['n', 'no']:
                confirm = False
            else:
                user_input = input("Invalid input. Please type 'yes', 'no', 'y' or 'n':\n")

        # Set things back to the way they were before continuing.
        sys.stdout = old_stdout

        # Pass back what the user typed
        return confirm

    def __str__(self):
        return sub(r'(.+\.)*', '', self.__class__.__module__)
<commit_msg>Verify=False<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Base management command that provides common functionality for the other commands in this app.
"""
import codecs
import locale
import logging
import os
import requests
import sys
from re import sub
from email.utils import parsedate_tz, mktime_tz
from datetime import datetime
from django.utils import timezone
from django.utils.termcolors import colorize
from django.core.management.base import BaseCommand
from calaccess_raw import get_data_directory
from calaccess_raw.models import RawDataVersion
logger = logging.getLogger(__name__)

class CalAccessCommand(BaseCommand):
    """
    Base management command that provides common functionality for the other commands in this app.
    """
    url = 'https://campaignfinance.cdn.sos.ca.gov/dbwebexport.zip'

    def handle(self, *args, **options):
        """
        The generic handler function.

        Any command subclassing this object should implement its own
        handle method, as is standard in Django, and run this method
        via a super call to inherit its functionality.
        """
        self.set_global_options(options)

    def set_global_options(self, options):
        """
        Set options to all commands.
        """
        # Set global options
        self.verbosity = options.get("verbosity")
        self.no_color = options.get("no_color")

        # set up data directories
        self.data_dir = get_data_directory()
        self.tsv_dir = os.path.join(self.data_dir, 'tsv')
        self.csv_dir = os.path.join(self.data_dir, 'csv')

        os.path.exists(self.data_dir) or os.makedirs(self.data_dir)
        os.path.exists(self.tsv_dir) or os.makedirs(self.tsv_dir)
        os.path.exists(self.csv_dir) or os.makedirs(self.csv_dir)

        # set path where zip will be downloaded
        self.download_dir = os.path.join(self.data_dir, 'download')
        self.zip_path = os.path.join(
            self.download_dir,
            self.url.split('/')[-1]
        )

        # Start the clock
        self.start_datetime = datetime.now()

    def parse_imf_datetime_str(self, datetime_str):
        """
        Parse a string containing a datetime value in Internet Message Format.

        See Section 7.1.1.1 of RFC 7231:
        https://tools.ietf.org/html/rfc7231.html#section-7.1.1.1

        Return a utc datetime object.
        """
        datetime_tuple = parsedate_tz(datetime_str)
        timestamp = mktime_tz(datetime_tuple)
        datetime_obj = datetime.fromtimestamp(timestamp, timezone.utc)
        return datetime_obj

    def get_download_metadata(self):
        """
        Returns a dict with metadata about the current CAL-ACCESS snapshot.
        """
        response = requests.head(self.url, verify=False)
        logger.debug(
            'Response status {0.status_code} ({0.reason}) from HEAD request.'.format(response)
        )
        if not response.ok:
            response.raise_for_status()
        # content length is a string, need to convert
        try:
            # long int type is big enough for double the current size of the zip
            length = long(response.headers['content-length'])
        except NameError:
            # in py3, no long(), instead int will suffice
            length = int(response.headers['content-length'])
        return {
            # should prob not call int here, can this remain a string until writing to db?
            'content-length': length,
            'last-modified': response.headers['last-modified'],
            'etag': response.headers['etag'],
            'server': response.headers['server'],
        }

    def get_or_create_version(self, expected_size, release_datetime):
        """
        Get or create a RawDataVersion.

        Create a new one only if:
        * expected_size is different from the latest version; or
        * release_datetime is five minutes later than latest_version's.

        Return a tuple of (object, created), where created is a boolean
        specifying whether an object was created.
        """
        obj = None
        try:
            latest = RawDataVersion.objects.latest('release_datetime')
        except RawDataVersion.DoesNotExist:
            obj = RawDataVersion.objects.create(
                release_datetime=release_datetime,
                expected_size=expected_size
            )
            created = True
        else:
            diff = release_datetime - latest.release_datetime
            if latest.expected_size == expected_size and diff.total_seconds() < 300:
                obj = latest
                created = False
            else:
                obj = RawDataVersion.objects.create(
                    release_datetime=release_datetime,
                    expected_size=expected_size,
                )
                created = True
        return obj, created

    #
    # Logging methods
    #

    def header(self, string):
        """
        Writes out a string to stdout formatted to look like a header.
        """
        logger.debug(string)
        if not self.no_color:
            string = colorize(string, fg="cyan", opts=("bold",))
        self.stdout.write(string)

    def log(self, string):
        """
        Writes out a string to stdout formatted to look like a standard line.
        """
        logger.debug(string)
        if not self.no_color:
            string = colorize("%s" % string, fg="white")
        self.stdout.write(string)

    def success(self, string):
        """
        Writes out a string to stdout formatted green to communicate success.
        """
        logger.debug(string)
        if not self.no_color:
            string = colorize(string, fg="green")
        self.stdout.write(string)

    def failure(self, string):
        """
        Writes out a string to stdout formatted red to communicate failure.
        """
        logger.debug(string)
        if not self.no_color:
            string = colorize(string, fg="red")
        self.stdout.write(string)

    def duration(self):
        """
        Calculates how long the command has been running and writes it to stdout.
        """
        duration = datetime.now() - self.start_datetime
        self.stdout.write('Duration: {}'.format(str(duration)))
        logger.debug('Duration: {}'.format(str(duration)))

    def confirm_proceed(self, prompt):
        """
        Prompts the user for yes/no confirmation to proceed.
        """
        # Ensure stdout can handle Unicode data: http://bit.ly/1C3l4eV
        locale_encoding = locale.getpreferredencoding()
        old_stdout = sys.stdout
        sys.stdout = codecs.getwriter(locale_encoding)(sys.stdout)

        # Send the confirmation prompt out to the user
        user_input = input(prompt)

        confirm = None

        while confirm is None:
            if user_input.lower() in ['y', 'yes']:
                confirm = True
            elif user_input.lower() in ['n', 'no']:
                confirm = False
            else:
                user_input = input("Invalid input. Please type 'yes', 'no', 'y' or 'n':\n")

        # Set things back to the way they were before continuing.
        sys.stdout = old_stdout

        # Pass back what the user typed
        return confirm

    def __str__(self):
        return sub(r'(.+\.)*', '', self.__class__.__module__)
<|endoftext|>


<commit_before>#!/usr/bin/env python3
import os
import sys
import random
import time
from random import seed, randint
import argparse
import platform
from datetime import datetime
import imp
import numpy as np
# from run_parameter import *
parser = argparse.ArgumentParser(
    description="This is a python3 script to\
    do see the difference variable make \
    run simulation")

parser.add_argument("template", help="the name of template file")
args = parser.parse_args()
protein_name = args.template.strip('/')
# protein_name = args.template.split('_', 1)[-1].strip('/')

folder_list = open('folder_list', 'w')
distance_list = np.arange(45, 250, 10)
temp_list = np.arange(300, 400, 25)
folder_name = ""
for temp in temp_list:
    pre_folder_name = "T_"+str(temp)
    for distance in distance_list:
        folder_name = pre_folder_name + "_D_"+str(distance)
        folder_list.write(folder_name+"\n")
        os.system("mkdir -p " + folder_name)
        os.system("cp -r "+protein_name+" "+folder_name+"/")
        os.chdir(folder_name)
        os.chdir(protein_name)
        os.system(
            "sed -i.bak 's/TEMPERATURE/'" +
            str(temp) +
            "'/g' "+protein_name+".in")
        os.system(
            "sed -i.bak 's/DISTANCE/'" +
            str(distance) +
            "'/g' colvars.x")
        # os.system(
        #     "sed -i.bak 's/MGamma/'" +
        #     str(MGamma) +
        #     "'/g' fix_backbone_coeff.data")
        os.chdir("..")
        os.system("run.py " + protein_name + "/ -o -n 2 -s 4")
        os.chdir("..")
# n = 5
# membrane_k = [1, 2, 3]
# rg_cylindrical_spring_constants = [1, 0.1]
# for i in range(n):
#     for MemK in membrane_k:
#         add_force_strengths = range(-3-MemK, -1-MemK)
#         pre_pre_folder_name = "MemK"+str(MemK)
#         for ForceStrength in add_force_strengths:
#             pre_folder_name = pre_pre_folder_name+"ForceStrength"+str(ForceStrength)
#             for SpringConstant in rg_cylindrical_spring_constants:
#                 # simulation set up
#                 folder_name = pre_folder_name+"SpringConstant"+str(SpringConstant)+"_"+str(i)+"/"
#                 os.system("mkdir -p "+folder_name)
#                 folder_list.write(folder_name+"\n")
#                 os.system("cp -r "+args.template+"* "+folder_name)
#                 os.chdir(folder_name)
#                 os.system(  # replace SIMULATION_STEPS with specific steps
#                     "sed -i.bak 's/WARM_UP_STEPS/'" +
#                     str(warm_up_steps) +
#                     "'/g' "+protein_name+".in")
#                 os.system(  # replace RANDOM with a radnom number
#                         "sed -i.bak 's/RANDOM/'" +
#                         str(randint(1, 10**6)) +
#                         "'/g' "+protein_name+".in")
#                 os.system(  # replace SIMULATION_STEPS with specific steps
#                         "sed -i.bak 's/SIMULATION_STEPS/'" +
#                         str(simulation_steps) +
#                         "'/g' "+protein_name+".in")
#                 os.system(  # replace SpringConstant with specific steps
#                         "sed -i.bak 's/SpringConstant/'" +
#                         str(SpringConstant) +
#                         "'/g' "+protein_name+".in")
#                 os.system(  # replace ForceStrength with specific steps
#                         "sed -i.bak 's/ForceStrength/'" +
#                         str(ForceStrength) +
#                         "'/g' "+protein_name+".in")
#                 os.system(  # replace ForceStrength with specific steps
#                         "sed -i.bak 's/MemK/'" +
#                         str(MemK) +
#                         "'/g' fix_backbone_coeff.data")
#             # if(platform.system() == 'Darwin'):
#             #     os.system("/Users/weilu/Documents/lammps-9Oct12_modified/src/lmp_serial \
#             #     < "+protein_name+".in")
#                 if(platform.system() == 'Darwin'):
#                     os.system("/Users/weilu/Documents/lammps-9Oct12_modified/src/lmp_serial \
#                     < "+protein_name+".in")
#                 elif(platform.system() == 'Linux'):
#                     os.system("cp ~/opt/run.slurm .")
#                     os.system(  # replace PROTEIN with pdb name
#                             "sed -i.bak 's/PROTEIN/'" +
#                             protein_name +
#                             "'/g' run.slurm")
#                     os.system("sbatch run.slurm")
#                 else:
#                     print("system unkown")
#                 os.chdir("..")

folder_list.close()
# print("hello world")
<commit_msg>free energy for 2xov<commit_after>#!/usr/bin/env python3
import os
import sys
import random
import time
from random import seed, randint
import argparse
import platform
from datetime import datetime
import imp
import numpy as np
# from run_parameter import *
parser = argparse.ArgumentParser(
    description="This is a python3 script to\
    do see the difference variable make \
    run simulation")

parser.add_argument("template", help="the name of template file")
args = parser.parse_args()
protein_name = args.template.strip('/')
# protein_name = args.template.split('_', 1)[-1].strip('/')

folder_list = open('folder_list', 'w')
distance_list = np.arange(20, 350, 10)
temp_list = np.arange(300, 400, 50)
folder_name = ""
for temp in temp_list:
    pre_folder_name = "T_"+str(temp)
    for distance in distance_list:
        folder_name = pre_folder_name + "_D_"+str(distance)
        folder_list.write(folder_name+"\n")
        os.system("mkdir -p " + folder_name)
        os.system("cp -r "+protein_name+" "+folder_name+"/")
        os.chdir(folder_name)
        os.chdir(protein_name)
        os.system(
            "sed -i.bak 's/TEMPERATURE/'" +
            str(temp) +
            "'/g' "+protein_name+".in")
        os.system(
            "sed -i.bak 's/DISTANCE/'" +
            str(distance) +
            "'/g' colvars.x")
        # os.system(
        #     "sed -i.bak 's/MGamma/'" +
        #     str(MGamma) +
        #     "'/g' fix_backbone_coeff.data")
        os.chdir("..")
        os.system("run.py " + protein_name + "/ -o -n 2 -s 4")
        os.chdir("..")
# n = 5
# membrane_k = [1, 2, 3]
# rg_cylindrical_spring_constants = [1, 0.1]
# for i in range(n):
#     for MemK in membrane_k:
#         add_force_strengths = range(-3-MemK, -1-MemK)
#         pre_pre_folder_name = "MemK"+str(MemK)
#         for ForceStrength in add_force_strengths:
#             pre_folder_name = pre_pre_folder_name+"ForceStrength"+str(ForceStrength)
#             for SpringConstant in rg_cylindrical_spring_constants:
#                 # simulation set up
#                 folder_name = pre_folder_name+"SpringConstant"+str(SpringConstant)+"_"+str(i)+"/"
#                 os.system("mkdir -p "+folder_name)
#                 folder_list.write(folder_name+"\n")
#                 os.system("cp -r "+args.template+"* "+folder_name)
#                 os.chdir(folder_name)
#                 os.system(  # replace SIMULATION_STEPS with specific steps
#                     "sed -i.bak 's/WARM_UP_STEPS/'" +
#                     str(warm_up_steps) +
#                     "'/g' "+protein_name+".in")
#                 os.system(  # replace RANDOM with a radnom number
#                         "sed -i.bak 's/RANDOM/'" +
#                         str(randint(1, 10**6)) +
#                         "'/g' "+protein_name+".in")
#                 os.system(  # replace SIMULATION_STEPS with specific steps
#                         "sed -i.bak 's/SIMULATION_STEPS/'" +
#                         str(simulation_steps) +
#                         "'/g' "+protein_name+".in")
#                 os.system(  # replace SpringConstant with specific steps
#                         "sed -i.bak 's/SpringConstant/'" +
#                         str(SpringConstant) +
#                         "'/g' "+protein_name+".in")
#                 os.system(  # replace ForceStrength with specific steps
#                         "sed -i.bak 's/ForceStrength/'" +
#                         str(ForceStrength) +
#                         "'/g' "+protein_name+".in")
#                 os.system(  # replace ForceStrength with specific steps
#                         "sed -i.bak 's/MemK/'" +
#                         str(MemK) +
#                         "'/g' fix_backbone_coeff.data")
#             # if(platform.system() == 'Darwin'):
#             #     os.system("/Users/weilu/Documents/lammps-9Oct12_modified/src/lmp_serial \
#             #     < "+protein_name+".in")
#                 if(platform.system() == 'Darwin'):
#                     os.system("/Users/weilu/Documents/lammps-9Oct12_modified/src/lmp_serial \
#                     < "+protein_name+".in")
#                 elif(platform.system() == 'Linux'):
#                     os.system("cp ~/opt/run.slurm .")
#                     os.system(  # replace PROTEIN with pdb name
#                             "sed -i.bak 's/PROTEIN/'" +
#                             protein_name +
#                             "'/g' run.slurm")
#                     os.system("sbatch run.slurm")
#                 else:
#                     print("system unkown")
#                 os.chdir("..")

folder_list.close()
# print("hello world")
<|endoftext|>


<commit_before>from derpbot import plugin

class AdminPlugin(plugin.Plugin):
    def __init__(self, bot, config):
        super(AdminPlugin, self).__init__(bot, config)

    def enable(self):
        pass

    def disable(self):
        pass

    @plugin.command("reload",
                    name="reload",
                    usage="reload",
                    desc="Reloads all plugins.",
                    require_admin=True)
    def reload(self, channel, nick, match, message, args):
        if not self.bot.is_admin(nick):
            channel.sendto(nick, "You don't have permission to use this command!")
        channel.sendto(nick, "Reloading.")
        self.bot.reload()

    @plugin.command("plugins",
                    name="plugins",
                    usage="plugins",
                    require_admin=True)
    def plugins(self, channel, nick, match, message, args):
        plugin_mgr = self.bot.plugins

        enabled = plugin_mgr._plugin_instances.keys()
        channel.sendpriv(nick, "Enabled plugins: " + ", ".join(enabled))

        not_enabled = []
        for name in plugin_mgr._plugins_avail.values():
            if name not in enabled:
                not_enabled.append(name)
        channel.sendpriv(nick, "Not enabled plugins: " + ", ".join(not_enabled))

    @plugin.command("say (.*)",
                    name="say",
                    usage="say <text>",
                    require_admin=True)
    def say(self, channel, nick, match, message, args):
        for c in self.bot.plugins.channels:
            c.send(match.group(1))
<commit_msg>Added functionality to load/unload single plugins via commands.<commit_after>from derpbot import plugin

class AdminPlugin(plugin.Plugin):
    def __init__(self, bot, config):
        super(AdminPlugin, self).__init__(bot, config)

    def enable(self):
        pass

    def disable(self):
        pass

    @plugin.command("reload",
                    name="reload",
                    usage="reload",
                    desc="Reloads all plugins.",
                    require_admin=True)
    def reload(self, channel, nick, match, message, args):
        if not self.bot.is_admin(nick):
            channel.sendto(nick, "You don't have permission to use this command!")
        channel.sendto(nick, "Reloading.")
        self.bot.reload()

    @plugin.command("plugins",
                    name="plugins",
                    usage="plugins",
                    require_admin=True)
    def plugins(self, channel, nick, match, message, args):
        plugin_mgr = self.bot.plugins

        enabled = plugin_mgr._plugin_instances.keys()
        channel.sendto(nick, "Enabled plugins: " + ", ".join(enabled))

        not_enabled = []
        for name in plugin_mgr._plugins_avail.values():
            if name not in enabled:
                not_enabled.append(name)
        channel.sendto(nick, "Not enabled plugins: " + ", ".join(not_enabled))

    @plugin.command("load (.*)",
                    require_admin=True)
    def load(self, channel, nick, match, message, args):
        plugin_mgr = self.bot.plugins
        plugins = plugin_mgr._plugins_avail.values()
        enabled = plugin_mgr._plugin_instances.keys()

        plugin = match.group(1)
        if plugin not in plugins:
            channel.sendto(nick, "Unknown plugin %s!" % plugin)
        elif plugin in enabled:
            channel.sendto(nick, "Plugin %s is already loaded!" % plugin)
        else:
            ok = plugin_mgr.load_plugin(plugin)
            if ok:
                channel.sendto(nick, "Successfully loaded plugin %s!" % plugin)
            else:
                channel.sendto(nick, "An error happened while loading plugin!")

    @plugin.command("unload (.*)",
                     require_admin=True)
    def unload(self, channel, nick, match, message, args):
        plugin_mgr = self.bot.plugins
        plugins = plugin_mgr._plugins_avail.values()
        enabled = plugin_mgr._plugin_instances.keys()

        plugin = match.group(1)
        if plugin not in plugins:
            channel.sendto(nick, "Unknown plugin %s!" % plugin)
        elif plugin not in enabled:
            channel.sendto(nick, "Plugin %s is not loaded!" % plugin)
        else:
            ok = plugin_mgr.unload_plugin(plugin)
            if ok:
                channel.sendto(nick, "Successfully unloaded plugin %s!" % plugin)
            else:
                channel.sendto(nick, "An error happened while unloading plugin!")

    @plugin.command("say (.*)",
                    name="say",
                    usage="say <text>",
                    require_admin=True)
    def say(self, channel, nick, match, message, args):
        for c in self.bot.plugins.channels:
            c.send(match.group(1))
<|endoftext|>


<commit_before>"""
Listeners to SQLAlchemy events to keep track of CUD operations.
"""

import logging
import inspect
from collections import deque

from sqlalchemy import event
from sqlalchemy.orm.session import Session as GlobalSession

from dbsync import core
from dbsync.models import Operation, ContentType

#: Operations to be flushed to the database after a commit.
_operations_queue = deque()

def flush_operations(_):
    "Flush operations after a commit has been issued."
    if not _operations_queue or not core.listening: return
    session = core.Session()
    while _operations_queue:
        op = _operations_queue.popleft()
        session.add(op)
        session.flush()
    session.commit()
    session.close()

def empty_queue(*_):
    "Empty the operations queue."
    if not core.listening: return
    while _operations_queue:
        _operations_queue.pop()

#: table name => content_type id
cts_cache = {}

def get_ct(tname, session):
    in_cache = cts_cache.get(tname, None)
    if in_cache is None:
        ct = session.query(ContentType).\
            filter(ContentType.table_name == tname).first()
        if ct is not None:
            cts_cache[tname] = ct.content_type_id
            return ct.content_type_id
    return in_cache

def make_listener(command):
    "Builds a listener for the given command (i, u, d)."
    def listener(mapper, connection, target):
        if not core.listening: return
        if command == 'u' and not core.SessionClass.object_session(target).\
                is_modified(target, include_collections=False):
            return
        session = core.Session()
        tname = mapper.mapped_table.name
        ct = get_ct(tname, session)
        if ct is None:
            logging.error("you must register a content type for {0} "\
                              "to keep track of operations".format(tname))
            return
        pk = getattr(target, mapper.primary_key[0].name)
        op = Operation(
            row_id=pk,
            version_id=None, # operation not yet versioned
            content_type_id=ct,
            command=command)
        _operations_queue.append(op)
        session.close()
    return listener

def _start_tracking(model, directions):
    if 'pull' in directions:
        core.pulled_models.add(model)
    if 'push' in directions:
        core.pushed_models.add(model)
    if model.__name__ in core.synched_models:
        return model
    core.synched_models[model.__name__] = model
    event.listen(model, 'after_insert', make_listener('i'))
    event.listen(model, 'after_update', make_listener('u'))
    event.listen(model, 'after_delete', make_listener('d'))
    return model

def track(*directions):
    """
    Adds an ORM class to the list of synchronized classes.

    It can be used as a class decorator. This will also install
    listeners to keep track of CUD operations for the given model.

    *directions* are optional arguments of values in ('push', 'pull')
    that can restrict the way dbsync handles the class during those
    procedures. If not given, both values are assumed. If only one of
    them is given, the other procedure will ignore the tracked class.
    """
    valid = ('push', 'pull')
    if not directions:
        return lambda model: _start_tracking(model, valid)
    if len(directions) == 1 and inspect.isclass(directions[0]):
        return _start_tracking(directions[0], valid)
    assert all(d in valid for d in directions), \
        "track only accepts the arguments: {0}".format(', '.join(valid))
    return lambda model: _start_tracking(model, directions)

event.listen(GlobalSession, 'after_commit', flush_operations)
event.listen(GlobalSession, 'after_soft_rollback', empty_queue)
<commit_msg>Don't track operations in the node for pull-only models.<commit_after>"""
Listeners to SQLAlchemy events to keep track of CUD operations.
"""

import logging
import inspect
from collections import deque

from sqlalchemy import event
from sqlalchemy.orm.session import Session as GlobalSession

from dbsync import core
from dbsync.models import Operation, ContentType

#: Operations to be flushed to the database after a commit.
_operations_queue = deque()

def flush_operations(_):
    "Flush operations after a commit has been issued."
    if not _operations_queue or not core.listening: return
    session = core.Session()
    while _operations_queue:
        op = _operations_queue.popleft()
        session.add(op)
        session.flush()
    session.commit()
    session.close()

def empty_queue(*_):
    "Empty the operations queue."
    if not core.listening: return
    while _operations_queue:
        _operations_queue.pop()

#: table name => content_type id
cts_cache = {}

def get_ct(tname, session):
    in_cache = cts_cache.get(tname, None)
    if in_cache is None:
        ct = session.query(ContentType).\
            filter(ContentType.table_name == tname).first()
        if ct is not None:
            cts_cache[tname] = ct.content_type_id
            return ct.content_type_id
    return in_cache

def make_listener(command):
    "Builds a listener for the given command (i, u, d)."
    def listener(mapper, connection, target):
        if not core.listening: return
        if command == 'u' and not core.SessionClass.object_session(target).\
                is_modified(target, include_collections=False):
            return
        session = core.Session()
        tname = mapper.mapped_table.name
        ct = get_ct(tname, session)
        if ct is None:
            logging.error("you must register a content type for {0} "\
                              "to keep track of operations".format(tname))
            return
        pk = getattr(target, mapper.primary_key[0].name)
        op = Operation(
            row_id=pk,
            version_id=None, # operation not yet versioned
            content_type_id=ct,
            command=command)
        _operations_queue.append(op)
        session.close()
    return listener

def _start_tracking(model, directions):
    if 'pull' in directions:
        core.pulled_models.add(model)
    if 'push' in directions:
        core.pushed_models.add(model)
    if model.__name__ in core.synched_models:
        return model
    core.synched_models[model.__name__] = model
    if 'push' not in directions:
        return model # don't track operations for pull-only models
    event.listen(model, 'after_insert', make_listener('i'))
    event.listen(model, 'after_update', make_listener('u'))
    event.listen(model, 'after_delete', make_listener('d'))
    return model

def track(*directions):
    """
    Adds an ORM class to the list of synchronized classes.

    It can be used as a class decorator. This will also install
    listeners to keep track of CUD operations for the given model.

    *directions* are optional arguments of values in ('push', 'pull')
    that can restrict the way dbsync handles the class during those
    procedures. If not given, both values are assumed. If only one of
    them is given, the other procedure will ignore the tracked class.
    """
    valid = ('push', 'pull')
    if not directions:
        return lambda model: _start_tracking(model, valid)
    if len(directions) == 1 and inspect.isclass(directions[0]):
        return _start_tracking(directions[0], valid)
    assert all(d in valid for d in directions), \
        "track only accepts the arguments: {0}".format(', '.join(valid))
    return lambda model: _start_tracking(model, directions)

event.listen(GlobalSession, 'after_commit', flush_operations)
event.listen(GlobalSession, 'after_soft_rollback', empty_queue)
<|endoftext|>


<commit_before>'''
Generating node upgrade script
'''

import subprocess
import uuid

import gen.build_deploy.util as util
import gen.calc
import gen.template
from dcos_installer.constants import SERVE_DIR
from pkgpanda.util import write_string

node_upgrade_template = """
#!/bin/bash
#
# BASH script to upgrade DC/OS on a node
#
# Metadata:
#   dcos image commit: {{ dcos_image_commit }}
#   generation date: {{ generation_date }}
# Copyright 2017 Mesosphere, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit -o nounset -o pipefail

source /opt/mesosphere/environment.export

# Check if this is a terminal, and if colors are supported, set some basic
# colors for outputs
if [ -t 1 ]; then
    colors_supported=$(tput colors)
    if [[ $colors_supported -ge 8 ]]; then
    RED='\e[1;31m'
    BOLD='\e[1m'
    NORMAL='\e[0m'
    fi
fi

if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

# check for version of dc/os upgrading from
found_version=`grep "version" /opt/mesosphere/etc/dcos-version.json | cut -d '"' -f 4`
if [ "$found_version" != "{{ installed_cluster_version }}" ]; then
    echo "ERROR: Expecting to upgrade DC/OS from {{ installed_cluster_version }} to {{ installer_version }}." \
         "Version found on node: $found_version"
    exit 1
fi

# Determine this node's role.
ROLE_DIR=/etc/mesosphere/roles
if [ -f $ROLE_DIR/master ]; then
    role="master"
    role_name="master"
elif [ -f $ROLE_DIR/slave ]; then
    role="slave"
    role_name="agent"
elif [ -f $ROLE_DIR/slave_public ]; then
    role="slave_public"
    role_name="public agent"
else
    echo "ERROR: Can't determine this node's role." \
         "One of master, slave, or slave_public must be present under $ROLE_DIR."
    exit 1
fi

echo "Upgrading DC/OS $role_name {{ installed_cluster_version }} -> {{ installer_version }}"
pkgpanda fetch --repository-url={{ bootstrap_url }} {{ cluster_packages }}
pkgpanda activate --no-block {{ cluster_packages }}

# If this is a master node, migrate Exhibitor data to the correct directory.
if [ "$role" == "master" ]; then
    # run exhibitor migration script here
    until dcos-shell dcos-exhibitor-migrate-perform
    do
        status=$?
        case $status in
            1) echo "Waiting for Exhibitor..."
               sleep 10
               ;;
            2) echo "Could not read from Exhibitor."
               echo "Please contact support."
               exit
               ;;
            4) echo "Rolling update in progress."
               exit
               ;;
            8) echo "At least one Exhibitor config value does not have the expected pre-migration value." \
                    "Unable to automatically migrate Exhibitor."
               echo "Please contact support."
               exit
               ;;
            16) echo "Exhibitor update failed with a non-OK response."
                echo "Please contact support."
                exit
                ;;
        esac
    done
fi

"""

def generate_node_upgrade_script(gen_out, installed_cluster_version, serve_dir=SERVE_DIR):

    # installed_cluster_version: Current installed version on the cluster
    # installer_version: Version we are upgrading to

    bootstrap_url = gen_out.arguments['bootstrap_url']

    installer_version = gen.calc.entry['must']['dcos_version']

    package_list = ' '.join(package['id'] for package in gen_out.cluster_packages.values())

    bash_script = gen.template.parse_str(node_upgrade_template).render({
        'dcos_image_commit': util.dcos_image_commit,
        'generation_date': util.template_generation_date,
        'bootstrap_url': bootstrap_url,
        'cluster_packages': package_list,
        'installed_cluster_version': installed_cluster_version,
        'installer_version': installer_version})

    upgrade_script_path = '/upgrade/' + uuid.uuid4().hex

    subprocess.check_call(['mkdir', '-p', serve_dir + upgrade_script_path])

    write_string(serve_dir + upgrade_script_path + '/dcos_node_upgrade.sh', bash_script)

    print("Node upgrade script URL: " + bootstrap_url + upgrade_script_path + '/dcos_node_upgrade.sh')

    return 0
<commit_msg>Exit with error when Exhibitor migration fails<commit_after>'''
Generating node upgrade script
'''

import subprocess
import uuid

import gen.build_deploy.util as util
import gen.calc
import gen.template
from dcos_installer.constants import SERVE_DIR
from pkgpanda.util import write_string

node_upgrade_template = """
#!/bin/bash
#
# BASH script to upgrade DC/OS on a node
#
# Metadata:
#   dcos image commit: {{ dcos_image_commit }}
#   generation date: {{ generation_date }}
# Copyright 2017 Mesosphere, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit -o nounset -o pipefail

source /opt/mesosphere/environment.export

# Check if this is a terminal, and if colors are supported, set some basic
# colors for outputs
if [ -t 1 ]; then
    colors_supported=$(tput colors)
    if [[ $colors_supported -ge 8 ]]; then
    RED='\e[1;31m'
    BOLD='\e[1m'
    NORMAL='\e[0m'
    fi
fi

if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

# check for version of dc/os upgrading from
found_version=`grep "version" /opt/mesosphere/etc/dcos-version.json | cut -d '"' -f 4`
if [ "$found_version" != "{{ installed_cluster_version }}" ]; then
    echo "ERROR: Expecting to upgrade DC/OS from {{ installed_cluster_version }} to {{ installer_version }}." \
         "Version found on node: $found_version"
    exit 1
fi

# Determine this node's role.
ROLE_DIR=/etc/mesosphere/roles
if [ -f $ROLE_DIR/master ]; then
    role="master"
    role_name="master"
elif [ -f $ROLE_DIR/slave ]; then
    role="slave"
    role_name="agent"
elif [ -f $ROLE_DIR/slave_public ]; then
    role="slave_public"
    role_name="public agent"
else
    echo "ERROR: Can't determine this node's role." \
         "One of master, slave, or slave_public must be present under $ROLE_DIR."
    exit 1
fi

echo "Upgrading DC/OS $role_name {{ installed_cluster_version }} -> {{ installer_version }}"
pkgpanda fetch --repository-url={{ bootstrap_url }} {{ cluster_packages }}
pkgpanda activate --no-block {{ cluster_packages }}

# If this is a master node, migrate Exhibitor data to the correct directory.
if [ "$role" == "master" ]; then
    # run exhibitor migration script here
    until dcos-shell dcos-exhibitor-migrate-perform
    do
        status=$?
        case $status in
            1) echo "Waiting for Exhibitor..."
               sleep 10
               ;;
            2) echo "ERROR: Could not read from Exhibitor."
               echo "Please contact support."
               exit 1
               ;;
            4) echo "Rolling update in progress."
               exit
               ;;
            8) echo "ERROR: At least one Exhibitor config value does not have the expected pre-migration value." \
                    "Unable to automatically migrate Exhibitor."
               echo "Please contact support."
               exit 1
               ;;
            16) echo "ERROR: Exhibitor update failed with a non-OK response."
                echo "Please contact support."
                exit 1
                ;;
        esac
    done
fi

"""

def generate_node_upgrade_script(gen_out, installed_cluster_version, serve_dir=SERVE_DIR):

    # installed_cluster_version: Current installed version on the cluster
    # installer_version: Version we are upgrading to

    bootstrap_url = gen_out.arguments['bootstrap_url']

    installer_version = gen.calc.entry['must']['dcos_version']

    package_list = ' '.join(package['id'] for package in gen_out.cluster_packages.values())

    bash_script = gen.template.parse_str(node_upgrade_template).render({
        'dcos_image_commit': util.dcos_image_commit,
        'generation_date': util.template_generation_date,
        'bootstrap_url': bootstrap_url,
        'cluster_packages': package_list,
        'installed_cluster_version': installed_cluster_version,
        'installer_version': installer_version})

    upgrade_script_path = '/upgrade/' + uuid.uuid4().hex

    subprocess.check_call(['mkdir', '-p', serve_dir + upgrade_script_path])

    write_string(serve_dir + upgrade_script_path + '/dcos_node_upgrade.sh', bash_script)

    print("Node upgrade script URL: " + bootstrap_url + upgrade_script_path + '/dcos_node_upgrade.sh')

    return 0
<|endoftext|>


<commit_before>from django.contrib.auth.decorators import login_required
from django.contrib.messages.views import SuccessMessageMixin
from django.shortcuts import render
from django.utils.datetime_safe import datetime
from django.utils.decorators import method_decorator
from django.views.generic import ListView
from django.http import HttpResponseRedirect, JsonResponse
from buzzit_messaging.views import __send_system__message__
from buzzit_models.models import *
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse, reverse_lazy
import django.contrib.messages as messages
from django.views.generic import ListView
from buzzit_models.models import *
from django.contrib.auth.decorators import login_required
import json
from django.core.mail import send_mail

@login_required()
def report_user(request,user_id):
    """
    current user report other user, and gives the reason which should not be empty
    :param request:
    :param user_id:
    :return:
    """

    try:
        reported_user = User.objects.get(pk=user_id)
    except ObjectDoesNotExist:
        messages.error(request,"Der Benutzer existiert nicht.")
        return HttpResponseRedirect(reverse_lazy("home"))
    if request.method == "POST":
        report_message = UserReport()
        report_text = request.POST.get["text",False]

        report_message.creator=request.user
        report_message.created=datetime.now()
        report_message.text=report_text
        report_message.reported_user=reported_user

        if len(report_message.text) < 1:
                messages.error(request,"Text zum Benutzermelden ist zu geben")
                return HttpResponseRedirect(reverse_lazy("home"))
        report_message.save()
        messages.info("Sie haben den <User:%s> Benutzer gemeldet" %reported_user)

    #bTODO send messages to admin user, not to SYSTEM user
    #system_user = User.objects.get(username="SYSTEM")
    #__send_system__message__(system_user.pk, "<Report:%s> Neue Meldung " % report_message)

    #return HttpResponseRedirect(reverse_lazy('home'))

class UserReportDetailsView(SuccessMessageMixin,ListView):
    """
    display the report text and reported user
    """
    model = UserReport
    template_name = "logged_in/user_report_deatails"

    def get_queryset(self):
        try:
            reported_user = self.kwargs.get["user_id"]
        except ObjectDoesNotExist:
            messages.error(self.request,"Benutzer existiert nicht")
            return HttpResponseRedirect(reverse_lazy("admin_frontpage"))
        return UserReport.objects.filter(reported_user=reported_user).order_by("created").all()

    def get_context_data(self, **kwargs):

        try:
            reported_user=self.kwargs.get["user_id"]
        except ObjectDoesNotExist:
            messages.error(self.request,"Benutzer existiert nicht")
            return HttpResponseRedirect(reverse_lazy("admin_frontpage"))

        reported_user_profile = reported_user.profile
        report = UserReport.objects.get(reported_user=reported_user)
        context=super(UserReportDetailsView, self).get_context_data(**kwargs)
        context["reported_user_profile"]= reported_user_profile
        context["report_text"]=report.text

        return context

    def dispatch(self, request, *args, **kwargs):
        return super(UserReportDetailsView, self).dispatch(request,*args,**kwargs)

class AdminFrontpageView():
    pass

def adminFrontPage(request):
    """
    show all user and post reports at the page
    :param request:
    :return:
    """
    all_user_reports =[]
    all_post_reports=[]

    all_user_reports = UserReport.objects.all()
    all_post_reports = CircleMessageReport.objects.all()

    return render(request,"logged_in/admin_dashboard.html",{"user_reports":all_user_reports,"post_reports":all_post_reports})

class MessageReportDetailsView(ListView):
    pass

class AdminOverviewView(ListView):
    pass

@login_required
def delete_reported_post(request, message_id):
    """
    delete reported message from admin, check if the message also has answers,
    reported message with all answers would be delete, else delete only message
    TODO was ist, wenn eine Nachricht rebuzzed wurde
    :param request:
    :param message_id:
    :return:
    """
    try:
        post_to_del=Circle_message.objects.get(pk=message_id)
    except ObjectDoesNotExist:
        messages.error(request,"Die Nachrichte existiert nicht")
        return HttpResponseRedirect(reverse_lazy("admin_frontpage"))
    #if the reported post has anwsers, delete all

    answers=Circle_message.objects.filter(answer_to=post_to_del)
    answers.delete()
    post_to_del.delete()
    messages.success(request,"Die Nachrichte wurde erfolgreich geloescht")
    return HttpResponseRedirect(reverse_lazy("admin_frontpage"))

@login_required
def promote_user_to_admin(request, user_id):
    """
    check if user exists, then check if user is active
    :param request:
    :param user_id:
    :return:
    """
    try:
        admin_user=User.objects.get(pk=user_id)
    except ObjectDoesNotExist:
        messages.error(request,"Der Benuzer existiert nicht")
        return HttpResponseRedirect(reverse_lazy("admin_frontpage"))

    if not(admin_user.is_active):
        messages.info(request,"Der Benutzer ist deaktiviert")
        return HttpResponseRedirect(reverse_lazy("admin_frontpage"))

    admin_user.is_staff=True
    messages.info(request,"Der Benutzer ist als AdminUser hinzugefuegt")
    return HttpResponseRedirect(reverse_lazy("admin_frontpage"))

@login_required
def demote_admin_to_user(request, user_id):
    pass

@login_required
def report_message(request, message_id):
    """
    user report message to admin
    :param request:
    :param message_id:
    :return:
    """
    try:
        message=Circle_message.objects.get(pk=message_id)
    except ObjectDoesNotExist:
        messages.error(request,"Die Nachrichte existiert nicht")
        return HttpResponseRedirect(reverse_lazy("home"))
    text=request.POST.get["text",False]
    if text:
        if len(text) < 1:
            messages.error(request,"Es wurde keinen Text gegeben")
            return HttpResponseRedirect(reverse_lazy("home"))
        report_message=CircleMessageReport()
        report_message.creator=request.user
        report_message.created=datetime.now()
        report_message.reported_message=message
        report_message.text=text
        report_message.save()

    messages.info(request,"Die Nachrichte <Circle_message:%s> wurde gemeldet" %message)
    return HttpResponseRedirect(reverse_lazy("home"))

@login_required
def ban_user(request, user_id):
    pass
<commit_msg>add demote user to admin<commit_after>from django.contrib.auth.decorators import login_required
from django.contrib.messages.views import SuccessMessageMixin
from django.shortcuts import render
from django.utils.datetime_safe import datetime
from django.utils.decorators import method_decorator
from django.views.generic import ListView
from django.http import HttpResponseRedirect, JsonResponse
from buzzit_messaging.views import __send_system__message__
from buzzit_models.models import *
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse, reverse_lazy
import django.contrib.messages as messages
from django.views.generic import ListView
from buzzit_models.models import *
from django.contrib.auth.decorators import login_required
import json
from django.core.mail import send_mail

@login_required()
def report_user(request,user_id):
    """
    current user report other user, and gives the reason which should not be empty
    :param request:
    :param user_id:
    :return:
    """

    try:
        reported_user = User.objects.get(pk=user_id)
    except ObjectDoesNotExist:
        messages.error(request,"Der Benutzer existiert nicht.")
        return HttpResponseRedirect(reverse_lazy("home"))
    if request.method == "POST":
        report_message = UserReport()
        report_text = request.POST.get["text",False]

        report_message.creator=request.user
        report_message.created=datetime.now()
        report_message.text=report_text
        report_message.reported_user=reported_user

        if len(report_message.text) < 1:
                messages.error(request,"Text zum Benutzermelden ist zu geben")
                return HttpResponseRedirect(reverse_lazy("home"))
        report_message.save()
        messages.info("Sie haben den <User:%s> Benutzer gemeldet" %reported_user)

    #bTODO send messages to admin user, not to SYSTEM user
    #system_user = User.objects.get(username="SYSTEM")
    #__send_system__message__(system_user.pk, "<Report:%s> Neue Meldung " % report_message)

    #return HttpResponseRedirect(reverse_lazy('home'))

class UserReportDetailsView(SuccessMessageMixin,ListView):
    """
    display the report text and reported user
    """
    model = UserReport
    template_name = "logged_in/user_report_deatails"

    def get_queryset(self):
        try:
            reported_user = self.kwargs.get["user_id"]
        except ObjectDoesNotExist:
            messages.error(self.request,"Benutzer existiert nicht")
            return HttpResponseRedirect(reverse_lazy("admin_frontpage"))
        return UserReport.objects.filter(reported_user=reported_user).order_by("created").all()

    def get_context_data(self, **kwargs):

        try:
            reported_user=self.kwargs.get["user_id"]
        except ObjectDoesNotExist:
            messages.error(self.request,"Benutzer existiert nicht")
            return HttpResponseRedirect(reverse_lazy("admin_frontpage"))

        reported_user_profile = reported_user.profile
        report = UserReport.objects.get(reported_user=reported_user)
        context=super(UserReportDetailsView, self).get_context_data(**kwargs)
        context["reported_user_profile"]= reported_user_profile
        context["report_text"]=report.text

        return context

    def dispatch(self, request, *args, **kwargs):
        return super(UserReportDetailsView, self).dispatch(request,*args,**kwargs)

class AdminFrontpageView():
    pass

def adminFrontPage(request):
    """
    show all user and post reports at the page
    :param request:
    :return:
    """
    all_user_reports =[]
    all_post_reports=[]

    all_user_reports = UserReport.objects.all()
    all_post_reports = CircleMessageReport.objects.all()

    return render(request,"logged_in/admin_dashboard.html",{"user_reports":all_user_reports,"post_reports":all_post_reports})

class MessageReportDetailsView(ListView):
    pass

class AdminOverviewView(ListView):
    pass

@login_required
def delete_reported_post(request, message_id):
    """
    delete reported message from admin, check if the message also has answers,
    reported message with all answers would be delete, else delete only message
    TODO was ist, wenn eine Nachricht rebuzzed wurde
    :param request:
    :param message_id:
    :return:
    """
    try:
        post_to_del=Circle_message.objects.get(pk=message_id)
    except ObjectDoesNotExist:
        messages.error(request,"Die Nachrichte existiert nicht")
        return HttpResponseRedirect(reverse_lazy("admin_frontpage"))
    #if the reported post has anwsers, delete all

    answers=Circle_message.objects.filter(answer_to=post_to_del)
    answers.delete()
    post_to_del.delete()
    messages.success(request,"Die Nachrichte wurde erfolgreich geloescht")
    return HttpResponseRedirect(reverse_lazy("admin_frontpage"))

@login_required
def promote_user_to_admin(request, user_id):
    """
    check if user exists, then check if user is active
    :param request:
    :param user_id:
    :return:
    """
    try:
        admin_user=User.objects.get(pk=user_id)
    except ObjectDoesNotExist:
        messages.error(request,"Der Benuzer existiert nicht")
        return HttpResponseRedirect(reverse_lazy("admin_frontpage"))

    if not(admin_user.is_active):
        messages.info(request,"Der Benutzer ist deaktiviert")
        return HttpResponseRedirect(reverse_lazy("admin_frontpage"))

    admin_user.is_staff=True
    messages.info(request,"Der Benutzer ist als AdminUser hinzugefuegt")
    return HttpResponseRedirect(reverse_lazy("admin_frontpage"))

@login_required
def demote_admin_to_user(request, user_id):
    """
    check if user exists, check if user is adminUser
    :param request:
    :param user_id:
    :return:
    """
    try:
        demote_user=User.objects.get(pk=user_id)
    except ObjectDoesNotExist:
        messages.error(request,"Der Benutzer existiert nicht")
        return HttpResponseRedirect(reverse_lazy("admin_frontpage"))

    if not(demote_user.is_staff):
        messages.error(request,"Der Benutzer ist kein Admin ")
        return HttpResponseRedirect(reverse_lazy("admin_frontpage"))

    demote_user.is_staff=False
    messages.info(request,"Die Adminrechte von dem Benutzer wird entziehen")
    return HttpResponseRedirect(reverse_lazy("admin_frontpage"))

@login_required
def report_message(request, message_id):
    """
    user report message to admin
    :param request:
    :param message_id:
    :return:
    """
    try:
        message=Circle_message.objects.get(pk=message_id)
    except ObjectDoesNotExist:
        messages.error(request,"Die Nachrichte existiert nicht")
        return HttpResponseRedirect(reverse_lazy("home"))
    text=request.POST.get["text",False]
    if text:
        if len(text) < 1:
            messages.error(request,"Es wurde keinen Text gegeben")
            return HttpResponseRedirect(reverse_lazy("home"))
        report_message=CircleMessageReport()
        report_message.creator=request.user
        report_message.created=datetime.now()
        report_message.reported_message=message
        report_message.text=text
        report_message.save()

    messages.info(request,"Die Nachrichte <Circle_message:%s> wurde gemeldet" %message)
    return HttpResponseRedirect(reverse_lazy("home"))

@login_required
def ban_user(request, user_id):
    pass
<|endoftext|>


<commit_before># Copyright (c) 2012 Paul Tagliamonte <paultag@debian.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

from debuild.models import DebuildDatabaseObject, db, strtype

class Package(DebuildDatabaseObject):
    _table = "packages"

    def __init__(self, thing):
        if isinstance(thing, strtype):
            self._record = self._fetch_by_id(thing)
            if self._record is None:
                raise Exception
        else:
            self._record = thing

    def reports(self):
        return db.reports.find({"package": self._record['_id']})
<commit_msg>Move to a generator thinger.<commit_after># Copyright (c) 2012 Paul Tagliamonte <paultag@debian.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

from debuild.models import DebuildDatabaseObject, db, strtype
from debuild.models.report import Report

class Package(DebuildDatabaseObject):
    _table = "packages"

    def __init__(self, thing):
        if isinstance(thing, strtype):
            self._record = self._fetch_by_id(thing)
            if self._record is None:
                raise Exception
        else:
            self._record = thing

    def reports(self):
        return (Report(x) for x in
                db.reports.find({"package": self._record['_id']}))
<|endoftext|>


<commit_before>"""
Django settings for defivelo project.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

import os

import dj_database_url
import pytz
from django.contrib import messages

from . import get_env_variable
from .. import get_project_root_path

gettext = lambda s: s

PROJECT_ROOT = get_project_root_path()

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = get_env_variable('SECRET_KEY', '')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = bool(get_env_variable('DEBUG', False))

ALLOWED_HOSTS = tuple(get_env_variable('ALLOWED_HOSTS', '').splitlines())

# Application definition

UPSTREAM_APPS = (
    'bootstrap3',
    'registration_bootstrap3',
    'django_admin_bootstrapped',
    'django.contrib.sites',

    'allauth',
    'allauth.account',
    'allauth.socialaccount',

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'parler',
    'compressor',
    'stronghold',
    'localflavor',
)

# Project apps tested by jenkins (everything in apps/)
APPS_DIR = os.path.join(PROJECT_ROOT, 'apps')
PROJECT_APPS = tuple(['apps.' + aname
                     for aname in os.listdir(APPS_DIR)
                     if os.path.isdir(os.path.join(APPS_DIR, aname))])

INSTALLED_APPS = UPSTREAM_APPS + PROJECT_APPS

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'stronghold.middleware.LoginRequiredMiddleware',
)

ROOT_URLCONF = 'defivelo.urls'

WSGI_APPLICATION = 'defivelo.wsgi.application'

# Bootstrap the admin
DAB_FIELD_RENDERER = 'django_admin_bootstrapped.renderers.BootstrapFieldRenderer'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(PROJECT_ROOT, 'defivelo/templates')
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                # Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
                # list if you haven't customized them:
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.request',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
            ],
            'debug': DEBUG
        },
    },
]

COMPRESS_PRECOMPILERS = (
    ('text/x-scss',
     os.path.join(
         get_env_variable('VIRTUAL_ENV',
                          os.path.join(PROJECT_ROOT, 'venv')
                          ),
         'bin', 'sassc') + ' {infile} {outfile}'),
)

# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
    'default': dj_database_url.parse(get_env_variable('DATABASE_URL'))
}

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
# Internationalization
LANGUAGE_CODE = 'fr'
TIME_ZONE = 'Europe/Zurich'
USE_I18N = True
USE_L10N = True
USE_TZ = True

LANGUAGES = (
    ('fr', gettext('French')),
    ('de', gettext('German')),
)

PARLER_LANGUAGES = {
    None: (
        [{'code': lang[0]} for lang in LANGUAGES]
    ),
    'default': {
    }
}

# This allows you to put project-wide translations in the "locale" directory of
# your project
LOCALE_PATHS = (
    os.path.join(PROJECT_ROOT, 'locale'),
)

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = get_env_variable('STATIC_URL', '/static/')

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# This is usually not used in a dev env, hence the default value
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = get_env_variable('STATIC_ROOT',
                               os.path.join(PROJECT_ROOT, 'static_files'))

STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, "defivelo/static"),
)

STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
    "compressor.finders.CompressorFinder",
)

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = get_env_variable('MEDIA_URL', '/media/')

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = get_env_variable('MEDIA_ROOT', '/tmp/static/media')

# Adapt Stronghold for allauth
STRONGHOLD_PUBLIC_URLS = [r'^/admin/.*$', r'^/accounts/.*$']

AUTHENTICATION_BACKENDS = (
    # Needed to login by username in Django admin, regardless of `allauth`
    'django.contrib.auth.backends.ModelBackend',
    # `allauth` specific authentication methods, such as login by e-mail
    'allauth.account.auth_backends.AuthenticationBackend',
)
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False

def defivelo_user_display(u):
    if u.first_name and u.last_name:
        return u'{first} {last}'.format(first=u.first_name, last=u.last_name)
    else:
        return u.email

ACCOUNT_USER_DISPLAY = defivelo_user_display
ACCOUNT_ADAPTER = 'defivelo.accounts.NoSignupAccountAdapter'

LOGIN_REDIRECT_URL = '/'

SITE_ID = 1
<commit_msg>Allow immediate login after email confirmatioN<commit_after>"""
Django settings for defivelo project.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

import os

import dj_database_url
import pytz
from django.contrib import messages

from . import get_env_variable
from .. import get_project_root_path

gettext = lambda s: s

PROJECT_ROOT = get_project_root_path()

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = get_env_variable('SECRET_KEY', '')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = bool(get_env_variable('DEBUG', False))

ALLOWED_HOSTS = tuple(get_env_variable('ALLOWED_HOSTS', '').splitlines())

# Application definition

UPSTREAM_APPS = (
    'bootstrap3',
    'registration_bootstrap3',
    'django_admin_bootstrapped',
    'django.contrib.sites',

    'allauth',
    'allauth.account',
    'allauth.socialaccount',

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'parler',
    'compressor',
    'stronghold',
    'localflavor',
)

# Project apps tested by jenkins (everything in apps/)
APPS_DIR = os.path.join(PROJECT_ROOT, 'apps')
PROJECT_APPS = tuple(['apps.' + aname
                     for aname in os.listdir(APPS_DIR)
                     if os.path.isdir(os.path.join(APPS_DIR, aname))])

INSTALLED_APPS = UPSTREAM_APPS + PROJECT_APPS

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'stronghold.middleware.LoginRequiredMiddleware',
)

ROOT_URLCONF = 'defivelo.urls'

WSGI_APPLICATION = 'defivelo.wsgi.application'

# Bootstrap the admin
DAB_FIELD_RENDERER = 'django_admin_bootstrapped.renderers.BootstrapFieldRenderer'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(PROJECT_ROOT, 'defivelo/templates')
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                # Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
                # list if you haven't customized them:
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.request',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
            ],
            'debug': DEBUG
        },
    },
]

COMPRESS_PRECOMPILERS = (
    ('text/x-scss',
     os.path.join(
         get_env_variable('VIRTUAL_ENV',
                          os.path.join(PROJECT_ROOT, 'venv')
                          ),
         'bin', 'sassc') + ' {infile} {outfile}'),
)

# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
    'default': dj_database_url.parse(get_env_variable('DATABASE_URL'))
}

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
# Internationalization
LANGUAGE_CODE = 'fr'
TIME_ZONE = 'Europe/Zurich'
USE_I18N = True
USE_L10N = True
USE_TZ = True

LANGUAGES = (
    ('fr', gettext('French')),
    ('de', gettext('German')),
)

PARLER_LANGUAGES = {
    None: (
        [{'code': lang[0]} for lang in LANGUAGES]
    ),
    'default': {
    }
}

# This allows you to put project-wide translations in the "locale" directory of
# your project
LOCALE_PATHS = (
    os.path.join(PROJECT_ROOT, 'locale'),
)

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = get_env_variable('STATIC_URL', '/static/')

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# This is usually not used in a dev env, hence the default value
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = get_env_variable('STATIC_ROOT',
                               os.path.join(PROJECT_ROOT, 'static_files'))

STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, "defivelo/static"),
)

STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
    "compressor.finders.CompressorFinder",
)

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = get_env_variable('MEDIA_URL', '/media/')

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = get_env_variable('MEDIA_ROOT', '/tmp/static/media')

# Adapt Stronghold for allauth
STRONGHOLD_PUBLIC_URLS = [r'^/admin/.*$', r'^/accounts/.*$']

AUTHENTICATION_BACKENDS = (
    # Needed to login by username in Django admin, regardless of `allauth`
    'django.contrib.auth.backends.ModelBackend',
    # `allauth` specific authentication methods, such as login by e-mail
    'allauth.account.auth_backends.AuthenticationBackend',
)
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION = True

def defivelo_user_display(u):
    if u.first_name and u.last_name:
        return u'{first} {last}'.format(first=u.first_name, last=u.last_name)
    else:
        return u.email

ACCOUNT_USER_DISPLAY = defivelo_user_display
ACCOUNT_ADAPTER = 'defivelo.accounts.NoSignupAccountAdapter'

LOGIN_REDIRECT_URL = '/'

SITE_ID = 1
<|endoftext|>