import cv2
from datetime import datetime
from get_db import get_user_profiles
import face_recognition
import datetime as dt
from collections import Counter
import itertools
from Insert_Measure_Info import Insert_Measure_Info
from PIL import Image, ImageDraw, ImageFont
import numpy as np
from pathlib import Path
from Combine_database import fetch_newest_temperature_db, Update_Measure_Info
from get_file_path import get_file_path

# Return:
# min_distance: smallest distance
# id: the id of the min_distance

def find_closest(list_of_face_encodings, unknown_face_encoding):
    distances = face_recognition.face_distance(
        list_of_face_encodings, unknown_face_encoding)
    min_distance = min(distances)
    id = distances.argmin()
    return id, min_distance

# Get the indices of faces in a list of frames which
#  corresponding to each face in the last frame.
def same_face_indices(prev_encodings, prev_locations, tolerance):
    selected_bools = []
    for col, x in enumerate(prev_encodings):
        selected_bools.append([])
        for y in x:
            selected_bools[col].append(False)
    same_indices = [[] for x in range(len(prev_encodings[-1]))]
    for i, (last_encoding, last_location, indices) \
            in enumerate(zip(
                prev_encodings[-1],
                prev_locations[-1],
                same_indices
            )):
        for row, (encodings, locations, bools)\
                in enumerate(zip(
                    prev_encodings[:-1],
                    prev_locations[:-1],
                    selected_bools[:-1]
                )):
            closest = None
            min_distance = 1.0
            for col in range(len(encodings)):
                distance = face_recognition.face_distance(
                    [encodings[col]], last_encoding
                )
                if distance < min_distance and distance < tolerance\
                        and bools[col] is False:
                    closest = col
                    min_distance = distance
            if closest is not None:
                bools[closest] = True
                indices.append((row, closest))
        indices.append((len(prev_encodings) - 1, i))
    return same_indices

def draw_results(frame, locations, results, user_names,
                 tolerance, font, scale=2):
    rgb_frame = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
    for (top, right, bottom, left), (_, min_distance), user_name \
            in zip(locations, results, user_names):
        top *= scale
        right *= scale
        bottom *= scale
        left *= scale

        draw = ImageDraw.Draw(rgb_frame)
        rectColor = (255, 0, 0)
        draw.rectangle([(left, top), (right, bottom)],
                       fill=None, outline=rectColor, width=5)

        draw.rectangle([(left, bottom - 70), (right, bottom)],
                       fill=rectColor, outline=None)
        textColor = (255, 255, 255)
        draw.text((left + 6, bottom - 72),
                  str(f'{user_name}, Diff: {min_distance: > .4f}'),
                  font=font, fill=textColor)
        match_state = "Not Matched" if min_distance > tolerance else "Matched"
        draw.text((left + 6, bottom - 36),
                  str(f' {match_state}'),
                  font=font, fill=textColor)
    result_frame = cv2.cvtColor(np.asarray(rgb_frame), cv2.COLOR_RGB2BGR)
    return result_frame

def main():
    database_name = './Release/teacher.db'
    tolerance = 0.35
    frame_buffer_size = 10  # number of buffered frames to generate result
    # [[en_face, ID, Name], ...]
    user_profile_list = get_user_profiles(database_name)
    known_face_encodings = [x[0] for x in user_profile_list]
    guest_encodings = []

    font = ImageFont.truetype(
        str(Path(get_file_path()).joinpath('NotoSansCJK-Regular222.ttc')), 20)
    prev_locations = []  # Previous face locations in buffered frames
    prev_encodings = []  # Previous face encodings in buffered frames
    # Indices of previous matched encodings in buffered frames
    prev_matched_ids = []
    time_dict = {}  # {id: leaving_time}
    last_detected_time = dt.datetime.min
    results = []  # [[closest_id, distance], ...]

    guest_count = 0
    user_names = []  # name of detected people in the current frame
    user_name_dict = {}  # {id: name}
    record_frame_count = 0

    video_capture = cv2.VideoCapture(1)
    for frame_count in itertools.count():
        _, frame = video_capture.read()

        # For every two frames, Skip one frame.
        if frame_count % 2 == 0:
            small_frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5)
            rgb_small_frame = small_frame[:, :, ::-1]
            new_locations = face_recognition.face_locations(rgb_small_frame)
            if len(new_locations) > 0 or (
                dt.datetime.now() - last_detected_time
            ).total_seconds() > 2.0:
                locations = new_locations
                last_detected_time = dt.datetime.now()
                encodings = face_recognition.face_encodings(
                    rgb_small_frame, locations)
                matched_ids = []
                for encoding in encodings:
                    id, distance = find_closest(known_face_encodings, encoding)
                    if distance > tolerance and len(guest_encodings) > 0:
                        id2, distance2 = find_closest(
                            guest_encodings, encoding)
                        id2 += len(known_face_encodings)
                        if distance2 < distance:
                            id, distance = id2, distance2
                    matched_ids.append(id)
                start = 0 if len(prev_locations) < frame_buffer_size else 1
                prev_locations[0:] = prev_locations[start:] + [locations]
                prev_encodings[0:] = prev_encodings[start:] + [encodings]
                prev_matched_ids[0:] = prev_matched_ids[start:] + [matched_ids]
                record_frame_count += 1
                # Generate results every frame_buffer_size frames
                if (record_frame_count % frame_buffer_size
                        == frame_buffer_size - 1):
                    # Get the indices of encodings that have the most matching
                    indices_list = same_face_indices(
                        prev_encodings, prev_locations, tolerance)
                    results = []
                    for encoding, location, indices in zip(
                            encodings, locations, indices_list):
                        ids = [prev_matched_ids[row][col]
                               for row, col in indices]
                        id = Counter(ids).most_common(1)[0][0]
                        distance = face_recognition.face_distance(
                            (known_face_encodings + guest_encodings)[id: id+1],
                            encoding)[0]
                        results.append(
                            (id, distance)
                        )
                    user_names = []
                    now = datetime.now()
                    for i, (id, min_distance) in enumerate(results):
                        # Is new guest
                        if min_distance > tolerance:
                            user_name = f'訪客{guest_count + 1}'
                            id = len(known_face_encodings) + \
                                len(guest_encodings)
                            guest_encodings.append(encodings[i])
                            guest_count += 1
                        # Is in database
                        elif id < len(known_face_encodings):
                            user_name = user_profile_list[id][2]
                        # Is old guest
                        else:
                            user_name = user_name_dict[id]

                        # 若現在距離人臉最後偵測時間大於十秒，將資料寫進資料庫

                        if (now - time_dict.setdefault(
                            user_name, dt.datetime.min))\
                                .total_seconds() > 10.0:
                            Insert_Measure_Info(
                                database_name, [user_name, now])
                            data = fetch_newest_temperature_db(database_name)
                            Update_Measure_Info(database_name, data)
                            print('寫入')

                        print(user_name)
                        time_dict[user_name] = now
                        user_name_dict[id] = user_name
                        user_names.append(user_name)
        # TODO: Match locations with results
        result_frame = draw_results(
            frame, locations, results, user_names, tolerance, font)
        cv2.imshow('Video', result_frame)
        # Press q to quit
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

if __name__ == '__main__':
    main()


import os, time
from nose.tools import assert_raises
from webassets import Environment, Bundle
from webassets.exceptions import BundleError
from webassets.updater import TimestampUpdater, BundleDefUpdater, SKIP_CACHE
from webassets.cache import MemoryCache
from helpers import TempEnvironmentHelper

class TestBundleDefBaseUpdater:
    """Test the updater which caches bundle definitions to determine
    changes.
    """

    def setup(self):
        self.env = Environment(None, None)  # we won't create files
        self.env.cache = MemoryCache(capacity=100)
        self.bundle = Bundle(output="target")
        self.updater = BundleDefUpdater()

    def test_no_rebuild_required(self):
        # Fake an initial build
        self.updater.build_done(self.bundle, self.env)
        # without any changes, no rebuild is required
        assert self.updater.needs_rebuild(self.bundle, self.env) == False

        # Build of another bundle won't change that, i.e. we use the
        # correct caching key for each bundle.
        bundle2 = Bundle(output="target2")
        self.updater.build_done(bundle2, self.env)
        assert self.updater.needs_rebuild(self.bundle, self.env) == False

    def test_filters_changed(self):
        self.updater.build_done(self.bundle, self.env)
        self.bundle.filters += ('jsmin',)
        assert self.updater.needs_rebuild(self.bundle, self.env) == True

    def test_contents_changed(self):
        self.updater.build_done(self.bundle, self.env)
        self.bundle.contents += ('foo.css',)
        assert self.updater.needs_rebuild(self.bundle, self.env) == True

    def test_debug_changed(self):
        self.updater.build_done(self.bundle, self.env)
        self.bundle.debug = not self.bundle.debug
        assert self.updater.needs_rebuild(self.bundle, self.env) == True

    def test_depends_changed(self):
        # Changing the depends attribute of a bundle will NOT cause
        # a rebuild. This is a close call, and might just as well work
        # differently. I decided that the purity of the Bundle.__hash__
        # implementation in not including anything that isn't affecting
        # to the final output bytes was more important. If the user
        # is changing depends than after the next rebuild that change
        # will be effective anyway.
        self.updater.build_done(self.bundle, self.env)
        self.bundle.depends += ['foo']
        assert self.updater.needs_rebuild(self.bundle, self.env) == False

class TestTimestampUpdater(TempEnvironmentHelper):

    default_files = {'in': '', 'out': ''}

    def setup(self):
        TempEnvironmentHelper.setup(self)

        # Test the timestamp updater with cache disabled, so that the
        # BundleDefUpdater() base class won't interfere.
        self.m.cache = False
        self.m.updater = TimestampUpdater()

    def test_timestamp_behavior(self):
        bundle = self.mkbundle('in', output='out')

        # Set both times to the same timestamp
        now = self.setmtime('in', 'out')
        assert self.m.updater.needs_rebuild(bundle, self.m) == False

        # Make in file older than out file
        now = self.setmtime('in', mtime=now-100)
        now = self.setmtime('out', mtime=now)
        assert self.m.updater.needs_rebuild(bundle, self.m) == False

        # Make in file newer than out file
        now = self.setmtime('in', mtime=now)
        now = self.setmtime('out', mtime=now-100)
        assert self.m.updater.needs_rebuild(bundle, self.m) == True

    def test_source_file_deleted(self):
        """If a source file is deleted, rather than raising an error
        when failing to check it's timestamp, we ask for a rebuild.

        The reason is that when a wildcard is used to build the list of
        source files, this is the behavior we want: At the time of the
        update check, we are presumably using a cached version of the
        bundle contents. For the rebuild, the contents are refreshed.

        So if the file that is missing was included via a wildcard, the
        rebuild will go through, without the file. If a file is gone
        missing that he user specifically added to the bundle, then the
        build process is going to raise the error (this is tested
        separately).
        """
        self.create_files({'1.css': '', '2.css': ''})
        bundle = self.mkbundle('in', '*.css', output='out')

        # Set all mtimes to the same timestamp
        self.setmtime('in', 'out', '1.css', '2.css')
        assert self.m.updater.needs_rebuild(bundle, self.m) == False

        # Delete a wildcarded file
        os.unlink(self.path('1.css'))
        assert self.m.updater.needs_rebuild(bundle, self.m) == True

    def test_bundle_definition_change(self):
        """Test that the timestamp updater uses the base class
        functionality of determining a bundle definition change as
        well.
        """
        self.m.cache = MemoryCache(capacity=100)
        bundle = self.mkbundle('in', output='out')
        now = time.time()

        # Fake an initial build
        self.m.updater.build_done(bundle, self.m)

        # Make in file older than out file
        os.utime(self.path('in'), (now, now-100))
        os.utime(self.path('out'), (now, now))
        assert self.m.updater.needs_rebuild(bundle, self.m) == False

        # Change the bundle definition
        bundle.filters = 'jsmin'

        # Timestamp updater will says we need to rebuild.
        assert self.m.updater.needs_rebuild(bundle, self.m) == True
        self.m.updater.build_done(bundle, self.m)

    def test_depends(self):
        """Test the timestamp updater properly considers additional
        bundle dependencies.
        """
        self.create_files({'d.sass': '', 'd.other': ''})
        bundle = self.mkbundle('in', output='out', depends=('*.sass',))

        # First, ensure that the dependency definition is not
        # unglobbed until we actually need to do it.
        internal_attr = '_resolved_depends'
        assert not getattr(bundle, internal_attr, None)

        now = time.time()

        # Make all files older than the output
        now = self.setmtime('out')
        self.setmtime('in', 'd.sass', 'd.other', mtime=now-100)
        assert self.m.updater.needs_rebuild(bundle, self.m) == False

        # Touch the file that is supposed to be unrelated
        self.setmtime('d.other', mtime=now+100)
        assert self.m.updater.needs_rebuild(bundle, self.m) == False

        # Touch the dependency file - now a rebuild is required
        self.setmtime('d.sass', mtime=now+100)
        assert self.m.updater.needs_rebuild(bundle, self.m) == SKIP_CACHE

        # Finally, counter-check that our previous check for the
        # internal attribute was valid.
        assert hasattr(bundle, internal_attr)

    def test_depends_nested(self):
        """Test the dependencies of a nested bundle are checked too.
        """
        self.create_files({'dependency': ''})
        bundle = self.mkbundle('in', Bundle('in', depends='dependency'),
                               output='out', )
        now = self.setmtime('out')
        self.setmtime('in', mtime=now-100)
        self.setmtime('dependency', mtime=now+100)

        assert self.m.updater.needs_rebuild(bundle, self.m) == SKIP_CACHE

    def test_wildcard_dependency_deleted(self):
        """If a dependency is deleted, a rebuild is always
        required.

        [Regression] This used to raise an error.
        """
        self.create_files({'1.sass': '', '2.sass': ''})
        bundle = self.mkbundle('in', output='out', depends=('*.sass',))

        # Set mtimes so that no update is required
        self.setmtime('1.sass', '2.sass', 'in', 'out')
        assert self.m.updater.needs_rebuild(bundle, self.m) == False

        # Delete a dependency
        os.unlink(self.path('1.sass'))
        # Now we need to update
        assert self.m.updater.needs_rebuild(bundle, self.m) == SKIP_CACHE

    def test_static_dependency_missing(self):
        """If a statically referenced dependency does not exist,
        an error is raised.
        """
        bundle = self.mkbundle('in', output='out', depends=('file',))
        assert_raises(BundleError, self.m.updater.needs_rebuild, bundle, self.m)


import os
import abc

import numpy as np
import scipy.linalg
import scipy.signal as sig

# TODO: Clean up this interface
class Basis(object):
    __metaclass__ = abc.ABCMeta

    def __init__(self, B, dt, dt_max,
                 orth=False,
                 norm=False,
                 allow_instantaneous=False):
        self.B = B
        self.dt = dt
        self.dt_max = dt_max
        self.orth = orth
        self.norm = norm
        self.allow_instantaneous = allow_instantaneous

        self.basis = self.interpolate_basis(self.create_basis(), self.dt, self.dt_max, self.norm)
        self.L = self.basis.shape[0]

    @abc.abstractmethod
    def create_basis(self):
        raise NotImplementedError()

    def convolve_with_basis(self, S):
        """
        Convolve each column of the event count matrix with this basis
        :param S:     TxK matrix of inputs.
                      T is the number of time bins
                      K is the number of input dimensions.
        :return: TxKxB tensor of inputs convolved with bases
        """
        (T,K) = S.shape
        (R,B) = self.basis.shape

        # Initialize array for filtered stimulus
        F = np.empty((T,K,B))

        # Compute convolutions fo each basis vector, one at a time
        for b in np.arange(B):
            F[:,:,b] = sig.fftconvolve(S,
                                       np.reshape(self.basis[:,b],(R,1)),
                                       'full')[:T,:]

        # Check for positivity
        if np.amin(self.basis) >= 0 and np.amin(S) >= 0:
            np.clip(F, 0, np.inf, out=F)
            assert np.amin(F) >= 0, "convolution should be >= 0"

        return F

    def interpolate_basis(self, basis, dt, dt_max,
                          norm=True):
        # Interpolate basis at the resolution of the data
        L,B = basis.shape
        t_int = np.arange(0.0, dt_max, step=dt)
        t_bas = np.linspace(0.0, dt_max, L)

        ibasis = np.zeros((len(t_int), B))
        for b in np.arange(B):
            ibasis[:,b] = np.interp(t_int, t_bas, basis[:,b])

        # Normalize so that the interpolated basis has volume 1
        if norm:
            # ibasis /= np.trapz(ibasis,t_int,axis=0)
            ibasis /= (dt * np.sum(ibasis, axis=0))

        if not self.allow_instantaneous:
            # Typically, the impulse responses are applied to times
            # (t+1:t+R). That means we need to prepend a row of zeros to make
            # sure the basis remains causal
            ibasis = np.vstack((np.zeros((1,B)), ibasis))

        return ibasis

    def create_basis(self):
        raise NotImplementedError("Override this in base class")

class CosineBasis(Basis):
    """
    Create a basis of raised cosine tuning curves
    """
    def __init__(self,
                 B, dt, dt_max,
                 orth=False,
                 norm=True,
                 allow_instantaneous=False,
                 n_eye=0,
                 a=1.0/120,
                 b=0.5,
                 L=100):

        self.n_eye = n_eye
        self.a = a
        self.b = b
        self.L = L

        super(CosineBasis, self).__init__(B, dt, dt_max, orth, norm, allow_instantaneous)

    def create_basis(self):
        n_pts = self.L              # Number of points at which to evaluate the basis
        n_cos = self.B - self.n_eye # Number of cosine basis functions'
        n_eye = self.n_eye          # Number of identity basis functions
        assert n_cos >= 0 and n_eye >= 0

        n_bas = n_eye + n_cos
        basis = np.zeros((n_pts,n_bas))

        # The first n_eye basis elements are identity vectors in the first time bins
        basis[:n_eye,:n_eye] = np.eye(n_eye)

        # The remaining basis elements are raised cosine functions with peaks
        # logarithmically warped between [n_eye*dt:dt_max].

        a = self.a                          # Scaling in log time
        b = self.b                          # Offset in log time
        nlin = lambda t: np.log(a*t+b)      # Nonlinearity
        u_ir = nlin(np.arange(n_pts))       # Time in log time
        ctrs = u_ir[np.floor(np.linspace(n_eye,(n_pts/2.0),n_cos)).astype(np.int)]
        if len(ctrs) == 1:
            w = ctrs/2
        else:
            w = (ctrs[-1]-ctrs[0])/(n_cos-1)    # Width of the cosine tuning curves

        # Basis function is a raised cosine centered at c with width w
        basis_fn = lambda u,c,w: (np.cos(np.maximum(-np.pi,np.minimum(np.pi,(u-c)*np.pi/w/2.0)))+1)/2.0
        for i in np.arange(n_cos):
            basis[:,n_eye+i] = basis_fn(u_ir,ctrs[i],w)

        # Orthonormalize basis (this may decrease the number of effective basis vectors)
        if self.orth:
            basis = scipy.linalg.orth(basis)
        if self.norm:
            # We can only normalize nonnegative bases
            if np.any(basis<0):
                raise Exception("We can only normalize nonnegative impulse responses!")

            # Normalize such that \int_0^1 b(t) dt = 1
            basis = basis / np.tile(np.sum(basis,axis=0), [n_pts,1]) / (1.0/n_pts)

        return basis

class IdentityBasis(Basis):

    def __init__(self,
                 dt, dt_max,
                 norm=True,
                 allow_instantaneous=False):

        self.dt = dt
        self.dt_max = dt_max
        self.allow_instantaneous = allow_instantaneous
        self.norm = norm
        self.B = int(dt_max // dt)

        if allow_instantaneous:
            self.B += 1

        # The length is always the current time bin + dt_max / dt
        self.L = dt_max // dt + 1

        # Save allow instantaneous
        self.basis = self.create_basis()

    def create_basis(self):
        ibasis = np.eye(self.B)

        # Typically we don't allow the instantaneous connection
        # since this is no longer a generative model
        if not self.allow_instantaneous:
            ibasis = np.vstack((np.zeros((1,self.B)), ibasis))

        assert ibasis.shape == (self.L, self.B)

        # Normalize so that the interpolated basis has volume 1
        if self.norm:
            ibasis /= (self.dt * np.sum(ibasis, axis=0))

        return ibasis


from django.urls import path
from . import views

app_name = 'Card'
urlpatterns = [
    path('review_card', views.go_review),
    path('ShowCards', views.get_cards),
    path('addCard', views.add_card),
    path('ApplyPermission', views.apply_permission),
    path('ModifyCard', views.modify_card),
    path('RemoveCard', views.remove_card),
    path('GetMemoryCar', views.get_memory_card),
    path('RememberCard', views.remember_card),
    path('ForgetCard', views.forget_card)
]


#!/usr/bin/env python

"""
Author:  Giovanni merlos Mellini
License: GNU General Public License v3.0
         https://github.com/gmellini/twitter-scraper/blob/master/LICENSE

This script allows you to get a complete list of twitter threads replies
so you can have a fast and complete view of complex threads even if you
are not cited in all the tweet branches

Check https://github.com/gmellini/twitter-scraper to read more on this

= Twitter Access Tokens =
Generate here
 https://developer.twitter.com/en/docs/basics/authentication/guides/access-tokens.html

and modify [ CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET ]

= LIMITATION =
Twitter search API only returns results from last 7 days. This means that
search results are limited to last 7 days

= TWITTER-SCRAPER-PY =
twitter-scraper.py reads a text file fileed with tweets URL (one per line) in
the following format https://twitter.com/<SCREEN_NAME>/status/<ID>
The script checks and writes to stdout any reply to the given tweet, keeping
replies indentation.

Option -s gives a short output that can be useful to diff content between
different iterations of the script; this way you can check for newer replies
and notify.

usage: twitter-scraper.py -f file [-s]
Options:
-f  : name of the input file that contains twitter URLs (1 per line) in the following
      format: https://twitter.com/<SCREEN_NAME>/status/<ID>
-s  : csv output; useful to diff content between different iterations of the script

= REQUIREMENTS =
The script is tested with python 2.7 and 3.6 on Ubuntu 18.04 and 18.10
- install required pip packages:
    # Python 2.7
    sudo pip install python-twitter
    sudo pip install pytz
    # Python 3
    sudo pip3 install python-twitter
    sudo pip3 install pytz
- modify your timezone to have local dates
    - Full list here: https://gist.github.com/heyalexej/8bf688fd67d7199be4a1682b3eec7568
    - Current: local_timezone = 'Europe/Rome'

= EXAMPLES =
$ ./twitter-scraper.py  -f tweet.list
[...]
$ ./twitter-scraper.py  -f tweet.list -s
[...]

= CREDITS =
Based on the initial work made by @edsu
 https://gist.github.com/edsu/54e6f7d63df3866a87a15aed17b51eaf
"""

import sys
import getopt
import json
import time
import twitter
import re
import os.path
import locale
from os import environ as e
from datetime import datetime
from pytz import timezone
try:
    import urllib # python 2.7
except ImportError:
    import urllib.parse # python 3

################################################
# SET YOUR TWITTER KEYS/SECRETS
e["consumer_key"]="CONSUMER_KEY"
e['consumer_secret']="CONSUMER_SECRET"
e["access_token_key"]="ACCESS_TOKEN"
e['access_token_secret']="ACCESS_TOKEN_SECRET"
# SET TIMEZONE FOR OUTPUT
local_timezone = 'Europe/Rome'
################################################

# Global vars
t = twitter.Api(
    consumer_key=e["consumer_key"],
    consumer_secret=e["consumer_secret"],
    access_token_key=e["access_token_key"],
    access_token_secret=e["access_token_secret"],
    sleep_on_rate_limit=True
)

tweet_url = None
tweet_user = None
twitter_id = None
twitter_sceen_name = None
short_output = False

# True to have debug messages
debug = False

def print_usage():
    print ('usage: twitter-scraper.py -f file [-s]')
    print ('Options:')
    print ('-f\t: name of the input file that contains twitter URLs (1 per line) in the following format: https://twitter.com/<SCREEN_NAME>/status/<ID>')
    print ('-s\t: csv output; useful to diff content between different iterations of the script')

def get_tweets(filename):
    global twitter_id
    global twitter_sceen_name
    global tweet_url
    global short_output

    for line in open(filename):
        if not short_output: print('=========================================')
        line.strip()
        if not re.search('^#', line):
            m = re.search('https://twitter.com/(.*)/status/(.*)', line)
            if m:
                twitter_sceen_name = m.group(1)
                twitter_id = m.group(2)
            else:
        	    if not short_output: print('[WARNING] Malformed URL - Not scraping %s' % (line))
        	    continue
            twitter_json = '{"user":{"screen_name": "'+twitter_sceen_name+'"},"id": '+twitter_id+'}'
            tweet_url = 'https://twitter.com/%s/status/%s' % (twitter_sceen_name, twitter_id)
            if not short_output:
                print('[INFO] Start scraping from tweet URL %s' % (tweet_url))
                print('')
            yield twitter.Status.NewFromJsonDict(json.loads(twitter_json))
        else:
            if not short_output: print('[INFO] Comment - %s' % (line))
            continue

def get_replies(tweet):
    global tweet_user
    global debug
    global short_output

    tweet_user = tweet.user.screen_name
    tweet_id = tweet.id
    max_id = None
    page_index = 0
    while True:
        page_index += 1
        try:
            term = "to:%s" % tweet_user
            if debug: print('[DEBUG] Search: term=%s since_id=%s max_id=%s count=100' % (term, tweet_id, max_id))
            replies = t.GetSearch(
                term=term,
                since_id=tweet_id,
                max_id=max_id,
                count=100,
            )
        except twitter.error.TwitterError as e:
            if not short_output: print('[WARNING] Caught twitter api error, sleep for 60s: %s', (e))
            time.sleep(60)
            continue

        if not replies:
            if debug: print('[DEBUG] Found no replies. Search continues...')
            if debug: print('-----------------------------------------')
            break

        for reply in replies:
            if reply.in_reply_to_status_id == tweet_id:
                if debug: print('[DEBUG] Reply found')
                yield reply
                # recursive magic to also get replies to the reply
                for reply_to_reply in get_replies(reply):
                    yield reply_to_reply
            max_id = reply.id

        max_id = reply.id - 1

        if len(replies) != 100:
            break

def main(argv):
    global tweet_url
    global twitter_id
    global short_output
    global local_timezone

    # Parse ARGV[]
    try:
        opts, args = getopt.getopt(argv, "hsf:")
    except getopt.GetoptError:
        print_usage()
        sys.exit(2)

    find_f = False
    for opt, arg in opts:
        if opt == '-h':
            print_usage()
            sys.exit(0)
        elif opt == '-f':
            file_in = arg
            find_f = True
        elif opt == '-s':
            short_output = True

    if find_f is False:
        print('[ERROR] Missing mandatory option -f')
        print('')
        print_usage()
        sys.exit(2)

    if not os.path.exists(file_in):
        print('[ERROR] File %s not exists' % (file_in))
        print('')
        print_usage()
        sys.exit(2)

    # Verify key/secrets
    if e["consumer_key"] == "CONSUMER_KEY" or e['consumer_secret'] == "CONSUMER_SECRET" or e["access_token_key"] == "ACCESS_TOKEN" or e['access_token_secret'] == "ACCESS_TOKEN_SECRET":
        print('[ERROR] It seems that Twitter key/secrets where not set.')
        print('\tGenerate here https://developer.twitter.com/en/docs/basics/authentication/guides/access-tokens.html and set to start playing')
        print('')
        print_usage()
        sys.exit(2)

    if short_output: print('date,reply,parent_thread');

    for tweet in get_tweets(file_in):
        if tweet_url is not None:
            tabs = ''
            last = None
            count = 0
            for reply in get_replies(tweet):
                j = json.loads(reply.AsJsonString())

                if int(reply.in_reply_to_status_id) == int(twitter_id):
                	tabs = ''
                elif int(reply.in_reply_to_status_id) == int(last):
                    tabs = '%s  ' % tabs
                else:
                    tabs = '%s\b\b' % tabs

                last = j['id']
                pre_human_date = j['created_at'].replace('+0000 ', '')
                human_date = datetime.strptime(pre_human_date, '%a %b %d %H:%M:%S %Y').replace(tzinfo=timezone('UTC')).astimezone(tz=timezone(local_timezone)).strftime('%d/%m/%Y %H:%M:%S')
                count += 1

                if not short_output:
                    print('%s/----------------------------------' % (tabs))
                    print('%s| From:\t %s (@%s)' % (tabs, j['user']['name'], j['user']['screen_name']))
                    if debug: print('%s| Twitter date:\t %s' % (tabs, j['created_at']))
                    print('%s| Date:\t %s' % (tabs, human_date))
                    print('%s| URL:\t https://twitter.com/%s/status/%s' % (tabs, j['user']['screen_name'], j['id']))
                    print('%s| %s' % (tabs, j['text']))
                    print('%s\\----------------------------------' % (tabs))
                    print ('')

                if short_output: print('%s,https://twitter.com/%s/status/%s,%s' % (human_date, j['user']['screen_name'], j['id'], tweet_url))

                if debug:
                    print('')
                    print('[DEBUG] status ID: %s' % (j['id']))
                    print('[DEBUG] in reply to: %s' % (j['in_reply_to_status_id']))
                    print('[DEBUG] JSON response:')
                    print(reply.AsJsonString())
            if not short_output:
                print('')
                print('[INFO] Total replies: %s' % (count))

    if not short_output: print('=========================================')

if __name__ == "__main__":
    main(sys.argv[1:])
    sys.exit(0)


import sys
import os
import numpy as np
import cv2 as cv
import tools as tool
import time as t
from PIL import	Image

args = sys.argv
folder_path = args[1]
sign_path = args[2]
dst_path = args[3]

t1 = t.time()

num_photos = len(os.listdir(os.getcwd()))
print("Processing", num_photos, "photos in", folder_path)
print("Using signature in", sign_path)

for filename in tool.get_files(folder_path):
	# Extract EXIF from original image
	im = Image.open(filename)
	exif_data = im.info['exif']

	# Run Signature tool
	signed = tool.sign_image(filename,sign_path,verbose=True)
	signed_filename = dst_path + "/" + filename
	cv.imwrite(signed_filename,signed)

	# Write EXIF on new image
	im_new = Image.open(signed_filename)
	image_new.save(signed_filename, 'JPEG', exif=exif_data)

t2 = t.time() - t1
print(" ")
print(num_photos,"photos processed in",round(t2,3),"s")
print("Signed photos saved in ",dst_path)


"""
Config management for llm-api
"""

import logging
import os
from typing import Any, Dict, Optional

import yaml
from pydantic import BaseSettings

logger = logging.getLogger("llm-api.config")

class Settings(BaseSettings):  # pylint: disable=too-few-public-methods
    """
    Settings class to configure the app
    """

    models_dir: str = "./models"
    model_family: str
    model_params: Optional[Dict[str, Any]] = {}
    setup_params: Dict[str, Any] = {}
    host: str = "0.0.0.0"
    port: int = 8000
    log_level: str = "info"

    class Config:  # pylint: disable=too-few-public-methods
        """
        Configurations customizations
        """

        env_prefix = "LLM_API_"

        @classmethod
        def customise_sources(
            cls,
            init_settings,
            env_settings,
            file_secret_settings,
        ):
            """
            Customization of the config hooks to add a yaml file settings source
            """
            return (
                init_settings,
                env_settings,
                yaml_config_settings_source,
                file_secret_settings,
            )

def yaml_config_settings_source(
    base_settings: BaseSettings,  # pylint: disable=unused-argument
) -> Dict[str, Any]:
    """
    YAML file settings source
    """
    if not os.path.exists("config.yaml"):
        logger.warning("no config file found")
        return {}
    try:
        logger.info("loading config file config.yaml")
        with open("config.yaml", encoding="utf-8") as conf_file:
            data = yaml.load(conf_file, Loader=yaml.FullLoader)
        if data is None:
            logger.warning("config file is empty")
            return {}
        logger.info(str(data))
        return data
    except yaml.YAMLError as exp:
        raise exp

settings = Settings()


from datetime import date

from django.core.validators import MaxValueValidator

from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from rest_framework.validators import UniqueTogetherValidator

from birthdata.models import Town, Writer

class WriterSerializer(serializers.Serializer):
    firstname = serializers.CharField(max_length=100)
    patronymic = serializers.CharField(max_length=100)
    lastname = serializers.CharField(max_length=100)
    birth_place = serializers.SlugRelatedField(
        slug_field='name',
        queryset=Town.objects
    )
    birth_date = serializers.DateField(
        format='%d.%m.%Y',
        input_formats=['%d.%m.%Y', 'iso-8601'],
        validators=[MaxValueValidator(date(1999, 12, 31))]
    )

    class Meta:
        validators = [
            UniqueTogetherValidator(
                queryset=Writer.objects,
                fields=['firstname', 'patronymic', 'lastname']
            )
        ]

    def validate(self, attrs):
        set_attrs = set(
            [attrs['firstname'], attrs['patronymic'], attrs['lastname']]
        )
        if len(set_attrs) != 3:
            raise ValidationError(
                'Имя, отчество и фамилия не могут совпадать между собой',
                code='duplicate values'
            )
        return attrs

    def create(self, validated_data):
        return Writer.objects.create(**validated_data)


import os
import numpy as np
import pandas as pd
from datetime import datetime
import matplotlib.pyplot as plt
from ohcoachtools.gpscoordtools import gpspoint_to_fieldlocal
from ohcoachtools.myconstants import *
from ohcoachtools.field import Field

if __name__ == '__main__':
    # Load stadium coordinates
    field_filepath = os.path.join(DATA_DIR, 'field_01.csv')
    field_coord = np.array(pd.read_csv(field_filepath, sep=',', header=None))
    field = Field()
    field.set_gps_corners(field_coord)

    # Read gps data
    gps_filepath = os.path.join(DATA_DIR, 'sample_01.och')
    och_df = pd.read_csv(gps_filepath, sep=',', header=0, names=OCH_HEADER)

    # Convert strings to datetimes
    och_df[LABEL_DATETIME] = och_df[LABEL_DATETIME].apply(
        lambda x: datetime.strptime(x, '%Y.%m.%d.%H.%M.%S.%f')
    )
    # Compute local coordinates
    och_df[[LABEL_X, LABEL_Y]] = och_df.apply(
        lambda x: gpspoint_to_fieldlocal(x[LABEL_LATITUDE], x[LABEL_LONGITUDE], field),
        axis=1, result_type='expand'
    )

    # Visualization
    plt.plot(och_df[LABEL_X], och_df[LABEL_Y])
    plt.title('Player Trajectory')
    plt.xlim(0, field.width)
    plt.ylim(0, field.height)
    plt.grid(True)
    plt.show()


import logging
from os import system
import logging.config
from time import sleep

from tabulate import tabulate

from helper import (
    Table,
    LogMsg,
    OtherMsg,
    LoadingLogo,
)
from control.class_command import command
from .register import func_register
from .login import func_login
"""
The implementation of the program is from this part, which was written by nested loop
"""

logger = logging.getLogger(__name__)

def main():
    logger.info(LogMsg.start)
    LoadingLogo(OtherMsg.multi_games).show()
    loop_condition_main_page = True
    while loop_condition_main_page:
        # -------------------------MAIN PAGE-------------------------
        system("clear")
        logger.info(LogMsg.main_section)
        main_page = [
                    [Table.description_register],
                    [Table.description_login],
                    [Table.description_help],
                    ]
        print(tabulate(main_page, [Table.main_page.center(35)], tablefmt="heavy_grid"))
        input_command = input(OtherMsg.icon_input).casefold()
        try:
            user_command = command(input_command, ("-login", "-register", "-help", "-exit"))
        except ValueError:
            logger.info(LogMsg.command_report_main.format(input_command))
            print(OtherMsg.valid_command, "'-login' '-register' '-help' '-exit'")
            sleep(0.7)
        else:
            if input_command == "-help":
                logger.info(LogMsg.help_main_section)
                user_command.dash_help()
                input("Press 'enter' on the keyboard to exit -help ")
                system("clear")
            elif input_command == "-exit":
                logger.info(LogMsg.exit_main_section)
                user_command.dash_exit()
                loop_condition_main_page = False
            elif input_command == "-register":
                sleep(0.1)
                func_register(logger)
            elif input_command == "-login":
                sleep(0.1)
                func_login(logger)
    logger.info(LogMsg.end)


#5. Write a Python program to swap two variables without temp variable?
import logging
logging.basicConfig(filename= "Programming Assignment_2.5.log", level= logging.DEBUG)

a = 10
b = 20

logging.info("value of variable a and b are %s and %s before swapping", a , b)

a, b = b, a

logging.info("value of variable a and b are %s and %s after swapping", a , b)


import json
from collections import defaultdict
from functools import wraps
from flask import current_app, Response
from flask_login import login_user
from flask_principal import (Identity, Permission, RoleNeed,
                                 session_identity_loader, identity_changed)
from sqlalchemy import func

#from web.lib.utils import default_handler
from web.models import (Category, Property, Attribute, ShelterPicture,
                        ShelterDocument, Shelter, Section)

admin_role = RoleNeed('admin')
admin_permission = Permission(admin_role)

def jsonify(func):
    """Will cast results of func as a result, and try to extract
    a status_code for the Response object"""
    @wraps(func)
    def wrapper(*args, **kwargs):
        status_code = 200
        result = func(*args, **kwargs)
        if isinstance(result, Response):
            return result
        elif isinstance(result, tuple):
            result, status_code = result
        return Response(json.dumps(result, default=scoped_default_handler()),
                        mimetype='application/json', status=status_code)
    return wrapper

def login_user_bundle(user):
    login_user(user)
    identity_changed.send(current_app, identity=Identity(user.id))
    session_identity_loader()
    # eventually update the last_seen field

def load_shelter_info(shelter_id, section_name):
    shelter = Shelter.query.filter(Shelter.id==shelter_id).first()
    if not shelter:
        raise Exception("No such shelter")

    section = Section.query.filter(
            func.lower(Section.name)==func.lower(section_name.replace('-', ' '))).first()
    if not section:
        raise Exception("No such section")

    categories = defaultdict(list)
    documents = defaultdict(list)
    pictures = defaultdict(list)

    for category in section.categories:

        if category.name == 'Walls & frame':
            superstructure_type = Property.query.filter(
                    Property.shelter_id==shelter.id,
                    Property.category.has(name="Walls & Frame"),
                    Property.category.has(Category.parent_id!=None)).first()
            if superstructure_type:
                superstructure_type_string = superstructure_type.get_values_as_string()
            else:
                superstructure_type_string = ""
        else:
            superstructure_type_string = ""

        for sub_category in category.sub_categories:
            if category.name == 'Walls & frame' and \
                sub_category.name != 'Walls & Frame' and \
                superstructure_type_string.lower()!=sub_category.name.lower():
                continue

            categories[sub_category.name].extend(
                Property.query.filter(
                                    Property.shelter_id==shelter.id,
                                    Property.category.has(name=sub_category.name))
                                .join(Attribute)
                                .order_by(Attribute.display_position.asc())
                                )
            pictures[sub_category.name].extend(
                ShelterPicture.query.filter(
                                    ShelterPicture.shelter_id==shelter.id,
                                    ShelterPicture.category_id==sub_category.id)
                                )
            documents[sub_category.name].extend(
                ShelterDocument.query.filter(
                                    ShelterDocument.shelter_id==shelter.id,
                                    ShelterDocument.category_id==sub_category.id)
                                )
    return shelter, section, categories, pictures, documents


print("hallo wereld")
import logging
import datetime
import time
import requests
from sense_hat import SenseHat
import json
sense = SenseHat()

color = "black"
colornames = {}
colornames["red"] = (255,0,0)
colornames["green"] = (0,255,0)
colornames["blue"] = (0,0,255)
colornames["yellow"] = (255,255,0)
colornames["black"] = (0,0,0)
fsUrl = "https://services9.arcgis.com/7e6lF03RcLhwFtm5/arcgis/rest/services/Selected_Color/FeatureServer/0"
logFilepath = "/home/admin/Documents/logs/updatecolor_{}.log".format(datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))

def main():
	logging.basicConfig(level=logging.DEBUG,format='%(asctime)s %(levelname)s - %(message)s',datefmt='%Y%m%d-%H:%M:%S',handlers=[logging.StreamHandler(),logging.FileHandler(logFilepath)])
	logging.info("Start update color v20231110.01")
	r = 0
	g = 0
	b = 0
	for i in range(0,254):
		sense.clear((r,i,b))
		#time.sleep(0.01)

	logging.info("Startting script")
	sense.show_message("starting",0.05)

	sense.color.gain = 16
	sense.color.integration_cycles = 256
	counter = 0

	logging.info("Showing M")
	sense.show_letter("M")
	x = 0
	y = 0
	rvalue = 75
	gvalue = 75
	while True:
		try:
			sense.set_pixel(x,y,colornames[color])
			y +=1
			if y > 7:
				y = 0
				x+=1
			if x > 7:
				x = 0
				if CheckConnection():
					rvalue = 75
					gvalue = 150
				else:
					rvalue = 150
					gvalue = 75
			sense.set_pixel(x,y,(rvalue,gvalue,75))
			#print("waiting for input")
			for event in sense.stick.get_events():
				logging.info(f"stick event: {event.direction},{event.action}")
				direction = event.direction
				if event.action=="pressed":
					if direction =="middle":
						SentColor()
					if direction == "up":
						SaveColor("red")
					if direction == "right":
						SaveColor("green")
					if direction == "down":
						SaveColor("blue")
					if direction == "left":
						SaveColor("yellow")
			time.sleep(0.1)
		except:
			logging.exception("Error in main loop")

def SaveColor(colorname):
	global color
	if colorname==color:
		colorname= "black"
	logging.info(f"Setting color to: {colorname}")
	sense.clear(colornames[colorname])
	color = colorname

def SentColor():
	try:
		text = f"sending  {color}"
		logging.info(text)
		sense.show_message(text,0.07, text_colour=colornames[color])

		#query feature for its objectid
		queryParams = {"f":"json","where":"1=1","outFields":"*"}
		queryUrl = f"{fsUrl}/query"

		r = requests.get(queryUrl,queryParams)
		print(r.text)
		results = r.json()
		feature = results["features"][0]
		logging.info(feature)
		#updating feature
		feature["attributes"]["color"] = color
		feature["attributes"]["color_count"] = feature["attributes"]["color_count"] + 1
		updateParams = {"f":"json","features":json.dumps([feature])}
		updateUrl = f"{fsUrl}/updateFeatures"

		r = requests.post(updateUrl,updateParams)
		logging.info(r.text)
		results = r.json()

		#{"updateResults":[{"objectId":1,"uniqueId":1,"globalId":null,"success":true}]}
		text = f"results: {results['updateResults'][0]['success']}"

		sense.show_message(text, text_colour=colornames[color])
	except:
		logging.exception("Error in SentColor")

def CheckConnection():
	logging.info("Checking connection")
	retValue = False

	try:
		params = {}
		params["f"] = "json"
		params["where"]="1=1"
		params["returncountOnly"]=True
		queryUrl = f"{fsUrl}/query"
		logging.info(f"requesting {queryUrl}")
		r = requests.get(queryUrl,params)

		result = r.json()
		logging.debug(f"result: {result}")

		if "count" in result:
			retValue = True
	except:
		logging.exception("Error getting value")
	return retValue

if __name__=="__main__":
	main()


import streamlit as st
from io import StringIO
from tools import process
from zipfile import ZipFile
import shutil

import base64
import json
import pickle
import uuid
import re
import os
import streamlit as st
import pandas as pd

# from download import download_button

def download_button(download_filename, button_text, pickle_it=False):
    """
    Generates a link to download the given object_to_download.

    Params:
    ------
    object_to_download:  The object to be downloaded.
    download_filename (str): filename and extension of file. e.g. mydata.csv,
    some_txt_output.txt download_link_text (str): Text to display for download
    link.
    button_text (str): Text to display on download button (e.g. 'click here to download file')
    pickle_it (bool): If True, pickle file.

    Returns:
    -------
    (str): the anchor tag to download object_to_download

    Examples:
    --------
    download_link(your_df, 'YOUR_DF.csv', 'Click to download data!')
    download_link(your_str, 'YOUR_STRING.txt', 'Click to download text!')

    """

    try:

        button_uuid = str(uuid.uuid4()).replace('-', '')
        button_id = re.sub('\d+', '', button_uuid)

        prim_color = st.config.get_option('theme.primaryColor') or '#F43365'
        bg_color = st.config.get_option('theme.backgroundColor') or '#f1f3f6'
        sbg_color = st.config.get_option('theme.secondaryBackgroundColor') or '#f1f3f6'
        txt_color = st.config.get_option('theme.textColor') or '#000000'
        font = st.config.get_option('theme.font') or 'sans serif'

        custom_css = f"""
             """

        with open('files.zip', "rb") as f:
            bytes = f.read()
            b64 = base64.b64encode(bytes).decode()
        dl_link = custom_css + f'{button_text}

'
        return dl_link

    except:
        pass

def get_all_file_paths():
    directory = 'done'
    # initializing empty file paths list
    file_paths = []
    # crawling through directory and subdirectories
    for root, directories, files in os.walk(directory):
        for filename in files:
            # join the two strings in order to form the full filepath.
            filepath = os.path.join(root, filename)
            file_paths.append(filepath)

    # returning all file paths
    return file_paths

code = ''

st.title("Data Processor")
st.write("""puede añadir un nuevo csv usando el componente siguiente, no obstante si ya el csv se encuentra cargado (de previas ejecuciones o agregado manualmente) puede directamente ejecutar el programa tocando el boton""")

uploaded_file = st.file_uploader(" ")
if uploaded_file is not None:
    stringio = StringIO(uploaded_file.getvalue().decode("utf-8"))
    string_data = stringio.read()
    with open('data.csv', 'w') as f:
        f.write(string_data)

if st.button('Comenzar procesamiento'):
    try:
        shutil.rmtree('done/')
        os.remove('files.zip')
    except:
        pass

    code = process()
    # print(code)
if code == 'errors':
    st.error('hubo errores, revisar archivo errors.csv')
elif code == 'notifications':
    st.info('se corrigieron automaticamente algunos errores')
elif code != '':
    st.success('procesado correctamente')

if code != 'errors':
    file_paths = get_all_file_paths()
    if len(file_paths) != 0:
        # writing files to a zipfile
        with ZipFile('files.zip','w') as zip:
            # writing each file one by one
            for file in file_paths:
                zip.write(file)
        st.markdown(download_button('files.zip', 'Descargar archivos'), unsafe_allow_html=True)


import typing
from collections import defaultdict

class Node:
    nodes = {}

    def __init__(self, value: int, left=None, right=None):
        self.value = value
        self.left = left
        self.right = right

__nodes = {}

def node(value: int) -> Node:
    return __nodes.setdefault(value, Node(value))

def print_nodes(node: Node):
    if node.left:
        print_nodes(node.left)
    print(node.value)
    if node.right:
        print_nodes(node.right)

def print_nodes_2(node: Node, lines: typing.List[str], level: int, pos: int):
    if len(lines) <= level:
        lines.append("")
    if node.left:
        print_nodes_2(node.left, lines, level + 1, pos + 1)
    else:
        lines[level] += " "
    if node.right:
        print_nodes_2(node.right, lines, level + 1, pos + 2)
    lines[level] = add(lines[level], node.value, pos)

def add(s: str, value: int, pos: int) -> str:
    s += " " * ((pos * 2) - len(s)) + str(value)
    return s

def main():
    head_node = node(3)
    node(3).left = node(1)
    node(1).right = node(2)
    node(3).right = node(5)
    node(5).right = node(6)
    print_nodes(head_node)
    print("")
    arr = []
    print_nodes_2(head_node, arr, 0, 0)
    for s in arr:
        print(s)

if __name__ == "__main__":
    main()


import streamlit as st
import re
import pandas as pd
import emoji
import os
import spacy
from arabert.preprocess import ArabertPreprocessor
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score, classification_report

@st.cache
def clean_text(text):
    arabic_punctuations = '''`÷×؛<>_()*&^%][ـ،/:"؟.,'{}~¦+|?�!”…“#•–ـ''' # define arabic punctuations
    text = re.sub(r'@\S+', '', text) # remove profile tags
    text = re.sub("[a-zA-Z]", " ", text) # remove english letters
    text = re.sub('\n', ' ', text) # remove \n from text
    text = re.sub(r'\d+', '', text) #remove number
    text = re.sub(r'http\S+', '', text) # remove links
    text = re.sub(r'#\S+', '', text) # remove hashtags
    text = emoji.replace_emoji(text, '') # remove emojies
    # text = text.translate(str.maketrans('','', arabic_punctuations)) # remove punctuation
    text = re.sub(' +', ' ',text) # remove extra space
    text = text.strip() #remove whitespaces
    text = text.replace('@', '')

    return text

@st.cache(allow_output_mutation=True)
def load_arabert():
    model_name = "aubmindlab/bert-base-arabertv2"
    arabert_prep = ArabertPreprocessor(model_name=model_name)
    return arabert_prep

@st.cache
def preprocess_text(text):
  text = re.sub("[a-zA-Z]", " ", text) # remove english letters
  text = re.sub(r'[ء-ي]+@', '', text) # remove profile tags
  text = re.sub(r'[ء-ي]+#', '', text) # remove profile tags
  arabert_prep = load_arabert()
  text = arabert_prep.preprocess(text)
  return text

def get_prediction(text, model):
    result = model(text).cats
    label = sorted(result, key=result.get, reverse=True)[0]
    score = result[label]

    return label, score

@st.cache
def convert_df(df):
    return df.to_csv().encode('utf-8')

@st.cache
def compute_score(target, predicted, score):
    if score == 'accuracy':
        return accuracy_score(target, predicted)
    elif score == 'recall':
        return recall_score(target, predicted, pos_label='not Misogyny')
    elif score == 'precision':
        return precision_score(target, predicted, pos_label='not Misogyny')
    elif score == 'f1_score':
        return f1_score(target, predicted, pos_label='not Misogyny')

@st.cache
def classify_from_df(df, column):
    predicted_classes_off = []
    scores_off = []
    letmi = []
    armis = []
    letmi_armis = []
    fb_votes = []
    scores_misg = []
    path = './models/vaw_misogyny_models/'
    misg = {}
    misg_paths = []
    models = []
    # get misogyny models
    for i in os.listdir(path):
        models.append(i)
        misg_paths.append(path+i)

    # load misogyny models
    loaded_misg_models = []
    for i in misg_paths:
        loaded_misg_models.append(spacy.load(i))

    # classify with models
    predictions_misg = []

    for t in list(df[column]):
        text_cleaned = preprocess_text(t)
        # offensive
        label, score = get_prediction(text_cleaned, st.session_state['nlp_off'])
        label = label.replace('_', ' ')
        predicted_classes_off.append(label)
        scores_off.append(score)
        # misogyny
        for i in range(len(loaded_misg_models)):
            label_misg, score_misg = get_prediction(text_cleaned, loaded_misg_models[i])
            label_misg = 'not Misogyny' if label_misg == 'none' else 'Misogyny'
            if models[i] not in misg.keys():
                misg[models[i]] = []

            misg[models[i]].append(label_misg)

    df_output = pd.DataFrame(list(zip(list(df[column]), predicted_classes_off, scores_off, misg['ArMIS'], misg['LetMI'], misg['LetMI_ArMIS'], misg['Model_fb_data_votes'])), columns=['Text', 'Offensiveness (predicted)', 'Score (Offensiveness)', 'Misogyny (ArMIS)', 'Misogyny (LetMI)', 'Misogyny (LetMI_ArMIS)', 'Misogyny (Model_fb_data_votes)'])
    return df_output


################################################################################
#   Authors:                                                                   #
#       · Alejandro Santorum Varela - alejandro.santorum@estudiante.uam.es     #
#                                     alejandro.santorum@gmail.com             #
#   Date: Apr 14, 2019                                                         #
#   File: fitting.py                                                           #
#   Project: Connect4 - Predicting heuristic values                            #
#   Version: 1.1                                                               #
################################################################################
import time
import numpy as np
from scipy.optimize import curve_fit
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
import matplotlib.pyplot as plt
import pandas as pd

def read_data_from_file(filename, sep):
    xdata = []
    ydata = []
    f = open(filename, "r")
    line = f.readline()
    while line:
        aux = [1]
        array = line.split(sep)
        for elem in array:
            aux.append(float(elem))
        xdata.append(aux[:-1])
        ydata.append(aux[-1])
        line = f.readline()
    f.close()
    return xdata, ydata

def expression_func1(X, th0, th1, th2, th3, th4, th5, th6, th7, th8, th9, th10, th11):
    res = X[0]*th0 + X[1]*th1 + X[2]*th2 + X[3]*th3 + X[4]*th4 + X[5]*th5 + X[6]*th6
    res += X[7]*th7 + X[8]*th8 + X[9]*th9 + X[10]*th10 + X[11]*th11
    return res

def my_test2(xdata_test, ydata_test, popt, intercepter, tol):
    good = 0
    m, n_feat = xdata_test.shape
    for i in range(m):
        pred = 0
        for j in range(n_feat-1):
            pred += popt[j]*xdata_test[i][j+1]
        pred += intercepter
        print("pred:", pred)
        print("ydata_test:", ydata_test[i])
        if pred > tol and ydata_test[i] > tol:
            good += 1
        elif pred < -tol and ydata_test[i] < -tol:
            good += 1
        elif (pred < tol and pred > -tol) and (ydata_test[i] < tol and ydata_test[i] > -tol):
            good += 1
        else:
            continue
    return (good/m)*100

def my_test(y_pred, y_test, tol):
    good = 0
    length = len(y_pred)
    for i in range(length):
        if y_pred[i]>tol and y_test[i]>tol:
            good += 1
        elif y_pred[i]<-tol and y_test[i]<-tol:
            good += 1
        elif (y_pred[i]<tol and y_pred[i]>-tol) and (y_test[i]<tol and y_test[i]>-tol):
            good += 1
        else:
            continue
    return (good/length)*100

def linear_regression(filename, train_test_h, feat_degree):
    # Reading file
    data = np.loadtxt(filename, delimiter=SEP, usecols=range(Y_COL))
    ydata = np.loadtxt(filename, usecols=Y_COL)

    # Feature preparation
    poly_feat = PolynomialFeatures(feat_degree)
    xdata = poly_feat.fit_transform(data)

    # Train and test set
    xdata_train = xdata[:train_test_h]
    xdata_test = xdata[train_test_h:]
    ydata_train = ydata[:train_test_h]
    ydata_test = ydata[train_test_h:]

    # Linear regression (actually polynomial regression)
    lin_reg = LinearRegression()
    # Fitting
    lin_reg.fit(xdata_train, ydata_train)
    # Predicting
    y_pred = lin_reg.predict(xdata_test)

    print("YPRED:")
    print(y_pred)

    # The coefficients
    print('Coefficients: \n', lin_reg.coef_)
    # The mean squared error
    print("Mean squared error: %.2f" % mean_squared_error(ydata_test, y_pred))
    # Explained variance score: 1 is perfect prediction
    print('Variance score: %.2f' % r2_score(ydata_test, y_pred))

    print("My test: ", my_test(y_pred, ydata_test, 1))

def curve_fitting(filename, train_test_h, feat_degree):
    # Reading
    data = np.loadtxt(FILENAME, delimiter=SEP, usecols=range(Y_COL))
    ydata = np.loadtxt(FILENAME, usecols=Y_COL)

    # Feature preparation
    poly_feat = PolynomialFeatures(FEAT_DEGREE)
    xdata = poly_feat.fit_transform(data)

    # Train and test set
    xdata_train = xdata[:TRAIN_TEST_TH]
    xdata_test = xdata[TRAIN_TEST_TH:]
    ydata_train = ydata[:TRAIN_TEST_TH]
    ydata_test = ydata[TRAIN_TEST_TH:]
    #xdata_train = np.transpose(xdata_train) # <--- curve_fit

    #popt, pcov = curve_fit(expression_func1, xdata_train, ydata_train)
    #print("Popt: ", popt)
    #perr = np.sqrt(np.diag(pcov))
    #print("perr: ", perr)

    #print(test(xdata_test, ydata_test, popt, 3))

FILENAME = "190K.csv"
SEP = ", "
TRAIN_TEST_TH = 160000
Y_COL = 11
FEAT_DEGREE = 2

if __name__ == "__main__":
    linear_regression(FILENAME, TRAIN_TEST_TH, FEAT_DEGREE)


from django.contrib.auth.admin import UserAdmin as OriginalUserAdmin
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.admin import UserAdmin
from .models import User
from django.contrib.auth.models import Group
from logging import exception
from django.contrib import admin
from django.contrib import messages
from django.db import IntegrityError
from django.http import Http404, HttpResponseRedirect
from django.urls import reverse
from . models import *
from django.urls import path
from . forms import *
from django.shortcuts import render
from django.http import HttpResponse
from django.utils.html import format_html
from math import ceil
# from django.contrib.auth.models import User
from django.contrib import messages
from import_export.admin import ExportActionMixin
from django_summernote.admin import SummernoteModelAdmin
from django_summernote.utils import get_attachment_model
from django.contrib.auth.admin import UserAdmin
from django.core.mail import send_mail

class AddressAdmin(ExportActionMixin, admin.ModelAdmin):
   #
    # env\Lib\site-packages\jazzmin\static\jazzmin\js\action_button.js
    js = ('jazzmin/js/action_button.js',)
    list_display = ('id', 'user', 'fullname','phone','locality','city','state','address','home','work','default')
    list_filter = ('city', 'user', 'state', 'pincode','home','work')
    list_per_page = 10
    search_fields = ('user', 'city', 'state', 'pincode','home','work')
    list_editable=("home",'work','default')
    # actions=['downloadCV']
    #autocomplete_fields = ['user']
    save_on_top = True
    class Meta:
        verbose_name_plural = "Customer Address"

class imageAdmin(admin.ModelAdmin):
    try:
        def image_tag(self, obj):
            return format_html(''.format(obj.image.url, "100", "100"))
        image_tag.short_description = 'Image'
        image_tag.allow_tags = True
        list_display = ['id', 'image_tag', 'action_btn']

        def action_btn(self, obj):
            html = "
 "
            try:
                html += "

".format(
                    obj.image.url, "100", "100")
            except:
                html += "

".format(
                    obj.image.url)
            html += "
"
            return format_html(html)
        action_btn.short_description = "Action"

        def get_urls(self):
            urls = super().get_urls()
            new_urls = [path('upload-csv/', self.upload_csv), ]
            return new_urls + urls

        def upload_csv(self, request):
            try:
                if request.method == "POST":
                    csv_file = request.FILES["csv_upload"]
                    # csvv=pd.read_csv(csv_file)

                    if not csv_file.name.endswith('.csv'):
                        messages.warning(request, 'Please upload csv file')
                        return HttpResponseRedirect(request.path_info)
                    file_data = csv_file.read().decode("utf-8")
                    csv_data = file_data.split("\r\n")
                    for x in csv_data:
                        fields = x.split(",")
                        try:
                            created = image.objects.create(
                                image="product/"+fields[0],)
                            created.save()
                        except IndexError:
                            pass
                        except (ValidationError, IntegrityError):
                            form = CsvImportForm()
                            data = {"form": form}
                            message = messages.warning(
                                request, 'Something went wrong! check your file again \n 1.Upload correct file \n 2.Check you data once')
                            return render(request, "admin/csv_upload.html", data)
                    url = reverse('admin:index')
                    return HttpResponseRedirect(url)
                form = CsvImportForm()
                data = {"form": form}
                return render(request, "admin/csv_upload.html", data)
            except:
                pass
    except:
        pass

class CategoryAdmin(ExportActionMixin, admin.ModelAdmin):
    try:
        def image_tag(self, obj):
            try:
                return format_html(''.format(obj.category_image.url, "100", "100"))
            except:
                return format_html('')
                #message=messages.warning('Something went wrong! check your file again \n 1.Upload correct file \n 2.Check you data once')
        image_tag.short_description = 'Brand Thumbnail'
        image_tag.allow_tags = True
        list_display = ['id', 'category', 'image_tag',
                        'is_active', 'updated_at', 'action_btn']
        #list_editable = ('slug', )
        list_editable = ('is_active',)
        list_filter = ('category', 'is_active', 'updated_at')
        list_per_page = 10
        search_fields = ('category', 'description', 'is_active', 'updated_at')
        save_on_top = True

        def action_btn(self, obj):
            html = "

"
            try:
                html += "

".format(
                    obj.category_image.url, "100", "100")
            except:
                html += "

"
            html += "
"
            return format_html(html)
        action_btn.short_description = "Action"
        #prepopulated_fields = {"slug": ("title", )}

        def get_urls(self):
            urls = super().get_urls()
            new_urls = [path('upload-csv/', self.upload_csv), ]
            return new_urls + urls

        def upload_csv(self, request):
            try:
                if request.method == "POST":
                    csv_file = request.FILES["csv_upload"]
                    if not csv_file.name.endswith('.csv'):
                        messages.warning(
                            request, 'The wrong file type was uploaded')
                        return HttpResponseRedirect(request.path_info)
                    file_data = csv_file.read().decode("utf-8")
                    csv_data = file_data.split("\r\n")
                    for x in csv_data:
                        fields = x.split(",")
                        try:
                            created = Category.objects.create(
                                brands=fields[0],
                                description=fields[1],
                                category_image="category/"+fields[2],)
                            created.save()
                        except IndexError:
                            pass
                        except (ValidationError, IntegrityError):
                            form = CsvImportForm()
                            data = {"form": form}
                            message = messages.warning(
                                request, 'Something went wrong! check your file again \n 1.Upload correct file \n 2.Check you data once')
                            return render(request, "admin/csv_upload.html", data)
                    url = reverse('admin:index')
                    return HttpResponseRedirect(url)
                form = CsvImportForm()
                data = {"form": form}
                return render(request, "admin/csv_upload.html", data)
            except:
                pass
    except exception:
        raise Http404

class BrandAdmin(ExportActionMixin, admin.ModelAdmin):
    try:
        def image_tag(self, obj):
            try:
                return format_html(''.format(obj.logo.url, "100", "100"))
            except:
                return format_html('')
                #message=messages.warning('Something went wrong! check your file again \n 1.Upload correct file \n 2.Check you data once')
        image_tag.short_description = 'Brand Thumbnail'
        image_tag.allow_tags = True
        list_display = ['id','brand_name', 'image_tag','details','date','action_btn']
        #list_editable = ('slug', )
        #list_editable = ('is_active',)
        list_filter = ('brand_name', 'date')
        #list_per_page = 10
        search_fields = ('brand_name', 'details', 'date')
        save_on_top = True

        def action_btn(self, obj):
            html = "

"
            try:
                html += "

".format(
                    obj.logo.url, "100", "100")
            except:
                html += "

"
            html += "
"
            return format_html(html)
        action_btn.short_description = "Action"
    except:
        pass

class ProductAdmin(ExportActionMixin, admin.ModelAdmin):
    try:
        #inlines = [ProductImageAdmin]
        def imagee(self, obj):
            # a=obj.image.first()
            # print(obj.image.first().image.url)
            try:
                return format_html(''.format(obj.image.first().image.url, "50", "50"))
            except:
                return format_html('')
        imagee.short_description = 'Product Image'

        def image_tag2(self, obj):
            try:
                return format_html(''.format(obj.product_image.url, "50", "50"))
            except:
                return format_html('')
        image_tag2.short_description = 'Product Thumbnail'
        image_tag2.allow_tags = True
        list_display = ['id', 'title',"type",'category','subcategory','brand',"attributes","amps","volts",'imagee', 'price', 'discounted_price','available_stocks', 'is_active',
                        'updated_at','created_at','action_btn']  # ,'is_active','is_featured'
        list_editable = ('category','attributes', 'is_active','brand','available_stocks',"type","amps","volts")
        list_filter = ('category', 'is_active', 'updated_at')
        list_per_page = 20
        #inlines = [ImagemInline]
        search_fields = ('title', 'short_description',
                         'is_active', 'updated_at')
        save_on_top = True
        actions = ['sales_discount', 'delete_offers']
        actions_list = ['sales_discount', 'delete_offers']
        actions_row = ['sales_discount', 'delete_offers']
        actions_detail = ['sales_discount', 'delete_offers']
        def action_btn(self, obj):
            html = "

"
            try:
                html += "

".format(
                    obj.image.first().image.url, "100", "100")
            except:
                html += "

"
            html += "
"
            return format_html(html)
        action_btn.short_description = "Action"

        def delete_offers(self, request, queryset):
            """ from django.db import connection
            cursor = connection.cursor()
            cursor.execute("update app1_product set discounted_price =NULL") """
            for product in queryset:
                product.discounted_price = None
                product.save(update_fields=['discounted_price'])
                if product.discounted_price == 0:
                    from django.db import connection
                    cursor = connection.cursor()
                    cursor.execute(
                        "update app1_product set discounted_price =NULL where discounted_price=0")

        def sales_discount(self, request, queryset):
            Sales = sales.objects.filter(is_active=True)
            # Sales = sales.objects.all()
            # print(Sales)
            for sale in Sales:
                discount = sale.sales_discount
                if sale.is_active == True:
                    for product in queryset:
                        multiplier = discount / 100
                        old_price = product.price
                        new_price = ceil(old_price - (old_price * multiplier))
                        product.discounted_price = new_price
                        product.save(update_fields=['discounted_price'])

                elif sale.is_active == False:
                    return messages.warning(request, 'No active Discounts')
                else:
                    print(sale.id)
                    return messages.warning(request, 'No Discounts')
        sales_discount.short_description = format_html(
            "
Apply discount
")
        def get_urls(self):
            urls = super().get_urls()
            new_urls = [path('upload-csv/', self.upload_csv), ]
            return new_urls + urls

        def upload_csv(self, request):
            # file = open('sample.csv')
            # csv_reader = csv.reader(file)
            # next(csv_reader)
            try:
                if request.method == "POST":
                    csv_file = request.FILES["csv_upload"]
                    if not csv_file.name.endswith('.csv'):
                        messages.warning(request, 'Please upload csv file')
                        return HttpResponseRedirect(request.path_info)
                    file_data = csv_file.read().decode("utf-8")
                    csv_data = file_data.split("\n")

                    for x in csv_data:
                        print(x, type(x))
                        fields = x.split(",")

                        try:
                            for i in range(len(fields)):
                                print("fields[{}]".format(i),fields[i])
                            created, k = Product.objects.update_or_create(
                                title=fields[0],
                                sku=fields[1],
                                short_description=fields[2],
                                detail_description=fields[3],
                                specification=fields[4],
                                price=fields[5],
                                discounted_price=fields[6],
                                category=Category.objects.get(pk=(fields[7])),
                                subcategory=subcategory.objects.get(pk=(fields[8])),
                                type=producttype.objects.get(pk=(fields[9])),
                                brand=Brand.objects.get(pk=(fields[10])),
                                amps=currentrating.objects.get(pk=(fields[11])),
                                volts=voltagerating.objects.get(pk=(fields[12])),
                                attributes=Attributes.objects.get(pk=(fields[13])),
                                available_stocks=fields[14],
                            )
                            n = str(x)
                            l = n.split('"')
                            if len(l) > 1:
                                k = l[1].split('"')
                                images_csv = (k[0].split('"'))
                                splited_image = (images_csv[0].split(','))

                                for i in range(len(splited_image)):
                                    iter_image = splited_image[i]

                                    imagess = image.objects.get(pk=(int(iter_image)))
                                    print("2222222",imagess)
                                    created.image.add(imagess)
                                    created.save()
                            else:
                                imagess = image.objects.get(pk=fields[15])
                                print("2222222",imagess)
                                print(imagess)
                                created.image.add(imagess)
                                created.save()
                                print("saved")
                        except IndexError:
                            pass

                        except (ValidationError, IntegrityError):
                            form = CsvImportForm()
                            data = {"form": form}
                            message = messages.warning(
                                request, "Something went wrong! check your file again \n 1.Upload correct file \n 2.Check your data once")
                            return render(request, "admin/csv_upload.html", data)
                        except TypeError as e:
                            form = CsvImportForm()
                            data = {"form": form}
                            message = messages.warning(request, e)
                            #message=messages.warning(request,"Check the category ID")
                            return render(request, "admin/csv_upload.html", data)
                        except:
                            form = CsvImportForm()
                            data = {"form": form}
                            message = messages.warning(
                                request, "category or image query doesnt exist")
                            return render(request, "admin/csv_upload.html", data)
                    url = reverse('admin:index')
                    return HttpResponseRedirect(url)

                form = CsvImportForm()
                data = {"form": form}
                return render(request, "admin/csv_upload.html", data)
            except (IntegrityError) as e:
                messages.error(request, e)
    except exception:
        pass
class subcategoryadmin(admin.ModelAdmin):
    list_display = ['id','sub_category', 'category',]
    # search_fields = ['category','Question']
    # actions = ['make_published', 'make_withdraw']
    list_editable= ['sub_category']

class AttributesAdmin(admin.ModelAdmin):
    list_display = ('id', 'Color','action_btn')
    list_filter = ('Color',)
    list_per_page = 20
    search_fields = ('Color',)
    save_on_top = True

    def action_btn(self, obj):
        html = "

"
        html += "

"
        html += "
"
        return format_html(html)
    action_btn.short_description = "Action"
class salesAdmin(admin.ModelAdmin):
    list_display = ('id', 'campaign_name', 'startdate', 'enddate',
                    'sales_discount', 'is_active', 'created_at', 'action_btn')
    list_filter = ('campaign_name', 'startdate', 'enddate', 'sales_discount')
    list_editable = ('is_active', )
    list_per_page = 20
    search_fields = ('campaign_name', 'startdate', 'enddate')
    save_on_top = True
    def action_btn(self, obj):
        html = "

"
        html += "

"
        html += "
"
        return format_html(html)
    action_btn.short_description = "Action"
class CoupenAdmin(admin.ModelAdmin):
    list_display = ['id', 'coupon', 'coupon_discount',
                    'startdate', 'enddate', 'created_at', 'action_btn']
    #list_filter = ['is_active','startdate','enddate']
    search_fields = ['created_at']
    #list_editable = ('is_active', )

    def action_btn(self, obj):
        html = "

"
        html += "

"
        html += "
"
        return format_html(html)
    action_btn.short_description = "Action"

class RatingAdmin(admin.ModelAdmin):
    list_display = ['id', 'user', 'product', 'Reviews', 'Rating',
                     'status_', 'created_date', 'action_btn']
    search_fields = ['user']
    actions = ['approved_status', 'rejected_status']

    def has_add_permission(self, request):
        return True

    def action_btn(self, obj):
        html = "
"
        return format_html(html)
    action_btn.short_description = "Delete"

    @admin.action(description='Approve')
    def approved_status(self, request, queryset):
        queryset.update(Status="Approved")

    @admin.action(description='Reject')
    def rejected_status(self, request, queryset):
        queryset.update(Status='Rejected')

class BlogAdmin(SummernoteModelAdmin):
    try:
        """ def dummy(self,obj):
            html="<object style='height: 100px; width: 100%'><param name='movie' value='https://youtu.be/HYOvEIimVzI'>"
            html+="<param name='allowFullScreen' value='true'><param name='allowScriptAccess' value='always'>"
            html+="<embed src='https://youtu.be/HYOvEIimVzI' type='application/x-shockwave-flash' allowfullscreen='true' allowScriptAccess='always' width='640' height='390'></object>"
            return format_html(html)
        dummy.allow_tags=True"""

        def imagee(self, obj):
            # a=obj.image.first()
            # print(obj.image.first().image.url)
            try:
                return format_html(''.format(obj.image.first().image.url, "100", "100"))
            except:
                return format_html('')
        imagee.short_description = 'Image'
        imagee.allow_tags = True
        def image_tag(self, obj):
            try:
                return format_html(''.format(obj.images.url, "100", "100"))
            except:
                return format_html('')
                #message=messages.warning('Something went wrong! check your file again \n 1.Upload correct file \n 2.Check you data once')
        image_tag.short_description = 'Thumbnail Image'
        image_tag.allow_tags = True

        def video_url(self, obj):
            return format_html('Play video' % (obj.url))
            # return format_html('' % (obj.url))
        video_url.allow_tags = True
        video_url.short_description = 'video'
        list_display = ['id', 'title', 'detail_description',
                        'imagee','location','uploaded_date','action_btn']
        search_fields = ['title']
        summernote_fields = ('description', )

        def action_btn(self, obj):
            html = "

"
            # html += "

"

            html += "

".format(
                    obj.image.first().image.url, "100", "100")
            html += "
"
            return format_html(html)
        action_btn.short_description = "Action"
    except:
        pass

class FAQAdmin(admin.ModelAdmin):
    list_display = ['id','category', 'Question', 'Answer',
                    'status', 'created_date', 'updated_at']
    search_fields = ['category','Question']
    actions = ['make_published', 'make_withdraw']
    list_editable= ['category']
    @admin.action(description='Publish')
    def make_published(modeladmin, request, queryset):
        queryset.update(status='p')

    @admin.action(description='Withdraw')
    def make_withdraw(modeladmin, request, queryset):
        queryset.update(status='w')

class BannerAdmin(admin.ModelAdmin):
    def image_tag(self, obj):
        try:
            return format_html(''.format(obj.image.url, "100", "100"))
        except:
            return format_html('')
            #message=messages.warning('Something went wrong! check your file again \n 1.Upload correct file \n 2.Check you data once')
    image_tag.short_description = 'Image'
    image_tag.allow_tags = True
    list_display = ['id', 'title', 'image_tag','is_active','uploaded_date', 'action_btn']
    list_editable=['is_active']
    search_fields = ['title']
    def action_btn(self, obj):
        html = "

"
        # html += "

"
        html += "

".format(
                    obj.image.url, "100", "100")
        html += "
"
        return format_html(html)
    action_btn.short_description = "Action"
from django.core.mail import EmailMessage
from django.template.loader import render_to_string,select_template
class customer_messageAdmin(admin.ModelAdmin):
    list_display = ['id', 'first_name', 'last_name', 'Phone',
                    'Email', 'Message', 'created_date', 'updated_at', 'action_btn']
    actions = ["send_message"]

    def action_btn(self, obj):
        html = "
"
        return format_html(html)
    action_btn.short_description = "Action"
    def send_message(self, request, queryset):
        print(queryset)
        # try:
        for i in queryset:
            if i.Email and i.first_name:
                html_tpl_path=("email.html",)
                context_data={"name":"sandeep"}
                email_html_template=select_template(html_tpl_path).render(context_data)
                email = i.Email
                message = "Hi {},\nyour message was recieved\nthank you ".format(i.first_name)
                a = send_mail('Prakash Electrical', email_html_template, settings.EMAIL_HOST_USER, [
                                email], fail_silently=False,),messages.success(request, "Successfully sent to {}".format(email))
    # actions = ["send_message"]
class mailadmin(admin.ModelAdmin):
    list_display = ['subject', 'message', 'send_it',
                    'created_date', 'updated_at', 'action_btnn']
    actions = ['send_message',]
    list_editable = ['send_it']

    def action_btnn(self, obj):
        html = "

"
        html += "

"
        html += "
"
        return format_html(html)
    action_btnn.short_description = "Action"

    def get_form(self, request, obj=None, **kwargs):
        # if obj.type == "1":
        self.exclude = ("send_it", )
        form = super(mailadmin, self).get_form(request, obj, **kwargs)
        return form
class cartadmin(ExportActionMixin,admin.ModelAdmin):
    list_display=['id','user','product','attributes','price','offer_price','quantity','Total_amount','amount_saved','date','updated_at']
    list_editable = ['product','attributes','quantity']
    search_fields = ['user__username','product__title']

class latestproductadmin(admin.ModelAdmin):
    list_display=['id','product','created_date','action_btnn']
    list_editable=['product']
    def action_btnn(self, obj):
        html = "

"
        html += "

"
        html += "
"
        return format_html(html)
    action_btnn.short_description = "Action"

    MAX_OBJECTS = 1

    def has_add_permission(self, request):
        if self.model.objects.count() >= 10:
            return False
        return super().has_add_permission(request)
class mostselledproductadmin(admin.ModelAdmin):
    list_display=['id','product','created_date','action_btnn']
    list_editable=['product']

    def action_btnn(self, obj):
        html = "

"
        html += "

"
        html += "
"
        return format_html(html)
    action_btnn.short_description = "Action"
    def has_add_permission(self, request):
        if self.model.objects.count() >= 10:
            return False
        return super().has_add_permission(request)
class newsletterproductadmin(admin.ModelAdmin):
    list_display=['id','Email','subscribed_date']
    # list_editable=['']
    search_fields = ['Email']
class myaccount(admin.ModelAdmin):
    list_display=['id','user','is_confirmed']
    list_editable=['is_confirmed']
    # search_fields = ['Email']
class socialmedialinksadmin(admin.ModelAdmin):
    list_display=['id','social_media','links','action_btnn']
    list_editable=['links']
    def action_btnn(self, obj):
        html = "

"
        html += "

"
        html += "
"
        return format_html(html)
    action_btnn.short_description = "Action"

class faq_enquiryAdmin(admin.ModelAdmin):
    list_display = ['id', 'name', 'Phone',
                    'Email', 'Message', 'created_date', 'updated_at', 'action_btn']
    def action_btn(self, obj):
        html = "
"
        return format_html(html)
    action_btn.short_description = "Action"
class notificationadmin(admin.ModelAdmin):
    list_display=["id",'message','created_date']
class userphotoadmin(admin.ModelAdmin):
    list_display=['id','user','photo']
class cartorderadmin(admin.ModelAdmin):
    list_display=["id","user","order_payment_id","total_price","shipping_address","is_paid","date","status"]
    list_editable=["status"]
class paymentadmin(admin.ModelAdmin):
    list_display=["id","order_id","payment_id","signature_id","amount"]
class refundadmin(admin.ModelAdmin):
    list_display=["refundId","entity","currency","amount","payment_id","created_at","status","speed_processed","speed_requested"]
class infoadmin(admin.ModelAdmin):
    list_display=['id','phone','mail','Address','about','is_active']
    list_editable=['phone',"mail","is_active"]
    MAX_OBJECTS = 1
    def has_add_permission(self, request):
        if self.model.objects.count() >= 1:
            return False
        return super().has_add_permission(request)
class typeadmin(admin.ModelAdmin):
    list_display=["id","type",]
class currentadmin(admin.ModelAdmin):
    list_display=["id","rating",]
class voltageadmin(admin.ModelAdmin):
    list_display=["id","rating",]

admin.site.register(userphoto,userphotoadmin)
admin.site.register(notification,notificationadmin)
admin.site.register(enquiryform,faq_enquiryAdmin)
admin.site.register(socialmedialinks,socialmedialinksadmin)
# admin.site.register(latest_product,latestproductadmin)
admin.site.register(most_selled_products,mostselledproductadmin)
admin.site.register(newsletter,newsletterproductadmin)
admin.site.register(Cart,cartadmin)
admin.site.register(Category, CategoryAdmin)
admin.site.register(Product, ProductAdmin)
admin.site.register(Address, AddressAdmin)
admin.site.register(Attributes, AttributesAdmin)
admin.site.register(sales, salesAdmin)
admin.site.register(image, imageAdmin)
admin.site.register(Coupon, CoupenAdmin)
admin.site.register(Rating, RatingAdmin)
admin.site.register(Blog, BlogAdmin)
admin.site.register(FAQ, FAQAdmin)
admin.site.register(customer_message, customer_messageAdmin)
admin.site.register(Banner, BannerAdmin)
admin.site.register(MailText, mailadmin)
admin.site.unregister(get_attachment_model())
admin.site.unregister(Group)
admin.site.register(Brand,BrandAdmin)
admin.site.register(my_account,myaccount)
admin.site.register(subcategory,subcategoryadmin)
admin.site.register(cart_order,cartorderadmin)
admin.site.register(payment,paymentadmin)
admin.site.register(refund,refundadmin)
admin.site.register(redeemedcoupons)
admin.site.register(info,infoadmin)
admin.site.register(producttype,typeadmin)
admin.site.register(currentrating,currentadmin)
admin.site.register(voltagerating,voltageadmin)
# admin.site.register(cart2)
# admin.site.register(notificationn)
class UserAdmin(ExportActionMixin,OriginalUserAdmin):
    list_display = ['id','username', 'email','is_staff', 'phone_no','is_confirmed','is_staff','action_btn','last_login','date_joined']
    list_editable=['is_confirmed']

    def action_btn(self, obj):
        html = "

"
        html += "

"
        html += "
"
        return format_html(html)
    action_btn.short_description = "Action"
admin.site.register(User, UserAdmin)


from flask import Flask, render_template, request, redirect, url_for, flash
from flask_mysqldb import MySQL

app = Flask(__name__)
#mysql conection
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = '1234'
app.config['MYSQL_DB'] = 'productosPRYT'
mysql = MySQL(app)
#sesion
app.secret_key = 'mysecretkey'

@app.route('/')
def index():
    cur = mysql.connection.cursor()
    cur.execute('SELECT * FROM productos')
    data = cur.fetchall()
    #print(data)
    return render_template("index.html", productos=data)

@app.route('/add_product', methods=['POST'])
def addproduct():
    if request.method == 'POST':
        nompr = request.form['nompr']
        preciopr = request.form['preciopr']
        categoriapr = request.form['categoriapr']
        fechaexppr = request.form['fechaexppr']
        cur = mysql.connection.cursor()
        cur.execute('INSERT INTO productos (nombre,precio,categoria,fechaexp) values (%s, %s, %s, %s)',
                    (nompr, preciopr, categoriapr, fechaexppr))
        mysql.connection.commit()
        flash('Producto agregado satisfactoriamente')
    return redirect(url_for('index'))

@app.route('/edit/<id>')
def obtenerProductos(id):
    cur = mysql.connection.cursor()
    cur.execute("SELECT * FROM productos WHERE idproductos = %s", (id))
    data = cur.fetchone()
    #print(data)
    return render_template('edit-pr.html', productos = data)

@app.route('/update/<id>', methods=['POST'])
def actualizarProd(id):
    if request.method == 'POST':
        nompr = request.form['nompr']
        preciopr = request.form['preciopr']
        categoriapr = request.form['categoriapr']
        fechaexppr = request.form['fechaexppr']
        cur = mysql.connection.cursor()
        cur.execute("""
        UPDATE productos SET nombre = %s,
            precio = %s,
            categoria = %s,
            fechaexp = %s
        WHERE idproductos = %s
        """, (nompr, preciopr, categoriapr, fechaexppr, id))
        mysql.connection.commit()
        flash('producto actualizado satisfactoriamente')
        return redirect(url_for('index'))

@app.route('/delete/<string:id>')
def borrarProductos(id):
    cur = mysql.connection.cursor()
    cur.execute("DELETE FROM productos WHERE idproductos = {0}".format(id))
    mysql.connection.commit()
    flash('producto removido satisfactoriamente!')
    return redirect(url_for('index'))

if __name__=="__main__":
    app.run(debug=True)


"""
Name: git-init.py
Discription: This script is used to initialize a git environment.
Usage: python3 git-init.py
Author: hdaojin
Version: 1.0.1
Date: 2022-10-4
Update: 2023-03-15
"""

import os
import sys
import platform
from pathlib import Path

# Check if the git is installed
def check_git():
    """
    Check if the git is installed
    """
    if os.system("git --version") != 0:
        print("Please install git first!")
        sys.exit(1)
    print("git is installed!")

# Check if the ssh package is installed
def check_ssh():
    """
    Check if the ssh package is installed
    """
    if os.system("ssh -V") != 0:
        print("Please install ssh first!")
        sys.exit(1)
    print("ssh is installed!")

# Configure git user name and email
def configure_git(name, mail):
    """
    Configure git user name and email
    """
    os.system("git config --global user.name " + name)
    os.system("git config --global user.email " + mail)
    print("git user name and email is configured!")

# Generate ssh key
def generate_ssh_key(mail):
    """
    Generate ssh key
    """
    ssh_key_file = Path.home() / ".ssh/id_ed25519"
    ssh_pub_key_file = Path.home() / ".ssh/id_ed25519.pub"
    if not ssh_key_file.exists() or not ssh_pub_key_file.exists():
        os.system("ssh-keygen -t ed25519 -C " + mail)
    print("ssh key is generated!")
    return ssh_key_file, ssh_pub_key_file

# Set ssh client config
def set_ssh_client_config(name, ssh_key_file):
    """
    Set ssh client config
    """
    ssh_config_file = Path.home() / ".ssh/config"
    with open(ssh_config_file, "a+") as f:
        f.write("\n")
        f.write("Host github.com\n")
        f.write("    HostName github.com\n")
        f.write("    Port 22\n")
        f.write("    User " + name + "\n")
        f.write("    IdentityFile " + str(ssh_key_file) + "\n")
    print("ssh client config is set!")

# Copy ssh public key to clipboard
def copy_ssh_pub_key(ssh_pub_key_file):
    """
    Copy ssh public key to clipboard
    """
    if os_platform == "Windows":
        os.system("clip < " + str(ssh_pub_key_file))
    elif os_platform == "Linux":
        os.system("xclip -sel clip < " + str(ssh_pub_key_file))
    elif os_platform == "Darwin":
        os.system("pbcopy < " + str(ssh_pub_key_file))
    print("ssh public key is copied to clipboard!")
    print("If not work, please copy it manually: \n")
    print(ssh_pub_key_file.read_text())

def test_ssh_to_github():
    """
    Test ssh to GitHub
    """
    if os.system("ssh -T git@github.com") == 0:
        print("ssh to GitHub is OK!")
    else:
        print("ssh to GitHub is failed!")
        sys.exit(1)

# Main function
def main():
    """
    Main function
    """
    check_git()
    check_ssh()
    name = input("Please input your name: ")
    mail = input("Please input your email: ")
    configure_git(name, mail)
    ssh_key_file, ssh_pub_key_file = generate_ssh_key(mail)
    set_ssh_client_config(name, ssh_key_file)
    copy_ssh_pub_key(ssh_pub_key_file)
    print("""Next,
             1. Login your github account;
             2. Click "Settings" -> "SSH and GPG keys" -> "New SSH key";
    """)
    test_ssh_to_github()

if __name__ == "__main__":
    # Check system platform
    if platform.system() == "Windows":
        os_platform = "Windows"
    elif platform.system() == "Linux":
        os_platform = "Linux"
    elif platform.system() == "Darwin":
        os_platform = "Darwin"
    else:
        print("This script only supports Windows, Linux and MacOS!")
        sys.exit(1)
    main()


# -*- coding: utf-8 -*-

import jsonschema
import sys

def clean_doc(doc):
    """
    Clean given JSON document from keys where its value is None
    :param doc: Pure, dirty JSON
    :return: Cleaned JSON document
    """
    for key, value in list(doc.items()):
        if value is None:
            del doc[key]
        elif isinstance(value, dict):
            clean_doc(value)
    return doc

def is_valid(doc, schema):
    """
    Checks if given doc is valid against given schema
    :param doc: to be validated JSON
    :param schema: base JSON
    :return: a boolean result and error
    """
    try:
        jsonschema.validate(doc, schema)
        sys.stdout.write("OK\n")
        return True, None

    except jsonschema.exceptions.ValidationError as val_err:
        sys.stderr.write("FAIL\n")
        return False, val_err